Skip to content

Commit

Permalink
bug #22340 [HttpKernel] Skip ContainerAwareInterface::setContainer fr…
Browse files Browse the repository at this point in the history
…om service_arguments actions registration (chalasr)

This PR was merged into the 3.3-dev branch.

Discussion
----------

[HttpKernel] Skip ContainerAwareInterface::setContainer from service_arguments actions registration

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | n/a

As it would never be used

Commits
-------

ad59e18 Skip ContainerAwareInterface::setContainer from service_arguments actions registration
  • Loading branch information
fabpot committed Apr 7, 2017
2 parents ec2cc08 + ad59e18 commit f04c0b5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
Expand Up @@ -11,9 +11,11 @@

namespace Symfony\Component\HttpKernel\DependencyInjection;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
Expand Down Expand Up @@ -65,11 +67,15 @@ public function process(ContainerBuilder $container)
if (!$r = $container->getReflectionClass($class)) {
throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
}
$isContainerAware = $r->implementsInterface(ContainerAwareInterface::class) || is_subclass_of($class, AbstractController::class);

// get regular public methods
$methods = array();
$arguments = array();
foreach ($r->getMethods(\ReflectionMethod::IS_PUBLIC) as $r) {
if ('setContainer' === $r->name && $isContainerAware) {
continue;
}
if (!$r->isConstructor() && !$r->isDestructor() && !$r->isAbstract()) {
$methods[strtolower($r->name)] = array($r, $r->getParameters());
}
Expand Down
Expand Up @@ -13,6 +13,8 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ServiceLocator;
Expand Down Expand Up @@ -187,6 +189,21 @@ public function testOptionalArgument()
$expected = array('bar' => new ServiceClosureArgument(new TypedReference('bar', ControllerDummy::class, RegisterTestController::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE)));
$this->assertEquals($expected, $locator->getArgument(0));
}

public function testSkipSetContainer()
{
$container = new ContainerBuilder();
$resolver = $container->register('argument_resolver.service')->addArgument(array());

$container->register('foo', ContainerAwareRegisterTestController::class)
->addTag('controller.service_arguments');

$pass = new RegisterControllerArgumentLocatorsPass();
$pass->process($container);

$locator = $container->getDefinition((string) $resolver->getArgument(0))->getArgument(0);
$this->assertSame(array('foo:fooAction'), array_keys($locator));
}
}

class RegisterTestController
Expand All @@ -204,6 +221,15 @@ protected function barAction(ControllerDummy $bar)
}
}

class ContainerAwareRegisterTestController implements ContainerAwareInterface
{
use ContainerAwareTrait;

public function fooAction(ControllerDummy $bar)
{
}
}

class ControllerDummy
{
}

0 comments on commit f04c0b5

Please sign in to comment.