Fix Controllers used as Services in Symfony >=4.1 (#1530)

* Symfony >4.1 ControllerService support

* Add usage

* StyleCI Fix

* Fix unittests deprecation for Symfony 4.3

* missing quote

* Revert "missing quote"

This reverts commit 25ca0a5b0b0ab01c7de52ae10b185ffeead38880.

* Revert "Fix unittests deprecation for Symfony 4.3"

This reverts commit bcdb51483d6052037820f1c669ee91a7099aced4.

* Test another deprecation fix

* Revert "Test another deprecation fix"

This reverts commit d9bc1b604e4ebec7789316472fd656ce30ee9e57.

* Syntax not removed in sf <4.1 only deprecated
This commit is contained in:
DemigodCode 2019-07-09 11:59:30 +02:00 committed by Guilhem Niot
parent 57f8cf4634
commit 50cf155855

View File

@ -13,6 +13,7 @@ namespace Nelmio\ApiDocBundle\Util;
use Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Kernel;
/**
* @internal
@ -85,10 +86,20 @@ class ControllerReflector
if (preg_match('#(.+)::([\w]+)#', $controller, $matches)) {
$class = $matches[1];
$method = $matches[2];
// Since symfony 4.1 routes are defined like service_id::method_name
if (Kernel::VERSION_ID >= 40100 && !class_exists($class)) {
if ($this->container->has($class)) {
$class = get_class($this->container->get($class));
if (class_exists(ClassUtils::class)) {
$class = ClassUtils::getRealClass($class);
}
}
}
} elseif (class_exists($controller)) {
$class = $controller;
$method = '__invoke';
} else {
// Has to be removed when dropping support of symfony < 4.1
if (preg_match('#(.+):([\w]+)#', $controller, $matches)) {
$controller = $matches[1];
$method = $matches[2];