-
Notifications
You must be signed in to change notification settings - Fork 48
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
base: 1.14
Are you sure you want to change the base?
Conversation
4ad598f
to
8d8f7d0
Compare
2759ae2
to
a36f44a
Compare
src/Bundle/Tests/DependencyInjection/GridBuilderConfigurationTest.php
Outdated
Show resolved
Hide resolved
use Sylius\Component\Grid\Attribute\AsGrid; | ||
|
||
#[AsGrid(resourceClass: Foo::class)] | ||
final class AttributeFooGrid extends AbstractGrid |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 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, | ||
)); | ||
} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Absolutely yes
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:
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.