Skip to content

Commit 2981552

Browse files
committed
feat(symfony): composer dependency JSON+LD/Hydra is now optional
1 parent 61c6dee commit 2981552

3 files changed

Lines changed: 29 additions & 9 deletions

File tree

src/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,14 @@ private function registerJsonLdHydraConfiguration(ContainerBuilder $container, a
711711
return;
712712
}
713713

714+
if (!InstalledVersions::isInstalled('api-platform/jsonld')) {
715+
throw new \LogicException('JSON+LD support cannot be enabled as the JSON+LD component is not installed. Try running "composer require api-platform/jsonld".');
716+
}
717+
718+
if (!InstalledVersions::isInstalled('api-platform/hydra')) {
719+
throw new \LogicException('JSON+LD support cannot be enabled as the Hydra component is not installed. Try running "composer require api-platform/hydra".');
720+
}
721+
714722
if ($config['use_symfony_listeners']) {
715723
$loader->load('symfony/jsonld.php');
716724
} else {

src/Symfony/Bundle/DependencyInjection/Configuration.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
namespace ApiPlatform\Symfony\Bundle\DependencyInjection;
1515

16+
use Composer\InstalledVersions;
17+
1618
use ApiPlatform\Doctrine\Common\Filter\OrderFilterInterface;
1719
use ApiPlatform\Metadata\ApiResource;
1820
use ApiPlatform\Metadata\Exception\InvalidArgumentException;
@@ -57,6 +59,8 @@ public function getConfigTreeBuilder(): TreeBuilder
5759
$treeBuilder = new TreeBuilder('api_platform');
5860
$rootNode = $treeBuilder->getRootNode();
5961

62+
$jsonLdInstalled = \Composer\InstalledVersions::isInstalled('api-platform/jsonld') && \Composer\InstalledVersions::isInstalled('api-platform/hydra');
63+
6064
$rootNode
6165
->beforeNormalization()
6266
->ifTrue(static function ($v) {
@@ -191,15 +195,17 @@ public function getConfigTreeBuilder(): TreeBuilder
191195

192196
$this->addExceptionToStatusSection($rootNode);
193197

194-
$this->addFormatSection($rootNode, 'formats', [
195-
'jsonld' => ['mime_types' => ['application/ld+json']],
198+
$this->addFormatSection($rootNode, 'formats', $jsonLdInstalled ? [
199+
'jsonld' => ['mime_types' => ['application/ld+json']]
200+
] : [
201+
'json' => ['mime_types' => ['application/json']]
196202
]);
197203
$this->addFormatSection($rootNode, 'patch_formats', [
198204
'json' => ['mime_types' => ['application/merge-patch+json']],
199205
]);
200206

201-
$defaultDocFormats = [
202-
'jsonld' => ['mime_types' => ['application/ld+json']],
207+
$defaultDocFormats = $jsonLdInstalled ? ['jsonld' => ['mime_types' => ['application/ld+json']]] : [];
208+
$defaultDocFormats += [
203209
'jsonopenapi' => ['mime_types' => ['application/vnd.openapi+json']],
204210
'html' => ['mime_types' => ['text/html']],
205211
];
@@ -210,11 +216,19 @@ public function getConfigTreeBuilder(): TreeBuilder
210216

211217
$this->addFormatSection($rootNode, 'docs_formats', $defaultDocFormats);
212218

213-
$this->addFormatSection($rootNode, 'error_formats', [
214-
'jsonld' => ['mime_types' => ['application/ld+json']],
219+
$defaultDocFormats = $jsonLdInstalled ? ['jsonld' => ['mime_types' => ['application/ld+json']]] : [];
220+
$defaultDocFormats += [
221+
'jsonopenapi' => ['mime_types' => ['application/vnd.openapi+json']],
222+
'html' => ['mime_types' => ['text/html']],
223+
];
224+
225+
$defaultErrorFormats = $jsonLdInstalled ? ['jsonld' => ['mime_types' => ['application/ld+json']]] : [];
226+
$defaultErrorFormats += [
215227
'jsonproblem' => ['mime_types' => ['application/problem+json']],
216228
'json' => ['mime_types' => ['application/problem+json', 'application/json']],
217-
]);
229+
];
230+
$this->addFormatSection($rootNode, 'error_formats', $defaultErrorFormats);
231+
218232
$rootNode
219233
->children()
220234
->arrayNode('jsonschema_formats')

src/Symfony/composer.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@
3232
"api-platform/documentation": "^4.3",
3333
"api-platform/http-cache": "^4.3",
3434
"api-platform/json-schema": "^4.3",
35-
"api-platform/jsonld": "^4.3",
36-
"api-platform/hydra": "^4.3",
3735
"api-platform/metadata": "^4.3",
3836
"api-platform/serializer": "^4.3",
3937
"api-platform/state": "^4.3",

0 commit comments

Comments
 (0)