-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathBaseCrudController.php
More file actions
165 lines (143 loc) · 5.09 KB
/
BaseCrudController.php
File metadata and controls
165 lines (143 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
declare(strict_types=1);
namespace Protung\EasyAdminPlusBundle\Controller;
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Dto\ActionDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\ActionGroupDto;
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
use EasyCorp\Bundle\EasyAdminBundle\Twig\Component\Option\AlertVariant;
use Override;
use Psl\Dict;
use Psl\Iter;
use Psl\Type;
use Psl\Vec;
use RuntimeException;
use Stringable;
use Symfony\Component\Translation\TranslatableMessage;
use Symfony\Contracts\Service\Attribute\SubscribedService;
use Symfony\Contracts\Translation\TranslatableInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* @template TEntity of object
* @extends AbstractCrudController<TEntity>
*/
abstract class BaseCrudController extends AbstractCrudController
{
/**
* Calling this method will disable all standard actions.
*/
public function disableAllActions(Actions $actions): Actions
{
return $this->allowOnlyActions($actions);
}
/**
* Calling this method will only disable the standard actions.
*/
protected function allowOnlyActions(Actions $actions, string ...$allowedActions): Actions
{
$allActions = [
Action::BATCH_DELETE,
Action::DELETE,
Action::DETAIL,
Action::EDIT,
Action::INDEX,
Action::NEW,
];
return $actions->disable(
...Vec\values(Dict\diff($allActions, $allowedActions)),
);
}
/**
* Calling this method will only disable the standard actions.
*/
protected function allowOnlyIndexAction(Actions $actions): Actions
{
return $this->allowOnlyActions($actions, Action::INDEX);
}
/**
* Calling this method will only disable the standard actions.
*/
protected function allowOnlyDetailAction(Actions $actions): Actions
{
return $this->allowOnlyActions($actions, Action::DETAIL);
}
protected function setActionsPermissions(Actions $actions, string $permission): Actions
{
return $this->applyToAllActions(
$actions,
static function (ActionDto|ActionGroupDto $actionDto) use ($actions, $permission): void {
$actions->setPermission($actionDto->getName(), $permission);
},
);
}
/**
* @param callable(ActionDto|ActionGroupDto):void $apply
*/
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 AdminContext<TEntity>
*/
protected function currentAdminContext(): AdminContext
{
$currentAdminContext = $this->getContext();
if ($currentAdminContext === null) {
throw new RuntimeException('Current request is not in an EasyAdmin context.');
}
return $currentAdminContext;
}
protected function addFlashMessageSuccess(string|Stringable|TranslatableInterface $message): void
{
$this->addFlashMessage(AlertVariant::Success, $message);
}
protected function addFlashMessageWarning(string|Stringable|TranslatableInterface $message): void
{
$this->addFlashMessage(AlertVariant::Warning, $message);
}
protected function addFlashMessageError(string|Stringable|TranslatableInterface $message): void
{
$this->addFlashMessage(AlertVariant::Error, $message);
}
protected function addFlashMessage(AlertVariant $type, string|Stringable|TranslatableInterface $message): void
{
$this->addFlash($type->value, $this->translate($message));
}
private function translate(string|Stringable|TranslatableInterface $message): string
{
// We check against TranslatableInterface because the implementation might be Stringable as well.
if (! $message instanceof TranslatableInterface) {
$translationDomain = $this->currentAdminContext()->getI18n()->getTranslationDomain();
$message = new TranslatableMessage((string) $message, [], $translationDomain);
}
return $message->trans($this->translator());
}
protected function translator(): TranslatorInterface
{
return $this->container->get(TranslatorInterface::class);
}
protected function adminUrlGenerator(): AdminUrlGenerator
{
return $this->container->get(AdminUrlGenerator::class);
}
/**
* @return array<array-key, string|SubscribedService>
*/
#[Override]
public static function getSubscribedServices(): array
{
return Dict\merge(
parent::getSubscribedServices(),
[
TranslatorInterface::class => '?' . TranslatorInterface::class,
],
);
}
}