Skip to content

Commit 712b9b5

Browse files
committed
Fix 1.15 branch CI
1 parent d9088c5 commit 712b9b5

File tree

20 files changed

+220
-191
lines changed

20 files changed

+220
-191
lines changed

phpstan.neon.dist

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,8 @@ parameters:
3333
identifier: method.nonObject
3434
count: 166
3535
path: src/Bundle/DependencyInjection/Configuration.php
36+
# Symfony Dependency injection Reflector
37+
- message: '#^Parameter \#2 \$configurator of method Symfony\\Component\\DependencyInjection\\ContainerBuilder\:\:registerAttributeForAutoconfiguration\(\) expects callable\(Symfony\\Component\\DependencyInjection\\ChildDefinition, Sylius\\Component\\Grid\\Attribute\\AsGrid, Reflector\)\: void, Closure\(Symfony\\Component\\DependencyInjection\\ChildDefinition, Sylius\\Component\\Grid\\Attribute\\AsGrid, ReflectionClass\<object\>\)\: void given\.$#'
38+
identifier: argument.type
39+
count: 1
40+
path: src/Bundle/DependencyInjection/SyliusGridExtension.php

src/Bundle/DependencyInjection/Compiler/RegisterAttributeGridsPass.php

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@
1313

1414
namespace Sylius\Bundle\GridBundle\DependencyInjection\Compiler;
1515

