Skip to content

Commit

Permalink
Register the filter in the registry
Browse files Browse the repository at this point in the history
  • Loading branch information
loic425 committed Dec 18, 2024
1 parent 6eaafe6 commit bf029b4
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 17 deletions.
46 changes: 35 additions & 11 deletions src/Bundle/DependencyInjection/Compiler/RegisterFiltersPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
namespace Sylius\Bundle\GridBundle\DependencyInjection\Compiler;

use InvalidArgumentException;
use Sylius\Component\Grid\Attribute\AsFilter;
use Sylius\Component\Grid\Filtering\FormTypeAwareFilterInterface;
use Sylius\Component\Grid\Filtering\TypeAwareFilterInterface;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

final class RegisterFiltersPass implements CompilerPassInterface
Expand All @@ -28,10 +30,21 @@ public function process(ContainerBuilder $container): void
return;
}

$registry = $container->getDefinition('sylius.registry.grid_filter');
$filterRegistry = $container->getDefinition('sylius.registry.grid_filter');
$formTypeRegistry = $container->getDefinition('sylius.form_registry.grid_filter');

foreach ($container->findTaggedServiceIds('sylius.grid_filter') as $id => $attributes) {
foreach ($container->findTaggedServiceIds(AsFilter::SERVICE_TAG) as $id => $attributes) {
$this->registerFilter($filterRegistry, $formTypeRegistry, $id, $attributes);
}

foreach ($container->findTaggedServiceIds('sylius.legacy_grid_filter') as $id => $attributes) {
$definition = $container->findDefinition($id);

// Already configured with "sylius.grid_filter" tag.
if ($definition->hasTag(AsFilter::SERVICE_TAG)) {
continue;
}

$type = null;
$formType = null;

Expand All @@ -46,18 +59,29 @@ public function process(ContainerBuilder $container): void
$formType = $class::getFormType();
}

foreach ($attributes as $attribute) {
if (null === $type && null === ($attribute['type'] ?? null)) {
throw new InvalidArgumentException(sprintf('Tagged grid filters needs to have "type" attributes or implements "%s".', TypeAwareFilterInterface::class));
}
$this->registerFilter($filterRegistry, $formTypeRegistry, $id, $attributes, $type, $formType);
}
}

if (null === $formType && null === ($attribute['form_type'] ?? null)) {
throw new InvalidArgumentException(sprintf('Tagged grid filters needs to have "form_type" attributes or implements %s.', FormTypeAwareFilterInterface::class));
}
private function registerFilter(
Definition $filterRegistry,
Definition $formTypeRegistry,
string $id,
array $attributes,
?string $type = null,
?string $formType = null,
): void {
foreach ($attributes as $attribute) {
if (null === $type && null === ($attribute['type'] ?? null)) {
throw new InvalidArgumentException(sprintf('Tagged grid filters needs to have "type" attribute or implements "%s".', TypeAwareFilterInterface::class));
}

$registry->addMethodCall('register', [$type ?? $attribute['type'], new Reference($id)]);
$formTypeRegistry->addMethodCall('add', [$type ?? $attribute['type'], 'default', $formType ?? $attribute['form_type']]);
if (null === $formType && null === ($attribute['form_type'] ?? null)) {
throw new InvalidArgumentException(sprintf('Tagged grid filters needs to have "form_type" attribute or implements "%s".', FormTypeAwareFilterInterface::class));
}

$filterRegistry->addMethodCall('register', [$type ?? $attribute['type'], new Reference($id)]);
$formTypeRegistry->addMethodCall('add', [$type ?? $attribute['type'], 'default', $formType ?? $attribute['form_type']]);
}
}
}
2 changes: 1 addition & 1 deletion src/Bundle/DependencyInjection/SyliusGridExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ static function (ChildDefinition $definition, AsFilter $attribute, \Reflector $r
);

$container->registerForAutoconfiguration(FilterInterface::class)
->addTag('sylius.grid_filter')
->addTag('sylius.legacy_grid_filter')
;

