Skip to content

[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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Exception\AmbiguousServiceException;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;

Expand All @@ -34,6 +35,10 @@ public static function assertSaneContainer(Container $container, $message = '')
try {
$container->get($id);
} catch (\Exception $e) {
if ($e instanceof AmbiguousServiceException) {
continue;
}

$errors[$id] = $e->getMessage();
}
}
Expand Down
29 changes: 29 additions & 0 deletions src/Symfony/Component/DependencyInjection/AmbiguousDefinition.php
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 src/Symfony/Component/DependencyInjection/AmbiguousService.php
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)
Copy link
Member

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 ?

{
throw new AmbiguousServiceException($class, $services);
}
}
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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function __construct()
new DecoratorServicePass(),
new ResolveParameterPlaceHoldersPass(),
new CheckDefinitionValidityPass(),
new RegisterClassNamedServicesPass(),
new ResolveReferencesToAliasesPass(),
new ResolveInvalidReferencesPass(),
new AutowirePass(),
Expand All @@ -57,6 +58,7 @@ public function __construct()
);

$this->removingPasses = array(
new CheckAmbiguousReferencesPass(),
new RemovePrivateAliasesPass(),
new RemoveAbstractDefinitionsPass(),
new ReplaceAliasByActualDefinitionPass(),
Expand Down
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));
}
}
}

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)));
}
}
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)));
}
}
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();
}
}
Loading