Skip to content

Commit 2a34498

Browse files
authored
fix(symfony): check that required package are installed before configuring services (#7607)
1 parent c099892 commit 2a34498

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
"api-platform/http-cache": "self.version",
9393
"api-platform/hydra": "self.version",
9494
"api-platform/json-api": "self.version",
95-
"api-platform/json-hal": "self.version",
95+
"api-platform/hal": "self.version",
9696
"api-platform/json-schema": "self.version",
9797
"api-platform/jsonld": "self.version",
9898
"api-platform/laravel": "self.version",
@@ -146,7 +146,7 @@
146146
"illuminate/routing": "^11.0 || ^12.0",
147147
"illuminate/support": "^11.0 || ^12.0",
148148
"jangregor/phpstan-prophecy": "^2.1.11",
149-
"justinrainbow/json-schema": "^5.2.11",
149+
"justinrainbow/json-schema": "5.3.0",
150150
"laravel/framework": "^11.0 || ^12.0",
151151
"orchestra/testbench": "^9.1",
152152
"phpspec/prophecy-phpunit": "^2.2",

src/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
use ApiPlatform\Symfony\Validator\Metadata\Property\Restriction\PropertySchemaRestrictionMetadataInterface;
4848
use ApiPlatform\Symfony\Validator\ValidationGroupsGeneratorInterface;
4949
use ApiPlatform\Validator\Exception\ValidationException;
50+
use Composer\InstalledVersions;
5051
use Doctrine\Persistence\ManagerRegistry;
5152
use PHPStan\PhpDocParser\Parser\PhpDocParser;
5253
use Ramsey\Uuid\Uuid;
@@ -635,6 +636,10 @@ private function registerJsonApiConfiguration(array $formats, PhpFileLoader $loa
635636
return;
636637
}
637638

639+
if (!InstalledVersions::isInstalled('api-platform/json-api')) {
640+
throw new \LogicException('JSON-API support cannot be enabled as the JSON-API component is not installed. Try running "composer require api-platform/json-api".');
641+
}
642+
638643
$loader->load('jsonapi.php');
639644
$loader->load('state/jsonapi.php');
640645
}
@@ -666,6 +671,10 @@ private function registerJsonHalConfiguration(array $formats, PhpFileLoader $loa
666671
return;
667672
}
668673

674+
if (!InstalledVersions::isInstalled('api-platform/hal')) {
675+
throw new \LogicException('HAL support cannot be enabled as the HAL component is not installed. Try running "composer require api-platform/hal".');
676+
}
677+
669678
$loader->load('hal.php');
670679
}
671680

@@ -738,6 +747,10 @@ private function registerDoctrineOrmConfiguration(ContainerBuilder $container, a
738747
return;
739748
}
740749

750+
if (!InstalledVersions::isInstalled('api-platform/doctrine-orm')) {
751+
throw new \LogicException('Doctrine support cannot be enabled as the doctrine ORM component is not installed. Try running "composer require api-platform/doctrine-orm".');
752+
}
753+
741754
// For older versions of doctrine bridge this allows autoconfiguration for filters
742755
if (!$container->has(ManagerRegistry::class)) {
743756
$container->setAlias(ManagerRegistry::class, 'doctrine');
@@ -770,6 +783,10 @@ private function registerDoctrineMongoDbOdmConfiguration(ContainerBuilder $conta
770783
return;
771784
}
772785

786+
if (!InstalledVersions::isInstalled('api-platform/doctrine-odm')) {
787+
throw new \LogicException('Doctrine MongoDB ODM support cannot be enabled as the doctrine ODM component is not installed. Try running "composer require api-platform/doctrine-odm".');
788+
}
789+
773790
$container->registerForAutoconfiguration(AggregationItemExtensionInterface::class)
774791
->addTag('api_platform.doctrine_mongodb.odm.aggregation_extension.item');
775792
$container->registerForAutoconfiguration(AggregationCollectionExtensionInterface::class)
@@ -879,6 +896,10 @@ private function registerMercureConfiguration(ContainerBuilder $container, array
879896
return;
880897
}
881898

899+
if (!InstalledVersions::isInstalled('symfony/mercure-bundle')) {
900+
throw new \LogicException('Mercure support cannot be enabled as the Symfony Mercure Bundle is not installed. Try running "composer require symfony/mercure-bundle".');
901+
}
902+
882903
$container->setParameter('api_platform.mercure.include_type', $config['mercure']['include_type']);
883904
$loader->load('state/mercure.php');
884905

@@ -900,6 +921,10 @@ private function registerMessengerConfiguration(ContainerBuilder $container, arr
900921
return;
901922
}
902923

924+
if (!InstalledVersions::isInstalled('symfony/messenger')) {
925+
throw new \LogicException('Messenger support cannot be enabled as the Symfony Messenger component is not installed. Try running "composer require symfony/messenger".');
926+
}
927+
903928
$loader->load('messenger.php');
904929
}
905930

@@ -913,6 +938,10 @@ private function registerElasticsearchConfiguration(ContainerBuilder $container,
913938
return;
914939
}
915940

941+
if (!InstalledVersions::isInstalled('api-platform/elasticsearch')) {
942+
throw new \LogicException('Elasticsearch support cannot be enabled as the Elasticsearch component is not installed. Try running "composer require api-platform/elasticsearch".');
943+
}
944+
916945
$clientClass = !class_exists(\Elasticsearch\Client::class)
917946
// ES v7
918947
? \Elastic\Elasticsearch\Client::class

src/Symfony/composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"api-platform/doctrine-orm": "^4.2.3",
5454
"api-platform/elasticsearch": "^4.2.3",
5555
"api-platform/graphql": "^4.2.3",
56+
"api-platform/hal": "^4.2.3",
5657
"phpspec/prophecy-phpunit": "^2.2",
5758
"phpunit/phpunit": "11.5.x-dev",
5859
"symfony/expression-language": "^6.4 || ^7.0 || ^8.0",
@@ -70,6 +71,7 @@
7071
"api-platform/elasticsearch": "To support Elasticsearch.",
7172
"api-platform/graphql": "To support GraphQL.",
7273
"api-platform/hal": "to support the HAL format",
74+
"api-platform/json-api": "to support the JSON-API format",
7375
"api-platform/ramsey-uuid": "To support Ramsey's UUID identifiers.",
7476
"phpstan/phpdoc-parser": "To support extracting metadata from PHPDoc.",
7577
"psr/cache-implementation": "To use metadata caching.",

0 commit comments

Comments
 (0)