Skip to content

Commit 5d7fb4d

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

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

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: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use ApiPlatform\Metadata\Post;
2121
use ApiPlatform\Metadata\Put;
2222
use ApiPlatform\Symfony\Controller\MainController;
23+
use Composer\InstalledVersions;
2324
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
2425
use Doctrine\Bundle\MongoDBBundle\DoctrineMongoDBBundle;
2526
use Doctrine\ORM\EntityManagerInterface;
@@ -57,6 +58,8 @@ public function getConfigTreeBuilder(): TreeBuilder
5758
$treeBuilder = new TreeBuilder('api_platform');
5859
$rootNode = $treeBuilder->getRootNode();
5960

61+
$jsonLdInstalled = InstalledVersions::isInstalled('api-platform/jsonld') && InstalledVersions::isInstalled('api-platform/hydra');
62+
6063
$rootNode
6164
->beforeNormalization()
6265
->ifTrue(static function ($v) {
@@ -191,15 +194,17 @@ public function getConfigTreeBuilder(): TreeBuilder
191194

192195
$this->addExceptionToStatusSection($rootNode);
193196

194-
$this->addFormatSection($rootNode, 'formats', [
195-
'jsonld' => ['mime_types' => ['application/ld+json']],
197+
$this->addFormatSection($rootNode, 'formats', $jsonLdInstalled ? [
198+
'jsonld' => ['mime_types' => ['application/ld+json']]
199+
] : [
200+
'json' => ['mime_types' => ['application/json']]
196201
]);
197202
$this->addFormatSection($rootNode, 'patch_formats', [
198203
'json' => ['mime_types' => ['application/merge-patch+json']],
199204
]);
200205

201-
$defaultDocFormats = [
202-
'jsonld' => ['mime_types' => ['application/ld+json']],
206+
$defaultDocFormats = $jsonLdInstalled ? ['jsonld' => ['mime_types' => ['application/ld+json']]] : [];
207+
$defaultDocFormats += [
203208
'jsonopenapi' => ['mime_types' => ['application/vnd.openapi+json']],
204209
'html' => ['mime_types' => ['text/html']],
205210
];
@@ -210,11 +215,19 @@ public function getConfigTreeBuilder(): TreeBuilder
210215

211216
$this->addFormatSection($rootNode, 'docs_formats', $defaultDocFormats);
212217

213-
$this->addFormatSection($rootNode, 'error_formats', [
214-
'jsonld' => ['mime_types' => ['application/ld+json']],
218+
$defaultDocFormats = $jsonLdInstalled ? ['jsonld' => ['mime_types' => ['application/ld+json']]] : [];
219+
$defaultDocFormats += [
220+
'jsonopenapi' => ['mime_types' => ['application/vnd.openapi+json']],
221+
'html' => ['mime_types' => ['text/html']],
222+
];
223+
224+
$defaultErrorFormats = $jsonLdInstalled ? ['jsonld' => ['mime_types' => ['application/ld+json']]] : [];
225+
$defaultErrorFormats += [
215226
'jsonproblem' => ['mime_types' => ['application/problem+json']],
216227
'json' => ['mime_types' => ['application/problem+json', 'application/json']],
217-
]);
228+
];
229+
$this->addFormatSection($rootNode, 'error_formats', $defaultErrorFormats);
230+
218231
$rootNode
219232
->children()
220233
->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)