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

Conversation

vasilvestre
Copy link

@vasilvestre vasilvestre commented Oct 22, 2024

To simplify the configuration of grids and promote code clarity, this PR introduces a new attribute, AsGrid, which allows developers to define the grid's name and resource class directly within their grid classes, eliminating the need for YAML configuration files.

Previously, these properties were defined at usage which could cause code duplication.

By using the AsGrid attribute, developers can now explicitly declare the name and resource class of a grid directly above the class definition. This approach improves code readability, centralizes configuration, and enhances maintainability.

Example usage:

use Sylius\Component\Grid\Metadata\AsGrid;

#[AsGrid(name: 'app_admin_product', resourceClass: Product::class)]
final class ProductGrid implements GridInterface
{
    // ...
}

This PR includes comprehensive tests and updated documentation to guide developers on using the new attribute.

Future considerations:

In a future PR, we can consider deprecating the YAML-based grid configuration in favor of the attribute-based approach to further streamline the grid creation process and improve consistency.

@vasilvestre vasilvestre changed the title feat(attributes): implement AsGrid Introduce AsGrid attribute for streamlined grid configuration Oct 22, 2024
@vasilvestre vasilvestre marked this pull request as ready for review October 22, 2024 10:04
@vasilvestre vasilvestre changed the base branch from 1.13 to 1.14 October 22, 2024 10:04
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.

Comment on lines +22 to +44
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(
'You have to implement %s method or use %s attribute.',
__METHOD__,
AsGrid::class,
));
}
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants