Skip to content

Commit

Permalink
Fix Doctrine optional requirement
Browse files Browse the repository at this point in the history
  • Loading branch information
loic425 committed Oct 9, 2024
1 parent d654fe3 commit a0b2c58
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/Bundle/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
20 changes: 19 additions & 1 deletion src/Bundle/DependencyInjection/SyliusGridExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
Expand Down Expand Up @@ -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;
}
}
13 changes: 7 additions & 6 deletions src/Bundle/Maker/MakeGrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;

Check failure on line 96 in src/Bundle/Maker/MakeGrid.php

View workflow job for this annotation

GitHub Actions / PHP 8.3, Symfony ^6.4

TypeDoesNotContainNull

src/Bundle/Maker/MakeGrid.php:96:78: TypeDoesNotContainNull: Doctrine\Persistence\ManagerRegistry does not contain null (see https://psalm.dev/090)

Check failure on line 96 in src/Bundle/Maker/MakeGrid.php

View workflow job for this annotation

GitHub Actions / PHP 8.3, Symfony ^6.4

RedundantCondition

src/Bundle/Maker/MakeGrid.php:96:78: RedundantCondition: Type Doctrine\Persistence\ManagerRegistry for $__tmp_nullsafe__3300 is never null (see https://psalm.dev/122)

Check failure on line 96 in src/Bundle/Maker/MakeGrid.php

View workflow job for this annotation

GitHub Actions / PHP 8.3, Symfony ^6.4

PossiblyNullArgument

src/Bundle/Maker/MakeGrid.php:96:78: PossiblyNullArgument: Argument 1 of ReflectionClass::__construct cannot be null, possibly null value provided (see https://psalm.dev/078)

Check failure on line 96 in src/Bundle/Maker/MakeGrid.php

View workflow job for this annotation

GitHub Actions / PHP 8.1, Symfony ^6.4

TypeDoesNotContainNull

src/Bundle/Maker/MakeGrid.php:96:78: TypeDoesNotContainNull: Doctrine\Persistence\ManagerRegistry does not contain null (see https://psalm.dev/090)

Check failure on line 96 in src/Bundle/Maker/MakeGrid.php

View workflow job for this annotation

GitHub Actions / PHP 8.1, Symfony ^6.4

RedundantCondition

src/Bundle/Maker/MakeGrid.php:96:78: RedundantCondition: Type Doctrine\Persistence\ManagerRegistry for $__tmp_nullsafe__3300 is never null (see https://psalm.dev/122)

Check failure on line 96 in src/Bundle/Maker/MakeGrid.php

View workflow job for this annotation

GitHub Actions / PHP 8.1, Symfony ^6.4

PossiblyNullArgument

src/Bundle/Maker/MakeGrid.php:96:78: PossiblyNullArgument: Argument 1 of ReflectionClass::__construct cannot be null, possibly null value provided (see https://psalm.dev/078)

Check failure on line 96 in src/Bundle/Maker/MakeGrid.php

View workflow job for this annotation

GitHub Actions / PHP 8.2, Symfony ^5.4

TypeDoesNotContainNull

src/Bundle/Maker/MakeGrid.php:96:78: TypeDoesNotContainNull: Doctrine\Persistence\ManagerRegistry does not contain null (see https://psalm.dev/090)

Check failure on line 96 in src/Bundle/Maker/MakeGrid.php

View workflow job for this annotation

GitHub Actions / PHP 8.2, Symfony ^5.4

RedundantCondition

src/Bundle/Maker/MakeGrid.php:96:78: RedundantCondition: Type Doctrine\Persistence\ManagerRegistry for $__tmp_nullsafe__3300 is never null (see https://psalm.dev/122)

Check failure on line 96 in src/Bundle/Maker/MakeGrid.php

View workflow job for this annotation

GitHub Actions / PHP 8.2, Symfony ^5.4

PossiblyNullArgument

src/Bundle/Maker/MakeGrid.php:96:78: PossiblyNullArgument: Argument 1 of ReflectionClass::__construct cannot be null, possibly null value provided (see https://psalm.dev/078)

Check failure on line 96 in src/Bundle/Maker/MakeGrid.php

View workflow job for this annotation

GitHub Actions / PHP 8.2, Symfony ^6.4

TypeDoesNotContainNull

src/Bundle/Maker/MakeGrid.php:96:78: TypeDoesNotContainNull: Doctrine\Persistence\ManagerRegistry does not contain null (see https://psalm.dev/090)

Check failure on line 96 in src/Bundle/Maker/MakeGrid.php

View workflow job for this annotation

GitHub Actions / PHP 8.2, Symfony ^6.4

RedundantCondition

src/Bundle/Maker/MakeGrid.php:96:78: RedundantCondition: Type Doctrine\Persistence\ManagerRegistry for $__tmp_nullsafe__3300 is never null (see https://psalm.dev/122)

Check failure on line 96 in src/Bundle/Maker/MakeGrid.php

View workflow job for this annotation

GitHub Actions / PHP 8.2, Symfony ^6.4

PossiblyNullArgument

src/Bundle/Maker/MakeGrid.php:96:78: PossiblyNullArgument: Argument 1 of ReflectionClass::__construct cannot be null, possibly null value provided (see https://psalm.dev/078)

Check failure on line 96 in src/Bundle/Maker/MakeGrid.php

View workflow job for this annotation

GitHub Actions / PHP 8.3, Symfony ^5.4

TypeDoesNotContainNull

src/Bundle/Maker/MakeGrid.php:96:78: TypeDoesNotContainNull: Doctrine\Persistence\ManagerRegistry does not contain null (see https://psalm.dev/090)

Check failure on line 96 in src/Bundle/Maker/MakeGrid.php

View workflow job for this annotation

GitHub Actions / PHP 8.3, Symfony ^5.4

RedundantCondition

src/Bundle/Maker/MakeGrid.php:96:78: RedundantCondition: Type Doctrine\Persistence\ManagerRegistry for $__tmp_nullsafe__3300 is never null (see https://psalm.dev/122)

Check failure on line 96 in src/Bundle/Maker/MakeGrid.php

View workflow job for this annotation

GitHub Actions / PHP 8.3, Symfony ^5.4

PossiblyNullArgument

src/Bundle/Maker/MakeGrid.php:96:78: PossiblyNullArgument: Argument 1 of ReflectionClass::__construct cannot be null, possibly null value provided (see https://psalm.dev/078)

Check failure on line 96 in src/Bundle/Maker/MakeGrid.php

View workflow job for this annotation

GitHub Actions / PHP 8.3, Symfony ^7.0

TypeDoesNotContainNull

src/Bundle/Maker/MakeGrid.php:96:78: TypeDoesNotContainNull: Doctrine\Persistence\ManagerRegistry does not contain null (see https://psalm.dev/090)

Check failure on line 96 in src/Bundle/Maker/MakeGrid.php

View workflow job for this annotation

GitHub Actions / PHP 8.3, Symfony ^7.0

RedundantCondition

src/Bundle/Maker/MakeGrid.php:96:78: RedundantCondition: Type Doctrine\Persistence\ManagerRegistry for $__tmp_nullsafe__3300 is never null (see https://psalm.dev/122)

Check failure on line 96 in src/Bundle/Maker/MakeGrid.php

View workflow job for this annotation

GitHub Actions / PHP 8.3, Symfony ^7.0

PossiblyNullArgument

src/Bundle/Maker/MakeGrid.php:96:78: PossiblyNullArgument: Argument 1 of ReflectionClass::__construct cannot be null, possibly null value provided (see https://psalm.dev/078)

Check failure on line 96 in src/Bundle/Maker/MakeGrid.php

View workflow job for this annotation

GitHub Actions / PHP 8.1, Symfony ^5.4

TypeDoesNotContainNull

src/Bundle/Maker/MakeGrid.php:96:78: TypeDoesNotContainNull: Doctrine\Persistence\ManagerRegistry does not contain null (see https://psalm.dev/090)

Check failure on line 96 in src/Bundle/Maker/MakeGrid.php

View workflow job for this annotation

GitHub Actions / PHP 8.1, Symfony ^5.4

RedundantCondition

src/Bundle/Maker/MakeGrid.php:96:78: RedundantCondition: Type Doctrine\Persistence\ManagerRegistry for $__tmp_nullsafe__3300 is never null (see https://psalm.dev/122)

Check failure on line 96 in src/Bundle/Maker/MakeGrid.php

View workflow job for this annotation

GitHub Actions / PHP 8.1, Symfony ^5.4

PossiblyNullArgument

src/Bundle/Maker/MakeGrid.php:96:78: PossiblyNullArgument: Argument 1 of ReflectionClass::__construct cannot be null, possibly null value provided (see https://psalm.dev/078)

Check failure on line 96 in src/Bundle/Maker/MakeGrid.php

View workflow job for this annotation

GitHub Actions / PHP 8.1, Symfony ^5.4, PostgresSQL 14.0

TypeDoesNotContainNull

src/Bundle/Maker/MakeGrid.php:96:78: TypeDoesNotContainNull: Doctrine\Persistence\ManagerRegistry does not contain null (see https://psalm.dev/090)

Check failure on line 96 in src/Bundle/Maker/MakeGrid.php

View workflow job for this annotation

GitHub Actions / PHP 8.1, Symfony ^5.4, PostgresSQL 14.0

RedundantCondition

src/Bundle/Maker/MakeGrid.php:96:78: RedundantCondition: Type Doctrine\Persistence\ManagerRegistry for $__tmp_nullsafe__3300 is never null (see https://psalm.dev/122)

Check failure on line 96 in src/Bundle/Maker/MakeGrid.php

View workflow job for this annotation

GitHub Actions / PHP 8.1, Symfony ^5.4, PostgresSQL 14.0

PossiblyNullArgument

src/Bundle/Maker/MakeGrid.php:96:78: PossiblyNullArgument: Argument 1 of ReflectionClass::__construct cannot be null, possibly null value provided (see https://psalm.dev/078)

Check failure on line 96 in src/Bundle/Maker/MakeGrid.php

View workflow job for this annotation

GitHub Actions / PHP 8.1, Symfony ^5.4, PostgresSQL 13.3

TypeDoesNotContainNull

src/Bundle/Maker/MakeGrid.php:96:78: TypeDoesNotContainNull: Doctrine\Persistence\ManagerRegistry does not contain null (see https://psalm.dev/090)

Check failure on line 96 in src/Bundle/Maker/MakeGrid.php

View workflow job for this annotation

GitHub Actions / PHP 8.1, Symfony ^5.4, PostgresSQL 13.3

RedundantCondition

src/Bundle/Maker/MakeGrid.php:96:78: RedundantCondition: Type Doctrine\Persistence\ManagerRegistry for $__tmp_nullsafe__3300 is never null (see https://psalm.dev/122)

Check failure on line 96 in src/Bundle/Maker/MakeGrid.php

View workflow job for this annotation

GitHub Actions / PHP 8.1, Symfony ^5.4, PostgresSQL 13.3

PossiblyNullArgument

src/Bundle/Maker/MakeGrid.php:96:78: PossiblyNullArgument: Argument 1 of ReflectionClass::__construct cannot be null, possibly null value provided (see https://psalm.dev/078)

if (0 !== \mb_strpos($repository->getName(), $generator->getRootNamespace())) {
if (0 !== \mb_strpos($repository?->getName() ?? '', $generator->getRootNamespace())) {
// not using a custom repository
$repository = null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Bundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
</service>

<service id="Sylius\Bundle\GridBundle\Maker\MakeGrid">
<argument type="service" id="doctrine" />
<argument type="service" id="doctrine" on-invalid="null" />
<tag name="maker.command" />
</service>
<service id="sylius.grid.maker" alias="Sylius\Bundle\GridBundle\Maker\MakeGrid" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<service id="sylius.grid_driver.doctrine.phpcrodm" class="Sylius\Bundle\GridBundle\Doctrine\PHPCRODM\Driver">
<argument type="service" id="doctrine_phpcr.odm.document_manager" />
<tag name="sylius.grid_driver" alias="doctrine/phpcr-odm" />
<deprecated>The "%service_id%" service is deprecated since Sylius 1.3. Doctrine MongoDB and PHPCR support will no longer be supported in Sylius 2.0.</deprecated>
<deprecated package="sylius/grid-bundle" version="1.3">The "%service_id%" service is deprecated since Sylius 1.3. Doctrine MongoDB and PHPCR support will no longer be supported in Sylius 2.0.</deprecated>
</service>
</services>
</container>
5 changes: 0 additions & 5 deletions tests/Application/config/sylius/resources.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
section: 'admin',
templatesDir: 'crud',
routePrefix: '/admin',
driver: false,
)]
#[Index(grid: 'app_board_game')]
final class BoardGameResource implements ResourceInterface
Expand Down

0 comments on commit a0b2c58

Please sign in to comment.