diff --git a/packages/rekapager-api-platform/config/services.php b/packages/rekapager-api-platform/config/services.php index 2ec6848..f9d672d 100644 --- a/packages/rekapager-api-platform/config/services.php +++ b/packages/rekapager-api-platform/config/services.php @@ -21,6 +21,14 @@ use function Symfony\Component\DependencyInjection\Loader\Configurator\service; return static function (ContainerConfigurator $containerConfigurator): void { + $parameters = $containerConfigurator->parameters(); + + $parameters + ->set( + 'rekalogika.rekapager.api_platform.enable_orm_support_by_default', + false + ); + $services = $containerConfigurator->services(); $services->set('rekalogika.rekapager.api_platform.open_api_factory_decorator') @@ -51,6 +59,7 @@ ->args([ '$pagerFactory' => service(PagerFactory::class), '$pagination' => service('api_platform.pagination'), + '$enabledByDefault' => '%rekalogika.rekapager.api_platform.enable_orm_support_by_default%', ]) ->tag('api_platform.doctrine.orm.query_extension.collection', [ 'priority' => -48, diff --git a/packages/rekapager-api-platform/src/DependencyInjection/Configuration.php b/packages/rekapager-api-platform/src/DependencyInjection/Configuration.php new file mode 100644 index 0000000..570a27b --- /dev/null +++ b/packages/rekapager-api-platform/src/DependencyInjection/Configuration.php @@ -0,0 +1,39 @@ + + * + * For the full copyright and license information, please view the LICENSE file + * that was distributed with this source code. + */ + +namespace Rekalogika\Rekapager\ApiPlatform\DependencyInjection; + +use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; +use Symfony\Component\Config\Definition\Builder\TreeBuilder; +use Symfony\Component\Config\Definition\ConfigurationInterface; + +class Configuration implements ConfigurationInterface +{ + public function getConfigTreeBuilder(): TreeBuilder + { + $treeBuilder = new TreeBuilder('rekalogika_rekapager_api_platform'); + + /** @var ArrayNodeDefinition $rootNode */ + $rootNode = $treeBuilder->getRootNode(); + + $rootNode + ->children() + ->booleanNode('enable_orm_support_by_default') + ->defaultValue(false) + ->end() + ->end() + ; + + return $treeBuilder; + } +} diff --git a/packages/rekapager-api-platform/src/DependencyInjection/RekalogikaRekapagerApiPlatformExtension.php b/packages/rekapager-api-platform/src/DependencyInjection/RekalogikaRekapagerApiPlatformExtension.php index 15101c9..d05450a 100644 --- a/packages/rekapager-api-platform/src/DependencyInjection/RekalogikaRekapagerApiPlatformExtension.php +++ b/packages/rekapager-api-platform/src/DependencyInjection/RekalogikaRekapagerApiPlatformExtension.php @@ -23,6 +23,11 @@ class RekalogikaRekapagerApiPlatformExtension extends Extension { public function load(array $configs, ContainerBuilder $container) { + // load configuration + + $configuration = new Configuration(); + $config = $this->processConfiguration($configuration, $configs); + // load our services $loader = new PhpFileLoader( @@ -42,5 +47,16 @@ public function load(array $configs, ContainerBuilder $container) // load services from symfony-bridge package RekapagerSymfonyBridge::loadServices($container); + + // process config + $enableOrmSupportByDefault = $config['enable_orm_support_by_default'] ?? false; + if (!\is_bool($enableOrmSupportByDefault)) { + throw new \InvalidArgumentException('The "enable_orm_support_by_default" option must be a boolean.'); + } + + $container->setParameter( + 'rekalogika.rekapager.api_platform.enable_orm_support_by_default', + $enableOrmSupportByDefault + ); } } diff --git a/packages/rekapager-api-platform/src/Implementation/RekapagerExtension.php b/packages/rekapager-api-platform/src/Implementation/RekapagerExtension.php index 1dfc407..061d74a 100644 --- a/packages/rekapager-api-platform/src/Implementation/RekapagerExtension.php +++ b/packages/rekapager-api-platform/src/Implementation/RekapagerExtension.php @@ -40,7 +40,8 @@ final class RekapagerExtension implements QueryResultCollectionExtensionInterfac { public function __construct( private readonly PagerFactory $pagerFactory, - private readonly Pagination $pagination + private readonly Pagination $pagination, + private readonly bool $enabledByDefault, ) { } @@ -64,6 +65,19 @@ public function supportsResult( ?Operation $operation = null, array $context = [] ): bool { + /** @psalm-suppress InternalMethod */ + $extraProperties = $operation?->getExtraProperties() ?? []; + /** @var bool|null */ + $isEnabled = $extraProperties['rekapager_orm_enabled'] ?? null; + + if (!\is_bool($isEnabled) && $isEnabled === null) { + $isEnabled = $this->enabledByDefault; + } + + if ($isEnabled === false) { + return false; + } + if ((bool) ($context['graphql_operation_name'] ?? false)) { return $this->pagination->isGraphQlEnabled($operation, $context); } diff --git a/psalm.xml b/psalm.xml index cdca033..ce00552 100644 --- a/psalm.xml +++ b/psalm.xml @@ -54,16 +54,19 @@ + + + diff --git a/tests/config/packages/rekalogika_rekapager_api_platform.yaml b/tests/config/packages/rekalogika_rekapager_api_platform.yaml new file mode 100644 index 0000000..ce5622b --- /dev/null +++ b/tests/config/packages/rekalogika_rekapager_api_platform.yaml @@ -0,0 +1,2 @@ +rekalogika_rekapager_api_platform: + enable_orm_support_by_default: false \ No newline at end of file diff --git a/tests/src/App/Entity/Post.php b/tests/src/App/Entity/Post.php index f4811a6..eeb64a9 100644 --- a/tests/src/App/Entity/Post.php +++ b/tests/src/App/Entity/Post.php @@ -26,7 +26,7 @@ #[ORM\Index(fields: ['date'])] #[ORM\Index(fields: ['title'])] #[ORM\Index(fields: ['setName'])] -#[ApiResource()] +#[ApiResource(extraProperties: ['rekapager_orm_enabled' => true])] #[ApiResource( operations: [ new GetCollection(