Skip to content

Commit

Permalink
review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
vasilvestre committed Oct 23, 2024
1 parent 81ae79d commit 52e0442
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 41 deletions.
2 changes: 1 addition & 1 deletion docs/custom_filter.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace App\Grid\Filter;
use App\Form\Type\Filter\SuppliersStatisticsFilterType;
use Sylius\Bundle\GridBundle\Doctrine\DataSourceInterface;
use Sylius\Component\Grid\Filtering\FilterInterface;
use Sylius\Component\Grid\Metadata\AsFilter;
use Sylius\Component\Grid\Attribute\AsFilter;

#[AsFilter(formType: SuppliersStatisticsFilterType::class)]
class SuppliersStatisticsFilter implements FilterInterface
Expand Down
26 changes: 15 additions & 11 deletions src/Bundle/DependencyInjection/SyliusGridExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
use Sylius\Bundle\CurrencyBundle\SyliusCurrencyBundle;
use Sylius\Bundle\GridBundle\Grid\GridInterface;
use Sylius\Bundle\GridBundle\SyliusGridBundle;
use Sylius\Component\Grid\Attribute\AsFilter;
use Sylius\Component\Grid\Data\DataProviderInterface;
use Sylius\Component\Grid\Filtering\FilterInterface;
use Sylius\Component\Grid\Metadata\AsFilter;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand Down Expand Up @@ -62,16 +62,20 @@ public function load(array $configs, ContainerBuilder $container): void
->addTag('sylius.grid')
;

$container->registerAttributeForAutoconfiguration(AsFilter::class, function (ChildDefinition $definition, AsFilter $attribute, \Reflector $reflector): void {
if (!$reflector instanceof \ReflectionClass) {
throw new \LogicException(sprintf('The attribute "%s" can only be used on classes.', AsFilter::class));
}
$tagAttributes = [
'type' => $attribute->type ?? $reflector->getName(),
'form_type' => $attribute->formType,
];
$definition->addTag('sylius.grid_filter', $tagAttributes);
});
$container->registerAttributeForAutoconfiguration(
AsFilter::class,
static function (ChildDefinition $definition, AsFilter $attribute, \Reflector $reflector): void {
// Helps to avoid issues with psalm
if (!$reflector instanceof \ReflectionClass) {
return;
}

$definition->addTag(AsFilter::SERVICE_TAG, [
'type' => $attribute->getType() ?? $reflector->getName(),
'form_type' => $attribute->getFormType(),
]);
},
);

$container->registerForAutoconfiguration(FilterInterface::class)
->addTag('sylius.grid_filter')
Expand Down
39 changes: 39 additions & 0 deletions src/Component/Attribute/AsFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Component\Grid\Attribute;

#[\Attribute(\Attribute::TARGET_CLASS)]
final class AsFilter
{
public const SERVICE_TAG = 'sylius.grid.filter';

/**
* @param class-string $formType The form type class name to use for filter rendering
*/
public function __construct(
private readonly string $formType,
private readonly ?string $type = null,
) {
}

public function getFormType(): string
{
return $this->formType;
}

public function getType(): ?string
{
return $this->type;
}
}
28 changes: 0 additions & 28 deletions src/Component/Metadata/AsFilter.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
namespace App\Filter;

use App\Grid\Type\NationalityFilterType;
use Sylius\Component\Grid\Attribute\AsFilter;
use Sylius\Component\Grid\Data\DataSourceInterface;
use Sylius\Component\Grid\Filter\EntityFilter;
use Sylius\Component\Grid\Filtering\FilterInterface;
use Sylius\Component\Grid\Metadata\AsFilter;

#[AsFilter(
formType: NationalityFilterType::class,
Expand Down

0 comments on commit 52e0442

Please sign in to comment.