16-
use Sylius\Bundle\GridBundle\Grid\GridInterface;
16+
use Sylius\Bundle\GridBundle\Grid\InvokableGrid;
1717
use Sylius\Component\Grid\Attribute\AsGrid;
18-
use Sylius\Component\Grid\Metadata\Grid\InvokableGrid;
1918
use Symfony\Component\DependencyInjection\ChildDefinition;
2019
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
2120
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -25,33 +24,31 @@ final class RegisterAttributeGridsPass implements CompilerPassInterface
2524
{
2625
private const TAG = 'sylius.attribute_grid';
2726

27+
/**
28+
* @param \ReflectionClass<object> $reflector
29+
*/
2830
public static function autoconfigureFromGridAttribute(ChildDefinition $definition, AsGrid $attribute, \ReflectionClass $reflector): void
2931
{
30-
if ($reflector->implementsInterface(GridInterface::class)) {
32+
if (
33+
!$reflector->hasMethod('__invoke')
34+
) {
3135
return;
3236
}
3337

34-
$definition->addTag(self::TAG, [
35-
'resource_class' => $attribute->resourceClass,
36-
'name' => $attribute->name ?? $reflector->getName(),
37-
'build_method' => $attribute->buildMethod,
38-
'provider' => $attribute->provider,
39-
]);
38+
$definition->addTag(self::TAG);
4039
}
4140

4241
public function process(ContainerBuilder $container): void
4342
{
4443
foreach ($container->findTaggedServiceIds(self::TAG, true) as $serviceId => $tags) {
44+
/**
45+
* @var int $id
46+
* @var array{string, mixed} $attribute
47+
*/
4548
foreach ($tags as $id => $attribute) {
46-
if (!isset($attribute['name'])) {
47-
throw new \InvalidArgumentException('Tagged grids need to have a `name` attribute.');
48-
}
49-
5049
$container->register('.sylius.grid.' . $serviceId . $id, InvokableGrid::class)
5150
->setArguments([
52-
$attribute['name'],
5351
new Reference($serviceId),
54-
$attribute['resource_class'] ?? null,
5552
])
5653
->addTag('sylius.invokable_grid');
5754
}

src/Bundle/DependencyInjection/Compiler/RegisterGridCollectionPass.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ final class RegisterGridCollectionPass implements CompilerPassInterface
2121
{
2222
public function process(ContainerBuilder $container): void
2323
{
24-
if (!$container->hasDefinition('sylius.grid.grid_collection')) {
24+
if (!$container->hasDefinition('sylius.grid.invokable_grid_collection')) {
2525
return;
2626
}
2727

28-
$definition = $container->findDefinition('sylius.grid.grid_collection');
28+
$definition = $container->findDefinition('sylius.grid.invokable_grid_collection');
2929

3030
$grids = $container->findTaggedServiceIds('sylius.invokable_grid');
3131

src/Bundle/Grid/InvokableGrid.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Sylius Sp. z o.o.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Sylius\Bundle\GridBundle\Grid;
15+
16+
use Sylius\Bundle\GridBundle\Builder\GridBuilderInterface;
17+
use Sylius\Component\Grid\Attribute\AsGrid;
18+
use Sylius\Component\Grid\Exception\InvalidArgumentException;
19+
use Sylius\Component\Grid\Exception\LogicException;
20+
use Sylius\Component\Grid\ResourceAwareGridInterface as ComponentResourceAwareGridInterface;
21+
22+
/** @internal */
23+
final class InvokableGrid implements ComponentResourceAwareGridInterface
24+
{
25+
private string $name;
26+
27+
/** @var callable */
28+
private $grid;
29+
30+
private ?string $resourceClass;
31+
32+
public function __construct(
33+
callable $grid,
34+
) {
35+
if (!\is_object($grid) || $grid instanceof \Closure) {
36+
throw new InvalidArgumentException('The grid must be an invokable object.');
37+
}
38+
39+
$attributes = (new \ReflectionClass($grid))->getAttributes(AsGrid::class);
40+
41+
if (0 === \count($attributes)) {
42+
throw new LogicException(sprintf('The grid must use the "%s" attribute.', AsGrid::class));
43+
}
44+
45+
/** @var AsGrid $attribute */
46+
$attribute = $attributes[0]->newInstance();
47+
48+
$this->name = $attribute->name ?? $grid::class;
49+
$this->grid = $grid;
50+
$this->resourceClass = $attribute->resourceClass;
51+
}
52+
53+
public function __invoke(GridBuilderInterface $gridBuilder): void
54+
{
55+
($this->grid)($gridBuilder);
56+
}
57+
58+
public function getName(): string
59+
{
60+
return $this->name;
61+
}
62+
63+
public function getResourceClass(): ?string
64+
{
65+
return $this->resourceClass;
66+
}
67+
}

src/Component/Provider/InvokableGridProvider.php renamed to src/Bundle/Provider/InvokableGridProvider.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,32 @@
1111

1212
declare(strict_types=1);
1313

14-
namespace Sylius\Component\Grid\Provider;
14+
namespace Sylius\Bundle\GridBundle\Provider;
1515

1616
use Sylius\Bundle\GridBundle\Builder\GridBuilder;
1717
use Sylius\Component\Grid\Definition\ArrayToDefinitionConverterInterface;
18-
use Sylius\Component\Grid\Definition\Grid;
18+
use Sylius\Component\Grid\Definition\Grid as GridDefinition;
1919
use Sylius\Component\Grid\Exception\UndefinedGridException;
20-
use Sylius\Component\Grid\Metadata\Grid\GridCollectionInterface;
21-
use Sylius\Component\Grid\Metadata\Grid\InvokableGrid;
20+
use Sylius\Component\Grid\InvokableGridCollectionInterface;
21+
use Sylius\Component\Grid\Provider\GridProviderInterface;
22+
use Sylius\Component\Grid\ResourceAwareGridInterface;
2223

2324
final readonly class InvokableGridProvider implements GridProviderInterface
2425
{
2526
public function __construct(
26-
private GridCollectionInterface $grids,
27+
private InvokableGridCollectionInterface $grids,
2728
private ArrayToDefinitionConverterInterface $converter,
2829
) {
2930
}
3031

31-
public function get(string $code): Grid
32+
public function get(string $code): GridDefinition
3233
{
3334
if (!$this->grids->has($code)) {
3435
throw new UndefinedGridException($code);
3536
}
3637

3738
$grid = $this->grids->get($code);
38-
39-
if (!$grid instanceof InvokableGrid) {
40-
// TODO: it should throw another exception
41-
throw new UndefinedGridException($code);
42-
}
43-
44-
$gridBuilder = GridBuilder::create($code, $grid->getResourceClass());
39+
$gridBuilder = GridBuilder::create($code, $grid instanceof ResourceAwareGridInterface ? $grid->getResourceClass() : null);
4540
$grid($gridBuilder);
4641

4742
return $this->converter->convert($code, $gridBuilder->toArray());

src/Bundle/Resources/config/services.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Sylius\Bundle\GridBundle\Maker\MakeGrid;
1818
use Sylius\Bundle\GridBundle\Parser\OptionsParser;
1919
use Sylius\Bundle\GridBundle\Parser\OptionsParserInterface;
20+
use Sylius\Bundle\GridBundle\Provider\InvokableGridProvider;
2021
use Sylius\Bundle\GridBundle\Provider\ServiceGridProvider;
2122
use Sylius\Bundle\GridBundle\Registry\GridRegistry;
2223
use Sylius\Bundle\GridBundle\Registry\GridRegistryInterface;
@@ -42,6 +43,8 @@
4243
use Sylius\Component\Grid\Filtering\FiltersApplicatorInterface;
4344
use Sylius\Component\Grid\Filtering\FiltersCriteriaResolver;
4445
use Sylius\Component\Grid\Filtering\FiltersCriteriaResolverInterface;
46+
use Sylius\Component\Grid\InvokableGridCollection;
47+
use Sylius\Component\Grid\InvokableGridCollectionInterface;
4548
use Sylius\Component\Grid\Provider\ArrayGridProvider;
4649
use Sylius\Component\Grid\Provider\ChainProvider;
4750
use Sylius\Component\Grid\Provider\GridProviderInterface;
@@ -79,6 +82,9 @@
7982

8083
$services->alias(GridRegistryInterface::class, 'sylius.grid.grid_registry');
8184

85+
$services->set('sylius.grid.invokable_grid_collection', InvokableGridCollection::class);
86+
$services->alias(InvokableGridCollectionInterface::class, 'sylius.grid.invokable_grid_collection');
87+
8288
$services->set('sylius.grid.configuration_extender', GridConfigurationExtender::class);
8389

8490
$services->alias(GridConfigurationExtenderInterface::class, 'sylius.grid.configuration_extender');
@@ -106,6 +112,13 @@
106112

107113
$services->alias(ArrayGridProvider::class, 'sylius.grid.array_grid_provider');
108114

115+
$services->set('sylius.grid.invokable_grid_provider', InvokableGridProvider::class)
116+
->args([
117+
service('sylius.grid.invokable_grid_collection'),
118+
service('sylius.grid.array_to_definition_converter'),
119+
])
120+
->tag('sylius.grid_provider', ['key' => 'service', 'priority' => -100]);
121+
109122
$services->set('sylius.grid.service_grid_provider', ServiceGridProvider::class)
110123
->args([
111124
service('sylius.grid.array_to_definition_converter'),
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Sylius Sp. z o.o.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Sylius\Bundle\GridBundle\Tests\Unit\Grid;
15+
16+
use PHPUnit\Framework\Attributes\CoversClass;
17+
use PHPUnit\Framework\TestCase;
18+
use Sylius\Bundle\GridBundle\Builder\GridBuilderInterface;
19+
use Sylius\Bundle\GridBundle\Grid\InvokableGrid;
20+
use Sylius\Component\Grid\Attribute\AsGrid;
21+
use Sylius\Component\Grid\Exception\InvalidArgumentException;
22+
use Sylius\Component\Grid\Exception\LogicException;
23+
24+
#[CoversClass(InvokableGrid::class)]
25+
final class InvokableGridTest extends TestCase
26+
{
27+
public function testHasItsNameIsTheFQCNByDefault(): void
28+
{
29+
$grid = new InvokableGrid(new SimpleGrid());
30+
31+
$this->assertSame(SimpleGrid::class, $grid->getName());
32+
}
33+
34+
public function testHasItsNameCanBeCustomizable(): void
35+
{
36+
$grid = new InvokableGrid(new CustomNameGrid());
37+
38+
$this->assertSame('custom', $grid->getName());
39+
}
40+
41+
public function testCanHaveAResourceClass(): void
42+
{
43+
$grid = new InvokableGrid(new ResourceGrid());
44+
45+
$this->assertSame(SimpleResource::class, $grid->getResourceClass());
46+
}
47+
48+
public function testItThrowsAnErrorOnClosureFunction(): void
49+
{
50+
$this->expectException(InvalidArgumentException::class);
51+
$this->expectExceptionMessage('The grid must be an invokable object.');
52+
53+
new InvokableGrid(function (GridBuilderInterface $gridBuilder): void {});
54+
}
55+
56+
public function testItThrowsAnErrorOnGridNotUsingTheAsGridAttribute(): void
57+
{
58+
$this->expectException(LogicException::class);
59+
$this->expectExceptionMessage('The grid must use the "Sylius\Component\Grid\Attribute\AsGrid" attribute.');
60+
61+
new InvokableGrid(new class() {
62+
public function __invoke(GridBuilderInterface $gridBuilder)
63+
{
64+
}
65+
});
66+
}
67+
}
68+
69+
#[AsGrid]
70+
final class SimpleGrid
71+
{
72+
public function __invoke(GridBuilderInterface $gridBuilder): void
73+
{
74+
}
75+
}
76+
77+
#[AsGrid(name: 'custom')]
78+
final class CustomNameGrid
79+
{
80+
public function __invoke(GridBuilderInterface $gridBuilder): void
81+
{
82+
}
83+
}
84+
85+
#[AsGrid(resourceClass: SimpleResource::class)]
86+
final class ResourceGrid
87+
{
88+
public function __invoke(GridBuilderInterface $gridBuilder): void
89+
{
90+
}
91+
}
92+
93+
final class SimpleResource
94+
{
95+
}

src/Component/Metadata/Grid/GridCollection.php renamed to src/Component/InvokableGridCollection.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,29 @@
1111

1212
declare(strict_types=1);
1313

14-
namespace Sylius\Component\Grid\Metadata\Grid;
14+
namespace Sylius\Component\Grid;
1515

16+
use Sylius\Bundle\GridBundle\Grid\InvokableGrid;
1617
use Sylius\Component\Grid\Exception\UndefinedGridException;
1718

1819
/**
1920
* @internal
2021
*/
21-
final class GridCollection implements GridCollectionInterface
22+
final class InvokableGridCollection implements InvokableGridCollectionInterface
2223
{
23-
/** @var array<string, Grid> */
24+
/** @var array<string, callable> */
2425
private array $grids = [];
2526

26-
public function add(Grid $grid): void
27+
public function add(callable $grid): void
2728
{
29+
if (!$grid instanceof InvokableGrid) {
30+
$grid = new InvokableGrid($grid);
31+
}
32+
2833
$this->grids[$grid->getName()] = $grid;
2934
}
3035

31-
public function get(string $name): Grid
36+
public function get(string $name): callable
3237
{
3338
if (!$this->has($name)) {
3439
throw new UndefinedGridException($name);

src/Component/Metadata/Grid/GridCollectionInterface.php renamed to src/Component/InvokableGridCollectionInterface.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111

1212
declare(strict_types=1);
1313

14-
namespace Sylius\Component\Grid\Metadata\Grid;
14+
namespace Sylius\Component\Grid;
1515

16-
/** @experimental */
17-
interface GridCollectionInterface
16+
/** @experimental */
17+
interface InvokableGridCollectionInterface
1818
{
19-
public function add(Grid $grid): void;
19+
public function add(callable $grid): void;
2020

21-
public function get(string $name): Grid;
21+
public function get(string $name): callable;
2222

2323
public function has(string $name): bool;
2424
}

0 commit comments

Comments
 (0)