Skip to content

Commit

Permalink
disable orm support by default, add config to enable it
Browse files Browse the repository at this point in the history
  • Loading branch information
priyadi committed Apr 8, 2024
1 parent 8f87284 commit 1c9cedb
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 2 deletions.
9 changes: 9 additions & 0 deletions packages/rekapager-api-platform/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

/*
* This file is part of rekalogika/rekapager package.
*
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev>
*
* 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {
}

Expand All @@ -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);
}
Expand Down
3 changes: 3 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,19 @@
<UndefinedInterfaceMethod>
<errorLevel type="suppress">
<file name="packages/rekapager-bundle/src/DependencyInjection/Configuration.php" />
<file name="packages/rekapager-api-platform/src/DependencyInjection/Configuration.php" />
</errorLevel>
</UndefinedInterfaceMethod>
<MixedMethodCall>
<errorLevel type="suppress">
<file name="packages/rekapager-bundle/src/DependencyInjection/Configuration.php" />
<file name="packages/rekapager-api-platform/src/DependencyInjection/Configuration.php" />
</errorLevel>
</MixedMethodCall>
<PossiblyNullReference>
<errorLevel type="suppress">
<file name="packages/rekapager-bundle/src/DependencyInjection/Configuration.php" />
<file name="packages/rekapager-api-platform/src/DependencyInjection/Configuration.php" />
</errorLevel>
</PossiblyNullReference>
<InternalClass>
Expand Down
2 changes: 2 additions & 0 deletions tests/config/packages/rekalogika_rekapager_api_platform.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rekalogika_rekapager_api_platform:
enable_orm_support_by_default: false
2 changes: 1 addition & 1 deletion tests/src/App/Entity/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 1c9cedb

Please sign in to comment.