Skip to content

Commit

Permalink
feature #6504 Allow AdminCrud and AdminAction attributes to be us…
Browse files Browse the repository at this point in the history
…ed without parameter names (Lustmored)

This PR was squashed before being merged into the 4.x branch.

Discussion
----------

Allow `AdminCrud` and `AdminAction` attributes to be used without parameter names

This PR allows using both `AdminCrud` and `AdminAction` attributes in their short form, like:

```
#[AdminCrud('/user-editor', 'external_user_editor')]
#[AdminAction('/bar/bar', 'foofoo')]
```

Right now they are only functional with parameters, like:
```
#[AdminAction(routeName: 'foobar', routePath: '/bar/foo')]
```

This closes #6501

Commits
-------

c2b7dae Allow `AdminCrud` and `AdminAction` attributes to be used without parameter names
  • Loading branch information
javiereguiluz committed Nov 4, 2024
2 parents cafdaec + c2b7dae commit fe40488
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
19 changes: 11 additions & 8 deletions src/Router/AdminRouteGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,20 +228,23 @@ private function getCrudControllerRouteConfig(string $crudControllerFqcn): array

// first, check if the CRUD controller defines a custom route config in the #[AdminCrud] attribute
if (null !== $attribute) {
if (\count(array_diff(array_keys($attribute->getArguments()), ['routePath', 'routeName'])) > 0) {
/** @var AdminCrud $attributeInstance */
$attributeInstance = $attribute->newInstance();

if (\count(array_diff(array_keys($attribute->getArguments()), ['routePath', 'routeName', 0, 1])) > 0) {
throw new \RuntimeException(sprintf('In the #[AdminCrud] attribute of the "%s" CRUD controller, the route configuration defines some unsupported keys. You can only define these keys: "routePath" and "routeName".', $crudControllerFqcn));
}

if (\array_key_exists('routePath', $attribute->getArguments())) {
$crudControllerConfig['routePath'] = trim($attribute->getArguments()['routePath'], '/');
if (null !== $attributeInstance->routePath) {
$crudControllerConfig['routePath'] = trim($attributeInstance->routePath, '/');
}

if (\array_key_exists('routeName', $attribute->getArguments())) {
if (!preg_match('/^[a-zA-Z0-9_-]+$/', $attribute->getArguments()['routeName'])) {
throw new \RuntimeException(sprintf('In the #[AdminCrud] attribute of the "%s" CRUD controller, the route name "%s" is not valid. It can only contain letter, numbers, dashes, and underscores.', $crudControllerFqcn, $attribute->getArguments()['routeName']));
if (null !== $attributeInstance->routeName) {
if (!preg_match('/^[a-zA-Z0-9_-]+$/', $attributeInstance->routeName)) {
throw new \RuntimeException(sprintf('In the #[AdminCrud] attribute of the "%s" CRUD controller, the route name "%s" is not valid. It can only contain letter, numbers, dashes, and underscores.', $crudControllerFqcn, $attributeInstance->routeName));
}

$crudControllerConfig['routeName'] = trim($attribute->getArguments()['routeName'], '_');
$crudControllerConfig['routeName'] = trim($attributeInstance->routeName, '_');
}
}

Expand Down Expand Up @@ -274,7 +277,7 @@ private function getCustomActionsConfig(string $crudControllerFqcn): array
$attributeInstance = $attribute->newInstance();
$action = $method->getName();

if (\count(array_diff(array_keys($attribute->getArguments()), ['routePath', 'routeName', 'methods'])) > 0) {
if (\count(array_diff(array_keys($attribute->getArguments()), ['routePath', 'routeName', 'methods', 0, 1, 2])) > 0) {
throw new \RuntimeException(sprintf('In the "%s" CRUD controller, the #[AdminAction] attribute applied to the "%s()" action includes some unsupported keys. You can only define these keys: "routePath", "routeName", and "methods".', $crudControllerFqcn, $action));
}

Expand Down
2 changes: 2 additions & 0 deletions tests/Controller/PrettyUrls/PrettyUrlsControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public function testGeneratedRoutes()
$expectedRoutes['second_dashboard_external_user_editor_delete_this_now'] = '/second/dashboard/user-editor/{entityId}/delete';
$expectedRoutes['second_dashboard_external_user_editor_detail'] = '/second/dashboard/user-editor/custom/path-for-detail/{entityId}';
$expectedRoutes['second_dashboard_external_user_editor_foobar'] = '/second/dashboard/user-editor/bar/foo';
$expectedRoutes['admin_pretty_external_user_editor_foofoo'] = '/admin/pretty/urls/user-editor/bar/bar';
$expectedRoutes['second_dashboard_external_user_editor_foofoo'] = '/second/dashboard/user-editor/bar/bar';

self::bootKernel();
$container = static::getContainer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use EasyCorp\Bundle\EasyAdminBundle\Tests\PrettyUrlsTestApplication\Entity\User;
use Symfony\Component\HttpFoundation\Response;

#[AdminCrud(routePath: '/user-editor', routeName: 'external_user_editor')]
#[AdminCrud('/user-editor', 'external_user_editor')]
class UserCrudController extends AbstractCrudController
{
public static function getEntityFqcn(): string
Expand Down Expand Up @@ -67,6 +67,12 @@ public function someCustomAction(): Response
return new Response('This is a custom action');
}

#[AdminAction('/bar/bar', 'foofoo')]
public function anotherCustomActionWithoutPropertyNames(): Response
{
return new Response('This is custom action with short attribute syntax');
}

// this custom action doesn't use the #[AdminAction] attribute on purpose to test default behavior
public function anotherCustomAction(): Response
{
Expand Down

0 comments on commit fe40488

Please sign in to comment.