2016-06-30 23:30:37 +02:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* This file is part of the ApiDocBundle package.
|
|
|
|
|
*
|
|
|
|
|
* (c) EXSyst
|
|
|
|
|
*
|
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
|
*/
|
|
|
|
|
|
2016-07-15 00:40:30 +02:00
|
|
|
|
namespace EXSyst\Bundle\ApiDocBundle\Describer;
|
2016-06-30 23:30:37 +02:00
|
|
|
|
|
|
|
|
|
use Doctrine\Common\Util\ClassUtils;
|
2016-07-28 10:20:59 +02:00
|
|
|
|
use EXSyst\Bundle\ApiDocBundle\RouteDescriber\RouteDescriberInterface;
|
2016-08-01 19:58:57 +02:00
|
|
|
|
use EXSyst\Component\Swagger\Swagger;
|
2016-06-30 23:30:37 +02:00
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser;
|
2016-07-29 10:22:40 +02:00
|
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
2016-06-30 23:30:37 +02:00
|
|
|
|
use Symfony\Component\Routing\Route;
|
2016-11-30 14:08:10 +01:00
|
|
|
|
use Symfony\Component\Routing\RouteCollection;
|
2016-06-30 23:30:37 +02:00
|
|
|
|
|
2016-07-15 00:40:30 +02:00
|
|
|
|
class RouteDescriber implements DescriberInterface
|
2016-06-30 23:30:37 +02:00
|
|
|
|
{
|
2016-07-29 10:22:40 +02:00
|
|
|
|
private $container;
|
2016-11-30 14:08:10 +01:00
|
|
|
|
private $routeCollection;
|
2016-07-29 10:22:40 +02:00
|
|
|
|
private $controllerNameParser;
|
2016-07-15 00:40:30 +02:00
|
|
|
|
private $routeDescribers;
|
2016-06-30 23:30:37 +02:00
|
|
|
|
|
|
|
|
|
/**
|
2016-07-29 10:22:40 +02:00
|
|
|
|
* @param ContainerInterface $container
|
2016-11-30 14:08:10 +01:00
|
|
|
|
* @param RouteCollection $routeCollection
|
2016-06-30 23:30:37 +02:00
|
|
|
|
* @param ControllerNameParser $controllerNameParser
|
2016-07-15 00:40:30 +02:00
|
|
|
|
* @param RouteDescriberInterface[] $routeDescribers
|
2016-06-30 23:30:37 +02:00
|
|
|
|
*/
|
2016-11-30 14:08:10 +01:00
|
|
|
|
public function __construct(ContainerInterface $container, RouteCollection $routeCollection, ControllerNameParser $controllerNameParser, array $routeDescribers)
|
2016-06-30 23:30:37 +02:00
|
|
|
|
{
|
2016-07-29 10:22:40 +02:00
|
|
|
|
$this->container = $container;
|
2016-11-30 14:08:10 +01:00
|
|
|
|
$this->routeCollection = $routeCollection;
|
2016-06-30 23:30:37 +02:00
|
|
|
|
$this->controllerNameParser = $controllerNameParser;
|
2016-07-15 00:40:30 +02:00
|
|
|
|
$this->routeDescribers = $routeDescribers;
|
2016-06-30 23:30:37 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-28 10:20:59 +02:00
|
|
|
|
public function describe(Swagger $api)
|
2016-06-30 23:30:37 +02:00
|
|
|
|
{
|
2016-07-15 00:40:30 +02:00
|
|
|
|
if (0 === count($this->routeDescribers)) {
|
2016-06-30 23:30:37 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-30 14:08:10 +01:00
|
|
|
|
foreach ($this->routeCollection->all() as $route) {
|
2016-06-30 23:30:37 +02:00
|
|
|
|
// if able to resolve the controller
|
|
|
|
|
if ($method = $this->getReflectionMethod($route->getDefault('_controller'))) {
|
|
|
|
|
// Extract as many informations as possible about this route
|
2016-07-15 00:40:30 +02:00
|
|
|
|
foreach ($this->routeDescribers as $describer) {
|
2016-07-28 10:20:59 +02:00
|
|
|
|
$describer->describe($api, $route, $method);
|
2016-06-30 23:30:37 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the ReflectionMethod for the given controller string.
|
|
|
|
|
*
|
|
|
|
|
* @param string $controller
|
|
|
|
|
*
|
|
|
|
|
* @return \ReflectionMethod|null
|
|
|
|
|
*/
|
2016-07-15 00:40:30 +02:00
|
|
|
|
private function getReflectionMethod(string $controller)
|
2016-06-30 23:30:37 +02:00
|
|
|
|
{
|
|
|
|
|
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];
|
|
|
|
|
} elseif (class_exists($controller)) {
|
|
|
|
|
$class = $controller;
|
|
|
|
|
$method = '__invoke';
|
|
|
|
|
} else {
|
|
|
|
|
if (preg_match('#(.+):([\w]+)#', $controller, $matches)) {
|
|
|
|
|
$controller = $matches[1];
|
|
|
|
|
$method = $matches[2];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($this->container->has($controller)) {
|
|
|
|
|
if (class_exists(ClassUtils::class)) {
|
|
|
|
|
$class = ClassUtils::getRealClass(get_class($this->container->get($controller)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!isset($method) && method_exists($class, '__invoke')) {
|
|
|
|
|
$method = '__invoke';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isset($class) && isset($method)) {
|
|
|
|
|
try {
|
|
|
|
|
return new \ReflectionMethod($class, $method);
|
|
|
|
|
} catch (\ReflectionException $e) {
|
2016-11-30 14:17:42 +01:00
|
|
|
|
// In case we can't reflect the controller, we just
|
|
|
|
|
// ignore the route
|
2016-06-30 23:30:37 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|