8000 Skip ContainerAwareInterface::setContainer from service_arguments act… · symfony/symfony@ad59e18 · GitHub
[go: up one dir, main page]

Skip to content

Commit ad59e18

Browse files
committed
Skip ContainerAwareInterface::setContainer from service_arguments actions registration
1 parent 549af73 commit ad59e18

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/Symfony/Component/HttpKernel/DependencyInjection/RegisterControllerArgumentLocatorsPass.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111

1212
namespace Symfony\Component\HttpKernel\DependencyInjection;
1313

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

6972
// get regular public methods
7073
$methods = array();
7174
$arguments = array();
7275
foreach ($r->getMethods(\ReflectionMethod::IS_PUBLIC) as $r) {
76+
if ('setContainer' === $r->name && $isContainerAware) {
77+
continue;
78+
}
7379
if (!$r->isConstructor() && !$r->isDestructor() && !$r->isAbstract()) {
7480
$methods[strtolower($r->name)] = array($r, $r->getParameters());
7581
}

src/Symfony/Component/HttpKernel/Tests/DependencyInjection/RegisterControllerArgumentLocatorsPassTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
16+
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
17+
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
1618
use Symfony\Component\DependencyInjection\ContainerBuilder;
1719
use Symfony\Component\DependencyInjection\ContainerInterface;
1820
use Symfony\Component\DependencyInjection\ServiceLocator;
@@ -187,6 +189,21 @@ public function testOptionalArgument()
187189
$expected = array('bar' => new ServiceClosureArgument(new TypedReference('bar', ControllerDummy::class, RegisterTestController::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE)));
188190
$this->assertEquals($expected, $locator->getArgument(0));
189191
}
192+
193+
public function testSkipSetContainer()
194+
{
195+
$container = new ContainerBuilder();
196+
$resolver = $container->register('argument_resolver.service')->addArgument(array());
197+
198+
$container->register('foo', ContainerAwareRegisterTestController::class)
199+
->addTag('controller.service_arguments');
200+
201+
$pass = new RegisterControllerArgumentLocatorsPass();
202+
$pass->process($container);
203+
204+
$locator = $container->getDefinition((string) $resolver->getArgument(0))->getArgument(0);
205+
$this->assertSame(array('foo:fooAction'), array_keys($locator));
206+
}
190207
}
191208

192209
class RegisterTestController
@@ -204,6 +221,15 @@ protected function barAction(ControllerDummy $bar)
204221
}
205222
}
206223

224+
class ContainerAwareRegisterTestController implements ContainerAwareInterface
225+
{
226+
use ContainerAwareTrait;
227+
228+
public function fooAction(ControllerDummy $bar)
229+
{
230+
}
231+
}
232+
207233
class ControllerDummy
208234
{
209235
}

0 commit comments

Comments
 (0)
0