Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce AsGrid attribute for streamlined grid configuration #349

Open
wants to merge 4 commits into
base: 1.14
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions docs/your_first_grid.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,11 @@ use Sylius\Bundle\GridBundle\Builder\Field\TwigField;
use Sylius\Bundle\GridBundle\Builder\GridBuilderInterface;
use Sylius\Bundle\GridBundle\Grid\AbstractGrid;
use Sylius\Bundle\GridBundle\Grid\ResourceAwareGridInterface;
use Sylius\Component\Grid\Attribute\AsGrid;

final class AdminSupplierGrid extends AbstractGrid implements ResourceAwareGridInterface
#[AsGrid('app_admin_supplier', Supplier::class)]
final class AdminSupplierGrid extends AbstractGrid
{
public static function getName(): string
{
return 'app_admin_supplier';
}

public function buildGrid(GridBuilderInterface $gridBuilder): void
{
$gridBuilder
Expand All @@ -133,11 +130,6 @@ final class AdminSupplierGrid extends AbstractGrid implements ResourceAwareGridI
)
;
}

public function getResourceClass(): string
{
return Supplier::class;
}
}
```

Expand Down
35 changes: 34 additions & 1 deletion src/Bundle/Grid/AbstractGrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,34 @@

use Sylius\Bundle\GridBundle\Builder\GridBuilder;
use Sylius\Bundle\GridBundle\Builder\GridBuilderInterface;
use Sylius\Component\Grid\Attribute\AsGrid;

abstract class AbstractGrid implements GridInterface
{
public static function getName(): string
{
if ($attribute = self::getAttributes()) {
if (!empty($attribute[0])) {
return $attribute[0]->newInstance()->name ?? static::class;
}
}

return static::class;
}

public function getResourceClass(): string
{
if ($attribute = self::getAttributes()) {
return $attribute[0]->newInstance()->resourceClass;
}

throw new \LogicException(sprintf(
vasilvestre marked this conversation as resolved.
Show resolved Hide resolved
'You have to implement %s method or use %s attribute.',
__METHOD__,
AsGrid::class,
));
}
Comment on lines +22 to +44
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this code needed to make AsGrid useful?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely yes


public function toArray(): array
{
$gridBuilder = $this->createGridBuilder();
Expand All @@ -29,10 +54,18 @@ public function toArray(): array

private function createGridBuilder(): GridBuilderInterface
{
if ($this instanceof ResourceAwareGridInterface) {
if ($this instanceof ResourceAwareGridInterface || $this::getAttributes() !== []) {
return GridBuilder::create($this::getName(), $this->getResourceClass());
}

return GridBuilder::create($this::getName());
}

/**
* @return \ReflectionAttribute<AsGrid>[]
*/
private static function getAttributes(): array
{
return (new \ReflectionClass(static::class))->getAttributes(AsGrid::class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
use Sylius\Bundle\GridBundle\Builder\GridBuilder;
use Sylius\Bundle\GridBundle\DependencyInjection\SyliusGridExtension;
use Sylius\Bundle\GridBundle\Doctrine\ORM\Driver;
use Sylius\Component\Grid\Tests\Dummy\AttributeFooGrid;
use Sylius\Component\Grid\Tests\Dummy\ClassAsParameterGrid;
use Sylius\Component\Grid\Tests\Dummy\Foo;
use Sylius\Component\Grid\Tests\Dummy\FooFightersGrid;
Expand Down Expand Up @@ -903,6 +904,37 @@ public function it_builds_extended_grids_with_grids_as_service(): void
]);
}

/**
* @test
*/
public function it_builds_grid_with_a_grid_attribute(): void
{
$grid = new AttributeFooGrid();

$this->load([
'grids' => [
AttributeFooGrid::class => $grid->toArray(),
],
]);

$this->assertContainerBuilderHasParameter('sylius.grids_definitions', [
AttributeFooGrid::class => [
'driver' => [
'name' => Driver::NAME,
'options' => [
'class' => Foo::class,
],
],
'removals' => [],
'sorting' => [],
'limits' => [10, 25, 50],
'fields' => [],
'filters' => [],
'actions' => [],
],
]);
}

protected function getContainerExtensions(): array
{
return [
Expand Down
24 changes: 24 additions & 0 deletions src/Component/Attribute/AsGrid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?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 AsGrid
{
public function __construct(
public readonly string $resourceClass,
public readonly ?string $name = null,
) {
}
}
26 changes: 26 additions & 0 deletions src/Component/Tests/Dummy/AttributeFooGrid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?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\Tests\Dummy;

use Sylius\Bundle\GridBundle\Builder\GridBuilderInterface;
use Sylius\Bundle\GridBundle\Grid\AbstractGrid;
use Sylius\Component\Grid\Attribute\AsGrid;

#[AsGrid(resourceClass: Foo::class)]
final class AttributeFooGrid extends AbstractGrid
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it required to extend AbstractGrid to take advantage of AsGrid attribute?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch, I will take a loot. AFAIK an interface should be enough

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, the AbstractGrid fetches the name in attributes or via code.
If we use GridInterface, getName() and toArray() must be reimplemented.

{
public function buildGrid(GridBuilderInterface $gridBuilder): void
{
}
}
Loading