NelmioApiDocBundle/RouteDescriber/RouteMetadataDescriber.php

49 lines
1.4 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\RouteDescriber;
2016-06-30 23:30:37 +02:00
2016-08-01 19:58:57 +02:00
use EXSyst\Component\Swagger\Swagger;
2016-06-30 23:30:37 +02:00
use Symfony\Component\Routing\Route;
2016-11-30 15:04:53 +01:00
final class RouteMetadataDescriber implements RouteDescriberInterface
2016-06-30 23:30:37 +02:00
{
2016-07-15 00:40:30 +02:00
use RouteDescriberTrait;
2016-06-30 23:30:37 +02:00
2016-07-15 00:40:30 +02:00
public function describe(Swagger $api, Route $route, \ReflectionMethod $reflectionMethod)
2016-06-30 23:30:37 +02:00
{
foreach ($this->getOperations($api, $route) as $operation) {
2016-07-29 18:40:56 +02:00
$operation->merge(['schemes' => $route->getSchemes()]);
2016-06-30 23:30:37 +02:00
2016-08-04 22:27:10 +02:00
$requirements = $route->getRequirements();
$compiledRoute = $route->compile();
2016-12-01 17:19:33 +01:00
// Don't include host requirements
2016-08-04 22:27:10 +02:00
foreach ($compiledRoute->getPathVariables() as $pathVariable) {
2016-12-01 17:19:33 +01:00
if ('_format' === $pathVariable) {
continue;
}
2016-08-04 22:27:10 +02:00
$parameter = $operation->getParameters()->get($pathVariable, 'path');
$parameter->setRequired(true);
2017-07-03 23:46:15 +02:00
if (null === $parameter->getType()) {
$parameter->setType('string');
}
2016-08-04 22:27:10 +02:00
if (isset($requirements[$pathVariable])) {
$parameter->setPattern($requirements[$pathVariable]);
2016-08-04 22:27:10 +02:00
}
2016-06-30 23:30:37 +02:00
}
}
}
}