From 5f25d547228ba4af1a936e7a94057ad6f5d9c132 Mon Sep 17 00:00:00 2001 From: Dragos Protung Date: Thu, 12 Nov 2026 21:42:36 +0100 Subject: [PATCH] Add ActionsWalker to apply a callback to all actions outside a controller --- README.md | 4 +++ config/mago-analyze-baseline.toml | 18 ------------ config/phpstan-baseline.neon | 12 -------- src/Action/ActionsWalker.php | 34 +++++++++++++++++++++++ src/Controller/BaseCrudController.php | 9 ++---- tests/Action/ActionsWalkerTest.php | 40 +++++++++++++++++++++++++++ 6 files changed, 80 insertions(+), 37 deletions(-) create mode 100644 src/Action/ActionsWalker.php create mode 100644 tests/Action/ActionsWalkerTest.php diff --git a/README.md b/README.md index b06cac2..628cab7 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,10 @@ Action::new('close') : false); ``` +## Applying a callback to all actions + +`ActionsWalker::applyToAllActions($actions, $apply)` calls `$apply` with every action of every page, and passes an action group as a whole, without opening it. Outside a controller, an `ActionsExtensionInterface` for example can use it. In a controller extending `BaseCrudController`, it is `$this->applyToAllActions()`. + ## License This package is licensed using the MIT License. diff --git a/config/mago-analyze-baseline.toml b/config/mago-analyze-baseline.toml index 2bee332..5e9394e 100644 --- a/config/mago-analyze-baseline.toml +++ b/config/mago-analyze-baseline.toml @@ -12,30 +12,12 @@ code = 'incompatible-return-type' message = 'Return type `array` of `Protung\EasyAdminPlusBundle\Controller\BaseCrudController::getsubscribedservices()` is incompatible with parent return type `array` of `EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController::getsubscribedservices()`' count = 1 -[[issues]] -file = 'src/Controller/BaseCrudController.php' -code = 'invalid-argument' -message = 'Invalid argument type for argument #2 of `Psl\Iter\apply`: expected `(closure(EasyCorp\Bundle\EasyAdminBundle\Dto\ActionDto|EasyCorp\Bundle\EasyAdminBundle\Dto\ActionGroupDto): mixed)`, but found `(callable(EasyCorp\Bundle\EasyAdminBundle\Dto\ActionDto|EasyCorp\Bundle\EasyAdminBundle\Dto\ActionGroupDto): void)`.' -count = 1 - [[issues]] file = 'src/Controller/BaseCrudController.php' code = 'invalid-return-statement' message = '''Invalid return type for function `Protung\EasyAdminPlusBundle\Controller\BaseCrudController::currentAdminContext`: expected `EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext<('TEntity.protung\easyadminplusbundle\controller\basecrudcontroller extends object)>`, but found `EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext`.''' count = 1 -[[issues]] -file = 'src/Controller/BaseCrudController.php' -code = 'mixed-argument' -message = 'Invalid argument type for argument #1 of `Psl\Iter\apply`: expected `iterable`, but found `mixed`.' -count = 1 - -[[issues]] -file = 'src/Controller/BaseCrudController.php' -code = 'mixed-assignment' -message = 'Assigning `mixed` type to a variable may lead to unexpected behavior.' -count = 1 - [[issues]] file = 'src/Controller/BaseCrudController.php' code = 'mixed-return-statement' diff --git a/config/phpstan-baseline.neon b/config/phpstan-baseline.neon index 3f2b4f2..153ebda 100644 --- a/config/phpstan-baseline.neon +++ b/config/phpstan-baseline.neon @@ -12,18 +12,6 @@ parameters: count: 1 path: ../src/Controller/BaseController.php - - - rawMessage: 'Parameter #1 $iterable of function Psl\Iter\apply expects iterable, mixed given.' - identifier: argument.type - count: 1 - path: ../src/Controller/BaseCrudController.php - - - - rawMessage: 'Parameter #2 $function of function Psl\Iter\apply expects Closure(mixed): mixed, callable(EasyCorp\Bundle\EasyAdminBundle\Dto\ActionDto|EasyCorp\Bundle\EasyAdminBundle\Dto\ActionGroupDto): void given.' - identifier: argument.type - count: 1 - path: ../src/Controller/BaseCrudController.php - - rawMessage: 'Return type (array) of method Protung\EasyAdminPlusBundle\Controller\BaseCrudController::getSubscribedServices() should be covariant with return type (array) of method EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController::getSubscribedServices()' identifier: method.childReturnType diff --git a/src/Action/ActionsWalker.php b/src/Action/ActionsWalker.php new file mode 100644 index 0000000..6771a77 --- /dev/null +++ b/src/Action/ActionsWalker.php @@ -0,0 +1,34 @@ +getAsDto(null)->getActions(); + + // Passing `null` asks for every page's actions, which is the array keyed by page name instead of a collection. + Type\mixed_dict()->assert($actionsByPage); + + foreach ($actionsByPage as $pageActions) { + foreach ($pageActions as $action) { + $apply($action); + } + } + + return $actions; + } +} diff --git a/src/Controller/BaseCrudController.php b/src/Controller/BaseCrudController.php index bec1bc2..d30a933 100644 --- a/src/Controller/BaseCrudController.php +++ b/src/Controller/BaseCrudController.php @@ -13,9 +13,8 @@ use EasyCorp\Bundle\EasyAdminBundle\Dto\ActionGroupDto; use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGeneratorInterface; use Override; +use Protung\EasyAdminPlusBundle\Action\ActionsWalker; use Psl\Dict; -use Psl\Iter; -use Psl\Type; use Psl\Vec; use RuntimeException; @@ -85,11 +84,7 @@ static function (ActionDto|ActionGroupDto $actionDto) use ($actions, $permission */ final protected function applyToAllActions(Actions $actions, callable $apply): Actions { - foreach (Type\mixed_dict()->coerce($actions->getAsDto(null)->getActions()) as $pageActions) { - Iter\apply($pageActions, $apply); - } - - return $actions; + return ActionsWalker::applyToAllActions($actions, $apply); } /** diff --git a/tests/Action/ActionsWalkerTest.php b/tests/Action/ActionsWalkerTest.php new file mode 100644 index 0000000..fbd2251 --- /dev/null +++ b/tests/Action/ActionsWalkerTest.php @@ -0,0 +1,40 @@ +linkToCrudAction('action'); + $group = ActionGroup::new('group')->addAction(Action::new('grouped_action')->linkToCrudAction('groupedAction')); + + $actions = Actions::new() + ->add(Crud::PAGE_DETAIL, $action) + ->add(Crud::PAGE_INDEX, $group); + + $applied = []; + $result = ActionsWalker::applyToAllActions( + $actions, + static function (ActionDto|ActionGroupDto $actionDto) use (&$applied): void { + $applied[] = $actionDto; + }, + ); + + self::assertSame($actions, $result); + self::assertSame([$action->getAsDto(), $group->getAsDto()], $applied); + } +}