-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DependencyInjection] Automatic registration of FQCN services #18268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/Symfony/Component/DependencyInjection/AmbiguousDefinition.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\DependencyInjection; | ||
|
||
class AmbiguousDefinition extends Definition | ||
{ | ||
private $services; | ||
|
||
public function __construct($class, array $services) | ||
{ | ||
parent::__construct($class, [$class, $services]); | ||
$this->setFactory([AmbiguousService::class, 'throwException']); | ||
$this->services = $services; | ||
} | ||
|
||
public function getServices() | ||
{ | ||
return $this->services; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/Symfony/Component/DependencyInjection/AmbiguousService.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\DependencyInjection; | ||
|
||
use Symfony\Component\DependencyInjection\Exception\AmbiguousServiceException; | ||
|
||
class AmbiguousService | ||
{ | ||
static public function throwException($class, $services) | ||
{ | ||
throw new AmbiguousServiceException($class, $services); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
src/Symfony/Component/DependencyInjection/Compiler/CheckAmbiguousReferencesPass.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\AmbiguousDefinition; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Exception\AmbiguousReferenceException; | ||
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
/** | ||
* @author Martin Hasoň <martin.hason@gmail.com> | ||
*/ | ||
class CheckAmbiguousReferencesPass implements CompilerPassInterface | ||
{ | ||
/** @var ContainerBuilder */ | ||
private $container; | ||
|
||
public function process(ContainerBuilder $container) | ||
{ | ||
$this->container = $container; | ||
foreach ($container->getDefinitions() as $id => $definition) { | ||
$this->processArguments($id, $definition->getArguments()); | ||
$this->processArguments($id, $definition->getMethodCalls()); | ||
$this->processArguments($id, $definition->getProperties()); | ||
$this->processFactory($id, $definition->getFactory()); | ||
} | ||
} | ||
|
||
private function processArguments($id, array $arguments) | ||
{ | ||
foreach ($arguments as $argument) { | ||
$definition = $argument; | ||
|
||
if (is_array($argument)) { | ||
$this->processArguments($id, $argument); | ||
} elseif ($argument instanceof Reference) { | ||
try { | ||
$definition = $this->container->findDefinition((string) $argument); | ||
} catch (ServiceNotFoundException $e) { | ||
continue; | ||
} | ||
} | ||
|
||
if ($definition instanceof AmbiguousDefinition) { | ||
throw new AmbiguousReferenceException($definition->getClass(), $id, $definition->getServices()); | ||
} | ||
} | ||
} | ||
|
||
private function processFactory($factory) | ||
{ | ||
if (null === $factory || !is_array($factory) || !$factory[0] instanceof Reference) { | ||
return; | ||
} | ||
|
||
$definition = $this->container->findDefinition($id = (string) $factory[0]); | ||
|
||
if ($definition instanceof AmbiguousDefinition) { | ||
throw new AmbiguousReferenceException($definition->getClass(), $id, $definition->getServices()); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
src/Symfony/Component/DependencyInjection/Compiler/RegisterClassNamedServicesPass.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\Alias; | ||
use Symfony\Component\DependencyInjection\AmbiguousDefinition; | ||
use Symfony\Component\DependencyInjection\Container; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
|
||
/** | ||
* @author Martin Hasoň <martin.hason@gmail.com> | ||
*/ | ||
class RegisterClassNamedServicesPass implements CompilerPassInterface | ||
{ | ||
private $container; | ||
private $types; | ||
private $definedTypes; | ||
private $reflectionClasses = array(); | ||
|
||
public function process(ContainerBuilder $container) | ||
{ | ||
$this->container = $container; | ||
$this->types = array(); | ||
$this->definedTypes = array(); | ||
|
||
//$this->populateAvailableTypes('service_container', new Definition(Container::class)); | ||
|
||
foreach ($container->getDefinitions() as $id => $definition) { | ||
$this->populateAvailableTypes($id, $definition); | ||
} | ||
|
||
foreach ($this->types as $type => $services) { | ||
$this->registerService($type, $services); | ||
} | ||
|
||
$this->container = null; | ||
$this->types = array(); | ||
$this->definedTypes = array(); | ||
$this->reflectionClasses = array(); | ||
} | ||
|
||
private function populateAvailableTypes($id, Definition $definition) | ||
{ | ||
if ($definition->isAbstract()) { | ||
return; | ||
} | ||
|
||
if (!$reflectionClass = $this->getReflectionClass($id, $definition)) { | ||
return; | ||
} | ||
|
||
foreach ($definition->getAutowiringTypes() as $type) { | ||
$this->definedTypes[$type] = true; | ||
$this->set($type, $id); | ||
} | ||
|
||
foreach ($reflectionClass->getInterfaces() as $interfaceClass) { | ||
$this->set($interfaceClass->name, $id); | ||
} | ||
|
||
do { | ||
$this->set($reflectionClass->name, $id); | ||
} while ($reflectionClass = $reflectionClass->getParentClass()); | ||
} | ||
|
||
private function getReflectionClass($id, Definition $definition) | ||
{ | ||
if (isset($this->reflectionClasses[$id])) { | ||
return $this->reflectionClasses[$id]; | ||
} | ||
|
||
if (!$class = $definition->getClass()) { | ||
return; | ||
} | ||
|
||
$class = $this->container->getParameterBag()->resolveValue($class); | ||
|
||
try { | ||
return $this->reflectionClasses[$id] = new \ReflectionClass($class); | ||
} catch (\ReflectionException $reflectionException) { | ||
return; | ||
} | ||
} | ||
|
||
private function set($type, $id) | ||
{ | ||
if (isset($this->definedTypes[$type])) { | ||
return; | ||
} | ||
|
||
$this->types[$type][] = $id; | ||
} | ||
|
||
private function registerService($type, array $services) | ||
{ | ||
if (1 === count($services)) { | ||
$service = reset($services); | ||
//$public = 'service_container' === $service ?: $this->container->getDefinition($service)->isPublic(); | ||
$public = $this->container->getDefinition($service)->isPublic(); | ||
$this->container->setAlias($type, new Alias($service, $public)); | ||
} else { | ||
$this->container->setDefinition($type, new AmbiguousDefinition($type, $services)); | ||
} | ||
} | ||
} | ||
|
20 changes: 20 additions & 0 deletions
20
src/Symfony/Component/DependencyInjection/Exception/AmbiguousReferenceException.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\DependencyInjection\Exception; | ||
|
||
class AmbiguousReferenceException extends InvalidArgumentException | ||
{ | ||
public function __construct($type, $service, $services) | ||
{ | ||
parent::__construct(sprintf('Ambiguous services for class "%s". You should use concrete service name instead of class: "%s"', $type, implode('", "', $services))); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/Symfony/Component/DependencyInjection/Exception/AmbiguousServiceException.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\DependencyInjection\Exception; | ||
|
||
class AmbiguousServiceException extends RuntimeException | ||
{ | ||
public function __construct($type, $services) | ||
{ | ||
parent::__construct(sprintf('Ambiguous services for class "%s". You should use concrete service name instead of class: "%s"', $type, implode('", "', $services))); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/Symfony/Component/DependencyInjection/Tests/Compiler/CheckAmbiguousReferencePassTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\DependencyInjection\Tests\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
class CheckAmbiguousReferencePassTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** @var ContainerBuilder */ | ||
private $container; | ||
|
||
protected function setUp() | ||
{ | ||
$this->container = new ContainerBuilder(); | ||
require_once __DIR__.'/../Fixtures/includes/classes2.php'; | ||
} | ||
|
||
/** | ||
* @expectedException Symfony\Component\DependencyInjection\Exception\AmbiguousReferenceException | ||
* @expectedExceptionMessage Ambiguous services for class "Symfony\Component\DependencyInjection\Tests\Compiler\ClassNamedServices\E". You should use concrete service name instead of class: "foo", "bar" | ||
*/ | ||
public function testThrowExceptionForAmbiguousDefinitionInArguments() | ||
{ | ||
$container = $this->container; | ||
$container->register('foo', ClassNamedServices\E::class); | ||
$container->register('bar', ClassNamedServices\E::class); | ||
|
||
$definition = $container->register('baz', ClassNamedServices\A::class); | ||
$definition->setArguments(array(new Reference(ClassNamedServices\E::class))); | ||
|
||
$container->compile(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the goal of this class ?