diff --git a/src/Bundle/DependencyInjection/Configuration.php b/src/Bundle/DependencyInjection/Configuration.php index 02d4c41a..5bdf643c 100644 --- a/src/Bundle/DependencyInjection/Configuration.php +++ b/src/Bundle/DependencyInjection/Configuration.php @@ -40,7 +40,7 @@ private function addDriversSection(ArrayNodeDefinition $node): void $node ->children() ->arrayNode('drivers') - ->defaultValue([SyliusGridBundle::DRIVER_DOCTRINE_ORM]) + ->defaultValue([]) ->enumPrototype()->values(SyliusGridBundle::getAvailableDrivers())->end() ->end() ->end() diff --git a/src/Bundle/DependencyInjection/SyliusGridExtension.php b/src/Bundle/DependencyInjection/SyliusGridExtension.php index 493e1dab..fc591b7b 100644 --- a/src/Bundle/DependencyInjection/SyliusGridExtension.php +++ b/src/Bundle/DependencyInjection/SyliusGridExtension.php @@ -41,7 +41,14 @@ public function load(array $configs, ContainerBuilder $container): void $container->setAlias('sylius.grid.bulk_action_renderer', 'sylius.grid.bulk_action_renderer.twig'); $container->setAlias('sylius.grid.data_extractor', 'sylius.grid.data_extractor.property_access'); - foreach ($config['drivers'] as $enabledDriver) { + $availableDrivers = $this->getAvailableDrivers($container); + + $drivers = $config['drivers']; + + // Enable all available drivers if there is no configured drivers + $drivers = [] !== $drivers ? $drivers : $availableDrivers; + + foreach ($drivers as $enabledDriver) { if ($enabledDriver === SyliusGridBundle::DRIVER_DOCTRINE_PHPCR_ODM) { @trigger_error(sprintf( 'The "%s" driver is deprecated in Sylius 1.3. Doctrine PHPCR will no longer be supported in Sylius 2.0.', @@ -77,4 +84,15 @@ public function getConfiguration(array $config, ContainerBuilder $container): Co return $configuration; } + + private function getAvailableDrivers(ContainerBuilder $container): array + { + $availableDrivers = []; + + if ($container::willBeAvailable(SyliusGridBundle::DRIVER_DOCTRINE_ORM, \Doctrine\ORM\EntityManagerInterface::class, ['doctrine/doctrine-bundle'])) { + $availableDrivers[] = SyliusGridBundle::DRIVER_DOCTRINE_ORM; + } + + return $availableDrivers; + } } diff --git a/src/Bundle/Maker/MakeGrid.php b/src/Bundle/Maker/MakeGrid.php index b8939d42..6afc2623 100644 --- a/src/Bundle/Maker/MakeGrid.php +++ b/src/Bundle/Maker/MakeGrid.php @@ -28,11 +28,8 @@ final class MakeGrid extends AbstractMaker { - private ManagerRegistry $managerRegistry; - - public function __construct(ManagerRegistry $managerRegistry) + public function __construct(private ?ManagerRegistry $managerRegistry = null) { - $this->managerRegistry = $managerRegistry; } public static function getCommandName(): string @@ -58,6 +55,10 @@ public function configureCommand(Command $command, InputConfiguration $inputConf public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void { + if (null === $this->managerRegistry) { + throw new \LogicException('Doctrine is not available. Try running "composer require doctrine/doctrine-bundle".'); + } + if ($input->getArgument('entity')) { return; } @@ -92,9 +93,9 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen $entity = new \ReflectionClass($class); $grid = $generator->createClassNameDetails($entity->getShortName(), $namespace, 'Grid'); - $repository = new \ReflectionClass($this->managerRegistry->getRepository($entity->getName())); + $repository = null !== $this->managerRegistry ? new \ReflectionClass($this->managerRegistry?->getRepository($entity->getName())) : null; - if (0 !== \mb_strpos($repository->getName(), $generator->getRootNamespace())) { + if (0 !== \mb_strpos($repository?->getName() ?? '', $generator->getRootNamespace())) { // not using a custom repository $repository = null; } diff --git a/src/Bundle/Resources/config/services.xml b/src/Bundle/Resources/config/services.xml index b281b1f3..4562f14d 100644 --- a/src/Bundle/Resources/config/services.xml +++ b/src/Bundle/Resources/config/services.xml @@ -124,7 +124,7 @@ - + diff --git a/src/Bundle/Resources/config/services/integrations/doctrine/phpcr-odm.xml b/src/Bundle/Resources/config/services/integrations/doctrine/phpcr-odm.xml index 4605dc9a..98b25bc7 100644 --- a/src/Bundle/Resources/config/services/integrations/doctrine/phpcr-odm.xml +++ b/src/Bundle/Resources/config/services/integrations/doctrine/phpcr-odm.xml @@ -18,7 +18,7 @@ - The "%service_id%" service is deprecated since Sylius 1.3. Doctrine MongoDB and PHPCR support will no longer be supported in Sylius 2.0. + The "%service_id%" service is deprecated since Sylius 1.3. Doctrine MongoDB and PHPCR support will no longer be supported in Sylius 2.0. diff --git a/tests/Application/config/sylius/resources.yaml b/tests/Application/config/sylius/resources.yaml index 95ff5f66..97e44b85 100644 --- a/tests/Application/config/sylius/resources.yaml +++ b/tests/Application/config/sylius/resources.yaml @@ -4,11 +4,6 @@ sylius_resource: - '%kernel.project_dir%/src/BoardGameBlog/Infrastructure/Sylius/Resource' resources: - app.board_game: - driver: false - classes: - model: App\BoardGameBlog\Infrastructure\Sylius\Resource\BoardGameResource - app.book: classes: model: App\Entity\Book diff --git a/tests/Application/src/BoardGameBlog/Infrastructure/Sylius/Resource/BoardGameResource.php b/tests/Application/src/BoardGameBlog/Infrastructure/Sylius/Resource/BoardGameResource.php index af4a6065..b6a70fbf 100644 --- a/tests/Application/src/BoardGameBlog/Infrastructure/Sylius/Resource/BoardGameResource.php +++ b/tests/Application/src/BoardGameBlog/Infrastructure/Sylius/Resource/BoardGameResource.php @@ -22,6 +22,7 @@ section: 'admin', templatesDir: 'crud', routePrefix: '/admin', + driver: false, )] #[Index(grid: 'app_board_game')] final class BoardGameResource implements ResourceInterface