NelmioApiDocBundle/Describer/RouteDescriber.php

68 lines
2.1 KiB
PHP
Raw Normal View History

2016-06-30 23:30:37 +02:00
<?php
/*
2016-12-29 12:09:26 +01:00
* This file is part of the NelmioApiDocBundle package.
2016-06-30 23:30:37 +02:00
*
2016-12-29 12:09:26 +01:00
* (c) Nelmio
2016-06-30 23:30:37 +02:00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
2016-12-29 12:09:26 +01:00
namespace Nelmio\ApiDocBundle\Describer;
2016-06-30 23:30:37 +02:00
2016-08-01 19:58:57 +02:00
use EXSyst\Component\Swagger\Swagger;
2016-12-29 12:09:26 +01:00
use Nelmio\ApiDocBundle\RouteDescriber\RouteDescriberInterface;
use Nelmio\ApiDocBundle\Util\ControllerReflector;
2016-06-30 23:30:37 +02:00
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
2016-06-30 23:30:37 +02:00
2017-01-14 17:36:56 +01:00
final class RouteDescriber implements DescriberInterface, ModelRegistryAwareInterface
2016-06-30 23:30:37 +02:00
{
2017-01-14 17:36:56 +01:00
use ModelRegistryAwareTrait;
private $routeCollection;
2017-12-22 17:42:18 +00:00
private $controllerReflector;
2017-12-22 17:42:18 +00:00
2016-07-15 00:40:30 +02:00
private $routeDescribers;
2016-06-30 23:30:37 +02:00
/**
2018-01-04 11:34:23 +01:00
* @param RouteCollection $routeCollection
* @param ControllerReflector $controllerReflector
* @param RouteDescriberInterface[]|iterable $routeDescribers
2016-06-30 23:30:37 +02:00
*/
2018-01-04 11:34:23 +01:00
public function __construct(RouteCollection $routeCollection, ControllerReflector $controllerReflector, $routeDescribers)
2016-06-30 23:30:37 +02:00
{
$this->routeCollection = $routeCollection;
$this->controllerReflector = $controllerReflector;
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;
}
foreach ($this->routeCollection->all() as $route) {
if (!$route->hasDefault('_controller')) {
continue;
}
2016-06-30 23:30:37 +02:00
// if able to resolve the controller
$controller = $route->getDefault('_controller');
if ($method = $this->controllerReflector->getReflectionMethod($controller)) {
2016-06-30 23:30:37 +02:00
// Extract as many informations as possible about this route
2016-07-15 00:40:30 +02:00
foreach ($this->routeDescribers as $describer) {
2017-01-14 17:36:56 +01:00
if ($describer instanceof ModelRegistryAwareInterface) {
$describer->setModelRegistry($this->modelRegistry);
}
2016-07-28 10:20:59 +02:00
$describer->describe($api, $route, $method);
2016-06-30 23:30:37 +02:00
}
}
}
}
}