Skip to content

Commit

Permalink
[HttpKernel] add deprecation for controler:method syntax in ServiceVa…
Browse files Browse the repository at this point in the history
…lueResolver

Single colon syntax when referencing a controller as a service
is deprecated since Symfony 4.1 but there was no deprecations
trigger on runtime.

See symfony#35909
  • Loading branch information
noemi-salaun committed Mar 9, 2020
1 parent 0578fdf commit f35855a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
Expand Up @@ -73,6 +73,16 @@ public function resolve(Request $request, ArgumentMetadata $argument)
$controller = substr($controller, 0, $i).strtolower(substr($controller, $i));
}

if (false !== strpos($controller, ':') && false === strpos($controller, '::')) {
$i = strrpos($controller, ':');
$controllerName = substr($controller, 0, $i);
$methodName = substr($controller, $i + 1);
@trigger_error(
sprintf('Referencing a controller as "%1$s:%2$s" is deprecated since Symfony 4.1, use "%1$s::%2$s" instead.', $controllerName, $methodName),
E_USER_DEPRECATED
);
}

try {
yield $this->container->get($controller)->get($argument->getName());
} catch (RuntimeException $e) {
Expand Down
Expand Up @@ -105,6 +105,29 @@ public function testControllerNameIsAnArray()
$this->assertYieldEquals([new DummyService()], $resolver->resolve($request, $argument));
}

/**
* @group legacy
* @expectedDeprecation Referencing a controller as "App\Controller\Mine:method" is deprecated since Symfony 4.1, use "App\Controller\Mine::method" instead.
*/
public function testExistingControllerWithSingleColonTriggersDeprecation()
{
$resolver = new ServiceValueResolver(new ServiceLocator([
'App\\Controller\\Mine:method' => function () {
return new ServiceLocator([
'dummy' => function () {
return new DummyService();
},
]);
},
]));

$request = $this->requestWithAttributes(['_controller' => '\\App\\Controller\\Mine:method']);
$argument = new ArgumentMetadata('dummy', DummyService::class, false, false, null);

$this->assertTrue($resolver->supports($request, $argument));
$this->assertYieldEquals([new DummyService()], $resolver->resolve($request, $argument));
}

/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
* @expectedExceptionMessage Cannot autowire argument $dummy of "Symfony\Component\HttpKernel\Tests\Controller\ArgumentResolver\DummyController::index()": it references class "Symfony\Component\HttpKernel\Tests\Controller\ArgumentResolver\DummyService" but no such service exists.
Expand Down

0 comments on commit f35855a

Please sign in to comment.