Skip to content
Merged
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 0 additions & 18 deletions config/mago-analyze-baseline.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,12 @@ code = 'incompatible-return-type'
message = 'Return type `array<array-key, Symfony\Contracts\Service\Attribute\SubscribedService|string>` of `Protung\EasyAdminPlusBundle\Controller\BaseCrudController::getsubscribedservices()` is incompatible with parent return type `array<array-key, string>` 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<object>`.'''
count = 1

[[issues]]
file = 'src/Controller/BaseCrudController.php'
code = 'mixed-argument'
message = 'Invalid argument type for argument #1 of `Psl\Iter\apply`: expected `iterable<mixed, EasyCorp\Bundle\EasyAdminBundle\Dto\ActionDto|EasyCorp\Bundle\EasyAdminBundle\Dto\ActionGroupDto>`, 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'
Expand Down
12 changes: 0 additions & 12 deletions config/phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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<string|Symfony\Contracts\Service\Attribute\SubscribedService>) of method Protung\EasyAdminPlusBundle\Controller\BaseCrudController::getSubscribedServices() should be covariant with return type (array<string>) of method EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController<TEntity of object>::getSubscribedServices()'
identifier: method.childReturnType
Expand Down
34 changes: 34 additions & 0 deletions src/Action/ActionsWalker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Protung\EasyAdminPlusBundle\Action;

use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
use EasyCorp\Bundle\EasyAdminBundle\Dto\ActionDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\ActionGroupDto;
use Psl\Type;

final class ActionsWalker
{
/**
* Calls `$apply` with every page's actions. An action group is passed as a whole, without its actions.
*
* @param callable(ActionDto|ActionGroupDto):void $apply
*/
public static function applyToAllActions(Actions $actions, callable $apply): Actions
{
$actionsByPage = $actions->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;
}
}
9 changes: 2 additions & 7 deletions src/Controller/BaseCrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}

/**
Expand Down
40 changes: 40 additions & 0 deletions tests/Action/ActionsWalkerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Protung\EasyAdminPlusBundle\Tests\Action;

use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use EasyCorp\Bundle\EasyAdminBundle\Config\ActionGroup;
use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Dto\ActionDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\ActionGroupDto;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Protung\EasyAdminPlusBundle\Action\ActionsWalker;

#[CoversClass(ActionsWalker::class)]
final class ActionsWalkerTest extends TestCase
{
public function testApplyToAllActionsPassesEveryPagesTopLevelActions(): void
{
$action = Action::new('action')->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);
}
}
Loading