NelmioApiDocBundle/Util/ControllerReflector.php
DemigodCode 50cf155855 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
2019-07-09 11:59:30 +02:00

129 lines
3.8 KiB
PHP
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\Util;
use Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Kernel;
/**
* @internal
*/
class ControllerReflector
{
private $container;
private $controllerNameParser;
private $controllers = [];
public function __construct(ContainerInterface $container, ControllerNameParser $controllerNameParser)
{
$this->container = $container;
$this->controllerNameParser = $controllerNameParser;
}
/**
* Returns the ReflectionMethod for the given controller string.
*
* @param string $controller
*
* @return \ReflectionMethod|null
*/
public function getReflectionMethod(string $controller)
{
$callable = $this->getClassAndMethod($controller);
if (null === $callable) {
return;
}
list($class, $method) = $callable;
try {
return new \ReflectionMethod($class, $method);
} catch (\ReflectionException $e) {
// In case we can't reflect the controller, we just
// ignore the route
}
}
public function getReflectionClassAndMethod(string $controller)
{
$callable = $this->getClassAndMethod($controller);
if (null === $callable) {
return;
}
list($class, $method) = $callable;
try {
return [new \ReflectionClass($class), new \ReflectionMethod($class, $method)];
} catch (\ReflectionException $e) {
// In case we can't reflect the controller, we just
// ignore the route
}
}
private function getClassAndMethod(string $controller)
{
if (isset($this->controllers[$controller])) {
return $this->controllers[$controller];
}
if (false === strpos($controller, '::') && 2 === substr_count($controller, ':')) {
$controller = $this->controllerNameParser->parse($controller);
}
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];
}
if ($this->container->has($controller)) {
$class = get_class($this->container->get($controller));
if (class_exists(ClassUtils::class)) {
$class = ClassUtils::getRealClass($class);
}
if (!isset($method) && method_exists($class, '__invoke')) {
$method = '__invoke';
}
}
}
if (!isset($class) || !isset($method)) {
$this->controllers[$controller] = null;
return;
}
return $this->controllers[$controller] = [$class, $method];
}
}