|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the API Platform project. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Kévin Dunglas <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace ApiPlatform\Core\Bridge\Symfony\Bundle\Action; |
|
13
|
|
|
|
|
14
|
|
|
use ApiPlatform\Core\Documentation\Documentation; |
|
15
|
|
|
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; |
|
16
|
|
|
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface; |
|
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
18
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
19
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Displays the documentation in Swagger UI. |
|
23
|
|
|
* |
|
24
|
|
|
* @author Kévin Dunglas <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
final class SwaggerUiAction |
|
27
|
|
|
{ |
|
28
|
|
|
private $resourceNameCollectionFactory; |
|
29
|
|
|
private $resourceMetadataFactory; |
|
30
|
|
|
private $serializer; |
|
31
|
|
|
private $twig; |
|
32
|
|
|
private $title; |
|
33
|
|
|
private $description; |
|
34
|
|
|
private $version; |
|
35
|
|
|
private $formats = []; |
|
36
|
|
|
|
|
37
|
|
|
public function __construct(ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, ResourceMetadataFactoryInterface $resourceMetadataFactory, SerializerInterface $serializer, \Twig_Environment $twig, string $title = '', string $description = '', string $version = '', array $formats = []) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->resourceNameCollectionFactory = $resourceNameCollectionFactory; |
|
40
|
|
|
$this->resourceMetadataFactory = $resourceMetadataFactory; |
|
41
|
|
|
$this->serializer = $serializer; |
|
42
|
|
|
$this->twig = $twig; |
|
43
|
|
|
$this->title = $title; |
|
44
|
|
|
$this->description = $description; |
|
45
|
|
|
$this->version = $version; |
|
46
|
|
|
$this->formats = $formats; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function __invoke(Request $request) |
|
50
|
|
|
{ |
|
51
|
|
|
$documentation = new Documentation($this->resourceNameCollectionFactory->create(), $this->title, $this->description, $this->version, $this->formats); |
|
52
|
|
|
|
|
53
|
|
|
return new Response($this->twig->render( |
|
54
|
|
|
'ApiPlatformBundle:SwaggerUi:index.html.twig', |
|
55
|
|
|
$this->getContext($request) + ['spec' => $this->serializer->serialize($documentation, 'json')]) |
|
56
|
|
|
); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Gets the base Twig context. |
|
61
|
|
|
* |
|
62
|
|
|
* @param Request $request |
|
63
|
|
|
* |
|
64
|
|
|
* @return array |
|
65
|
|
|
*/ |
|
66
|
|
|
private function getContext(Request $request) : array |
|
67
|
|
|
{ |
|
68
|
|
|
$context = [ |
|
69
|
|
|
'title' => $this->title, |
|
70
|
|
|
'description' => $this->description, |
|
71
|
|
|
'formats' => $this->formats, |
|
72
|
|
|
'shortName' => null, |
|
73
|
|
|
'operationId' => null, |
|
74
|
|
|
]; |
|
75
|
|
|
|
|
76
|
|
|
if (!$request->isMethodSafe() || null === $resourceClass = $request->attributes->get('_api_resource_class')) { |
|
77
|
|
|
return $context; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$metadata = $this->resourceMetadataFactory->create($resourceClass); |
|
81
|
|
|
$context['shortName'] = $metadata->getShortName(); |
|
82
|
|
|
|
|
83
|
|
|
if (null !== $collectionOperationName = $request->attributes->get('_api_collection_operation_name')) { |
|
84
|
|
|
$context['operationId'] = sprintf('%s%sCollection', $collectionOperationName, $context['shortName']); |
|
85
|
|
|
} elseif (null !== $itemOperationName = $request->attributes->get('_api_item_operation_name')) { |
|
86
|
|
|
$context['operationId'] = sprintf('%s%sItem', $itemOperationName, $context['shortName']); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return $context; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|