Skip to content

Commit 8171c0c

Browse files
author
Alexis Thinardon
committed
Ability to disable entrypoint and /api/docs
1 parent d2a6729 commit 8171c0c

File tree

9 files changed

+40
-8
lines changed

9 files changed

+40
-8
lines changed

src/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ public function load(array $configs, ContainerBuilder $container)
143143
*/
144144
private function handleConfig(ContainerBuilder $container, array $config, array $formats, array $errorFormats)
145145
{
146+
$container->setParameter('api_platform.enable_entrypoint', $config['enable_entrypoint']);
147+
$container->setParameter('api_platform.enable_docs', $config['enable_docs']);
146148
$container->setParameter('api_platform.title', $config['title']);
147149
$container->setParameter('api_platform.description', $config['description']);
148150
$container->setParameter('api_platform.version', $config['version']);

src/Bridge/Symfony/Bundle/DependencyInjection/Configuration.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ public function getConfigTreeBuilder()
9999
->end()
100100
->booleanNode('enable_swagger')->defaultValue(true)->info('Enable the Swagger documentation and export.')->end()
101101
->booleanNode('enable_swagger_ui')->defaultValue(class_exists(TwigBundle::class))->info('Enable Swagger ui.')->end()
102+
->booleanNode('enable_entrypoint')->defaultTrue()->info('Enable the entrypoint')->end()
103+
->booleanNode('enable_docs')->defaultTrue()->info('Enable the docs')->end()
102104

103105
->arrayNode('oauth')
104106
->canBeEnabled()

src/Bridge/Symfony/Bundle/Resources/config/api.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
<argument>%api_platform.resource_class_directories%</argument>
3939
<argument type="service" id="api_platform.subresource_operation_factory" />
4040
<argument>%api_platform.graphql.enabled%</argument>
41+
<argument>%api_platform.enable_entrypoint%</argument>
42+
<argument>%api_platform.enable_docs%</argument>
4143

4244
<tag name="routing.loader" />
4345
</service>

src/Bridge/Symfony/Bundle/Resources/config/routing/api.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,4 @@
1313
<requirement key="index">index</requirement>
1414
</route>
1515

16-
<route id="api_doc" path="/docs.{_format}">
17-
<default key="_controller">api_platform.action.documentation</default>
18-
<default key="_api_respond">1</default>
19-
<default key="_format" />
20-
</route>
21-
2216
</routes>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
3+
<routes xmlns="http://symfony.com/schema/routing"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://symfony.com/schema/routing
6+
http://symfony.com/schema/routing/routing-1.0.xsd">
7+
8+
<route id="api_doc" path="/docs.{_format}">
9+
<default key="_controller">api_platform.action.documentation</default>
10+
<default key="_api_respond">1</default>
11+
<default key="_format" />
12+
</route>
13+
14+
</routes>

src/Bridge/Symfony/Routing/ApiLoader.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@ final class ApiLoader extends Loader
5151
private $resourceClassDirectories;
5252
private $subresourceOperationFactory;
5353
private $graphqlEnabled;
54+
private $entrypointEnabled;
55+
private $docsEnabled;
5456

55-
public function __construct(KernelInterface $kernel, ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, ResourceMetadataFactoryInterface $resourceMetadataFactory, OperationPathResolverInterface $operationPathResolver, ContainerInterface $container, array $formats, array $resourceClassDirectories = [], SubresourceOperationFactoryInterface $subresourceOperationFactory = null, bool $graphqlEnabled = false)
57+
public function __construct(KernelInterface $kernel, ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, ResourceMetadataFactoryInterface $resourceMetadataFactory, OperationPathResolverInterface $operationPathResolver, ContainerInterface $container, array $formats, array $resourceClassDirectories = [], SubresourceOperationFactoryInterface $subresourceOperationFactory = null, bool $graphqlEnabled = false, bool $entrypointEnabled = true, bool $docsEnabled = true)
5658
{
5759
$this->fileLoader = new XmlFileLoader(new FileLocator($kernel->locateResource('@ApiPlatformBundle/Resources/config/routing')));
5860
$this->resourceNameCollectionFactory = $resourceNameCollectionFactory;
@@ -63,6 +65,8 @@ public function __construct(KernelInterface $kernel, ResourceNameCollectionFacto
6365
$this->resourceClassDirectories = $resourceClassDirectories;
6466
$this->subresourceOperationFactory = $subresourceOperationFactory;
6567
$this->graphqlEnabled = $graphqlEnabled;
68+
$this->entrypointEnabled = $entrypointEnabled;
69+
$this->docsEnabled = $docsEnabled;
6670
}
6771

6872
/**
@@ -146,6 +150,14 @@ private function loadExternalFiles(RouteCollection $routeCollection)
146150
{
147151
$routeCollection->addCollection($this->fileLoader->load('api.xml'));
148152

153+
if ($this->entrypointEnabled) {
154+
$routeCollection->addCollection($this->fileLoader->load('api.xml'));
155+
}
156+
157+
if ($this->docsEnabled) {
158+
$routeCollection->addCollection($this->fileLoader->load('docs.xml'));
159+
}
160+
149161
if ($this->graphqlEnabled) {
150162
$graphqlCollection = $this->fileLoader->load('graphql.xml');
151163
$graphqlCollection->addDefaults(['_graphql' => true]);

tests/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtensionTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,8 @@ private function getPartialContainerBuilderProphecy($test = false)
396396
'api_platform.http_cache.shared_max_age' => null,
397397
'api_platform.http_cache.vary' => ['Accept'],
398398
'api_platform.http_cache.public' => null,
399+
'api_platform.enable_entrypoint' => true,
400+
'api_platform.enable_docs' => true,
399401
];
400402

401403
foreach ($parameters as $key => $value) {

tests/Bridge/Symfony/Bundle/DependencyInjection/ConfigurationTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ public function testDefaultConfig()
7878
'enable_nelmio_api_doc' => false,
7979
'enable_swagger' => true,
8080
'enable_swagger_ui' => true,
81+
'enable_entrypoint' => true,
82+
'enable_docs' => true,
8183
'graphql' => [
8284
'enabled' => true,
8385
'graphiql' => [

tests/Bridge/Symfony/Routing/ApiLoaderTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@ private function getApiLoaderWithResourceMetadata(ResourceMetadata $resourceMeta
210210
$containerProphecy->has($possibleArgument)->willReturn(true);
211211
}
212212
$containerProphecy->getParameter('api_platform.enable_swagger')->willReturn(true);
213+
$containerProphecy->getParameter('api_platform.enable_entrypoint')->willReturn(true);
214+
$containerProphecy->getParameter('api_platform.enable_docs')->willReturn(true);
213215

214216
$containerProphecy->has(Argument::type('string'))->willReturn(false);
215217

@@ -257,7 +259,7 @@ private function getApiLoaderWithResourceMetadata(ResourceMetadata $resourceMeta
257259

258260
$subresourceOperationFactory = new SubresourceOperationFactory($resourceMetadataFactory, $propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), new UnderscorePathSegmentNameGenerator());
259261

260-
$apiLoader = new ApiLoader($kernelProphecy->reveal(), $resourceNameCollectionFactoryProphecy->reveal(), $resourceMetadataFactory, $operationPathResolver, $containerProphecy->reveal(), ['jsonld' => ['application/ld+json']], [], $subresourceOperationFactory, false);
262+
$apiLoader = new ApiLoader($kernelProphecy->reveal(), $resourceNameCollectionFactoryProphecy->reveal(), $resourceMetadataFactory, $operationPathResolver, $containerProphecy->reveal(), ['jsonld' => ['application/ld+json']], [], $subresourceOperationFactory, false, true, true);
261263

262264
return $apiLoader;
263265
}

0 commit comments

Comments
 (0)