$container->registerForAutoconfiguration(DataProviderInterface::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@

namespace Sylius\Bundle\GridBundle\Tests\DependencyInjection\Compiler;

use App\Filter\AttributeNationalityFilter;
use App\Filter\Foo;
use App\Filter\NationalityFilter;
use App\Grid\Type\NationalityFilterType;
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase;
use Sylius\Bundle\GridBundle\DependencyInjection\Compiler\RegisterFiltersPass;
use Sylius\Component\Grid\Filtering\FormTypeAwareFilterInterface;
use Sylius\Component\Grid\Filtering\TypeAwareFilterInterface;
use Sylius\Component\Registry\ServiceRegistry;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
Expand Down Expand Up @@ -73,15 +76,18 @@ public function it_registers_a_grid_filter_and_form_type_for_every_service_filte
/**
* @test
*/
public function it_autoconfigures_a_grid_filter(): void
public function it_autoconfigures_a_legacy_grid_filter(): void
{
$this->registerService(NationalityFilter::class, NationalityFilter::class)
->addTag('sylius.grid_filter')
->addTag('sylius.legacy_grid_filter')
;

$this->registerService($filterRegistryServiceId = 'sylius.registry.grid_filter', ServiceRegistry::class);
$filterRegistryServiceId = 'sylius.registry.grid_filter';
$filterFormTypeRegistryServiceId = 'sylius.form_registry.grid_filter';

$this->registerService($filterRegistryServiceId, ServiceRegistry::class);
$this->registerService(
$filterFormTypeRegistryServiceId = 'sylius.form_registry.grid_filter',
$filterFormTypeRegistryServiceId,
ServiceRegistry::class,
);

Expand Down Expand Up @@ -109,6 +115,97 @@ public function it_autoconfigures_a_grid_filter(): void
);
}

/**
* @test
*/
public function it_autoconfigures_a_grid_filter(): void
{
$this->registerService(AttributeNationalityFilter::class, AttributeNationalityFilter::class)
->addTag('sylius.grid_filter', ['form_type' => NationalityFilterType::class, 'type' => AttributeNationalityFilter::class])
->addTag('sylius.legacy_grid_filter')
;

$filterRegistryServiceId = 'sylius.registry.grid_filter';
$filterFormTypeRegistryServiceId = 'sylius.form_registry.grid_filter';

$this->registerService($filterRegistryServiceId, ServiceRegistry::class);
$this->registerService(
$filterFormTypeRegistryServiceId,
ServiceRegistry::class,
);

$this->compile();

// Filter
$this->assertContainerBuilderHasServiceDefinitionWithMethodCall(
$filterRegistryServiceId,
'register',
[
AttributeNationalityFilter::class,
new Reference(AttributeNationalityFilter::class),
],
);

// Form Type
$this->assertContainerBuilderHasServiceDefinitionWithMethodCall(
$filterFormTypeRegistryServiceId,
'add',
[
AttributeNationalityFilter::class,
'default',
NationalityFilterType::class,
],
);
}

/**
* @test
*/
public function it_throws_an_exception_when_grid_filter_has_no_type_attribute(): void
{
$this->registerService(Foo::class, Foo::class)
->addTag('sylius.grid_filter')
;

$filterRegistryServiceId = 'sylius.registry.grid_filter';
$filterFormTypeRegistryServiceId = 'sylius.form_registry.grid_filter';

$this->registerService($filterRegistryServiceId, ServiceRegistry::class);
$this->registerService(
$filterFormTypeRegistryServiceId,
ServiceRegistry::class,
);

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage(sprintf('Tagged grid filters needs to have "type" attribute or implements "%s".', TypeAwareFilterInterface::class));

$this->compile();
}

/**
* @test
*/
public function it_throws_an_exception_when_grid_filter_has_no_form_type_attribute(): void
{
$this->registerService(Foo::class, Foo::class)
->addTag('sylius.grid_filter', ['type' => Foo::class])
;

$filterRegistryServiceId = 'sylius.registry.grid_filter';
$filterFormTypeRegistryServiceId = 'sylius.form_registry.grid_filter';

$this->registerService($filterRegistryServiceId, ServiceRegistry::class);
$this->registerService(
$filterFormTypeRegistryServiceId,
ServiceRegistry::class,
);

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage(sprintf('Tagged grid filters needs to have "form_type" attribute or implements "%s".', FormTypeAwareFilterInterface::class));

$this->compile();
}

protected function registerCompilerPass(ContainerBuilder $container): void
{
$container->addCompilerPass(new RegisterFiltersPass());
Expand Down
2 changes: 1 addition & 1 deletion src/Component/Attribute/AsFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#[\Attribute(\Attribute::TARGET_CLASS)]
final class AsFilter
{
public const SERVICE_TAG = 'sylius.grid.filter';
public const SERVICE_TAG = 'sylius.grid_filter';

/**
* @param class-string $formType The form type class name to use for filter rendering
Expand Down
1 change: 1 addition & 0 deletions tests/Application/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ services:
- '@app.repository.author'
public: true

App\Filter\AttributeNationalityFilter: null
App\Filter\NationalityFilter: null

App\BoardGameBlog\:
Expand Down

0 comments on commit bf029b4

Please sign in to comment.