From 50cf155855b683d45da06779077d9fd84b608d75 Mon Sep 17 00:00:00 2001 From: DemigodCode <36764562+DemigodCode@users.noreply.github.com> Date: Tue, 9 Jul 2019 11:59:30 +0200 Subject: [PATCH] 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 --- Util/ControllerReflector.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Util/ControllerReflector.php b/Util/ControllerReflector.php index 568da23..7eba46e 100644 --- a/Util/ControllerReflector.php +++ b/Util/ControllerReflector.php @@ -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];