NelmioApiDocBundle/Describer/ApiPlatformDescriber.php

47 lines
1.5 KiB
PHP
Raw Normal View History

2016-07-29 10:22:40 +02:00
<?php
/*
2016-12-29 12:09:26 +01:00
* This file is part of the NelmioApiDocBundle package.
2016-07-29 10:22:40 +02:00
*
2016-12-29 12:09:26 +01:00
* (c) Nelmio
2016-07-29 10:22:40 +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-07-29 10:22:40 +02:00
use ApiPlatform\Core\Documentation\Documentation;
2016-11-18 22:16:53 +01:00
use ApiPlatform\Core\Swagger\Serializer\DocumentationNormalizer;
2017-07-05 15:41:53 +02:00
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RequestContext;
2016-07-29 10:22:40 +02:00
2016-11-30 15:04:53 +01:00
final class ApiPlatformDescriber extends ExternalDocDescriber
2016-07-29 10:22:40 +02:00
{
2017-07-05 15:41:53 +02:00
public function __construct(Documentation $documentation, DocumentationNormalizer $normalizer, UrlGeneratorInterface $urlGenerator)
2016-07-29 10:22:40 +02:00
{
2017-07-05 15:41:53 +02:00
parent::__construct(function () use ($documentation, $normalizer, $urlGenerator) {
$paths = [];
2017-07-05 15:41:53 +02:00
$baseContext = $urlGenerator->getContext();
$urlGenerator->setContext(new RequestContext());
2017-12-22 17:42:18 +00:00
2017-07-05 15:41:53 +02:00
try {
$basePath = $urlGenerator->generate('api_entrypoint');
} finally {
$urlGenerator->setContext($baseContext);
}
2017-01-18 18:02:00 +01:00
$documentation = (array) $normalizer->normalize($documentation);
2017-07-05 15:41:53 +02:00
unset($documentation['basePath']);
foreach ($documentation['paths'] as $path => $value) {
$paths['/'.ltrim($basePath.'/'.ltrim($path, '/'), '/')] = $value;
2017-01-18 18:02:00 +01:00
}
2017-07-05 15:41:53 +02:00
$documentation['paths'] = $paths;
2017-01-18 18:02:00 +01:00
return $documentation;
2017-07-05 15:41:53 +02:00
});
2016-07-29 10:22:40 +02:00
}
}