-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathAdminContextFactory.php
More file actions
268 lines (216 loc) · 12.8 KB
/
Copy pathAdminContextFactory.php
File metadata and controls
268 lines (216 loc) · 12.8 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<?php
namespace EasyCorp\Bundle\EasyAdminBundle\Factory;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Config\Option\EA;
use EasyCorp\Bundle\EasyAdminBundle\Config\Option\TextDirection;
use EasyCorp\Bundle\EasyAdminBundle\Config\UserMenu;
use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
use EasyCorp\Bundle\EasyAdminBundle\Context\CrudContext;
use EasyCorp\Bundle\EasyAdminBundle\Context\DashboardContext;
use EasyCorp\Bundle\EasyAdminBundle\Context\I18nContext;
use EasyCorp\Bundle\EasyAdminBundle\Context\RequestContext;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Controller\CrudControllerInterface;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Controller\DashboardControllerInterface;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Factory\MenuFactoryInterface;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Registry\AdminControllerRegistryInterface;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Router\AdminRouteGeneratorInterface;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Translation\EntityTranslationIdGeneratorInterface;
use EasyCorp\Bundle\EasyAdminBundle\Dto\ActionConfigDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\AssetsDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\CrudDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\DashboardDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\FilterConfigDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\I18nDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\SearchDto;
use EasyCorp\Bundle\EasyAdminBundle\Registry\TemplateRegistry;
use EasyCorp\Bundle\EasyAdminBundle\Router\EntityIdReader;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Contracts\Translation\TranslatableInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use function Symfony\Component\String\u;
use function Symfony\Component\Translation\t;
/**
* @author Javier Eguiluz <javier.eguiluz@gmail.com>
*/
final readonly class AdminContextFactory
{
public function __construct(
private ?TokenStorageInterface $tokenStorage,
private MenuFactoryInterface $menuFactory,
private AdminControllerRegistryInterface $adminControllers,
private EntityFactory $entityFactory,
private AdminRouteGeneratorInterface $adminRouteGenerator,
private ActionFactory $actionFactory,
private ?EntityTranslationIdGeneratorInterface $entityTranslationIdGenerator,
private TranslatorInterface $translator,
) {
}
public function create(Request $request, DashboardControllerInterface $dashboardController, ?CrudControllerInterface $crudController, ?string $actionName = null): AdminContext
{
$crudAction = $actionName ?? $request->query->get(EA::CRUD_ACTION);
$validPageNames = [Crud::PAGE_INDEX, Crud::PAGE_DETAIL, Crud::PAGE_EDIT, Crud::PAGE_NEW];
$pageName = \in_array($crudAction, $validPageNames, true) ? $crudAction : null;
$dashboardDto = $this->getDashboardDto($dashboardController);
$assetDto = $this->getAssetDto($dashboardController, $crudController, $pageName);
$filters = $this->getFilters($dashboardController, $crudController);
// build a first version of CrudDto without actions so we can create AdminContext, which is
// needed for action extensions; later, we'll update the CrudDto object with the full action config
$crudDto = $this->getCrudDto($this->adminControllers, $dashboardController, $crudController, new ActionConfigDto(), $filters, $crudAction, $pageName);
$entityDto = $this->getEntityDto($request, $crudDto);
$searchDto = $this->getSearchDto($request, $crudDto);
$i18nDto = $this->getI18nDto($request, $dashboardDto, $crudDto, $entityDto);
$templateRegistry = $this->getTemplateRegistry($dashboardController, $crudDto);
$user = $this->getUser($this->tokenStorage);
// build sub-contexts
$requestContext = new RequestContext($request, $user);
$crudContext = new CrudContext($crudDto, $entityDto, $searchDto, $this->adminControllers);
$dashboardContext = new DashboardContext($dashboardDto, $dashboardController::class, $assetDto);
$i18nContext = new I18nContext($i18nDto, $templateRegistry);
// set lazy menu builders to avoid circular dependencies
// (MenuFactory needs AdminContext, but we're still creating it)
$dashboardContext->setMainMenuBuilder(function () use ($dashboardController) {
$configuredMenuItems = $dashboardController->configureMenuItems();
$mainMenuItems = \is_array($configuredMenuItems) ? $configuredMenuItems : iterator_to_array($configuredMenuItems, false);
return $this->menuFactory->createMainMenu($mainMenuItems);
});
$dashboardContext->setUserMenuBuilder(function () use ($dashboardController, $user) {
if (null === $user) {
return UserMenu::new()->getAsDto();
}
return $this->menuFactory->createUserMenu($dashboardController->configureUserMenu($user));
});
$adminContext = new AdminContext($requestContext, $crudContext, $dashboardContext, $i18nContext);
// build actions with extensions and update the CrudDto
// (ActionFactory needs the full AdminContext to apply extensions)
$actionConfigDto = $this->actionFactory->buildActionsConfig($dashboardController, $crudController, $adminContext, $pageName);
$crudDto?->setActionsConfig($actionConfigDto);
return $adminContext;
}
private function getDashboardDto(DashboardControllerInterface $dashboardControllerInstance): DashboardDto
{
$dashboardRoutes = $this->adminRouteGenerator->getDashboardRoutes();
$dashboardFqcn = $dashboardControllerInstance::class;
$dashboardRouteName = $dashboardRoutes[$dashboardFqcn] ?? null;
if (null === $dashboardRouteName) {
throw new \RuntimeException(sprintf('The name of the route associated to "%s" cannot be determined. Clear the application cache to run the EasyAdmin custom route loader, which generates the needed data to find this route.', $dashboardControllerInstance::class));
}
$dashboardDto = $dashboardControllerInstance->configureDashboard()->getAsDto();
$dashboardDto->setRouteName($dashboardRouteName);
return $dashboardDto;
}
private function getAssetDto(DashboardControllerInterface $dashboardController, ?CrudControllerInterface $crudController, ?string $pageName): AssetsDto
{
$defaultAssets = $dashboardController->configureAssets();
if (null === $crudController) {
return $defaultAssets->getAsDto();
}
return $crudController->configureAssets($defaultAssets)->getAsDto()->loadedOn($pageName);
}
private function getCrudDto(AdminControllerRegistryInterface $adminControllers, DashboardControllerInterface $dashboardController, ?CrudControllerInterface $crudController, ActionConfigDto $actionConfigDto, FilterConfigDto $filters, ?string $crudAction, ?string $pageName): ?CrudDto
{
if (null === $crudController) {
return null;
}
$defaultCrud = $dashboardController->configureCrud();
$crudDto = $crudController->configureCrud($defaultCrud)->getAsDto();
$entityFqcn = $adminControllers->findEntityByCrudController($crudController::class);
$crudDto->setControllerFqcn($crudController::class);
$crudDto->setActionsConfig($actionConfigDto);
$crudDto->setFiltersConfig($filters);
$crudDto->setCurrentAction($crudAction);
$crudDto->setEntityFqcn($entityFqcn);
$crudDto->setPageName($pageName);
return $crudDto;
}
private function getFilters(DashboardControllerInterface $dashboardController, ?CrudControllerInterface $crudController): FilterConfigDto
{
if (null === $crudController) {
return new FilterConfigDto();
}
$defaultFilterConfig = $dashboardController->configureFilters();
return $crudController->configureFilters($defaultFilterConfig)->getAsDto();
}
private function getTemplateRegistry(DashboardControllerInterface $dashboardController, ?CrudDto $crudDto): TemplateRegistry
{
$templateRegistry = TemplateRegistry::new();
$defaultCrudDto = $dashboardController->configureCrud()->getAsDto();
$templateRegistry->setTemplates($defaultCrudDto->getOverriddenTemplates());
if (null !== $crudDto) {
$templateRegistry->setTemplates($crudDto->getOverriddenTemplates());
}
return $templateRegistry;
}
private function getI18nDto(Request $request, DashboardDto $dashboardDto, ?CrudDto $crudDto, ?EntityDto $entityDto): I18nDto
{
$locale = $request->getLocale();
$configuredTextDirection = $dashboardDto->getTextDirection();
$localePrefix = strtolower(substr($locale, 0, 2));
$defaultTextDirection = \in_array($localePrefix, ['ar', 'fa', 'he'], true) ? TextDirection::RTL : TextDirection::LTR;
$textDirection = $configuredTextDirection ?? $defaultTextDirection;
$translationDomain = $dashboardDto->getTranslationDomain();
$translationParameters = [];
if (null !== $crudDto) {
$translationParameters['%entity_name%'] = basename(str_replace('\\', '/', $crudDto->getEntityFqcn()));
$translationParameters['%entity_as_string%'] = null === $entityDto ? '' : (string) $entityDto;
$translationParameters['%entity_id%'] = $entityId = EntityIdReader::fromRequest($request);
$translationParameters['%entity_short_id%'] = null === $entityId ? null : u($entityId)->truncate(7)->toString();
$entityInstance = $entityDto?->getInstance();
$pageName = $crudDto->getCurrentPage();
$singularLabel = $crudDto->getEntityLabelInSingular($entityInstance, $pageName)
?? ($dashboardDto->isUseEntityTranslations() && null !== $this->entityTranslationIdGenerator
? $this->entityTranslationIdGenerator->generateForEntity($crudDto->getEntityFqcn(), true)
: $translationParameters['%entity_name%']
);
if (!$singularLabel instanceof TranslatableInterface) {
$singularLabel = t($singularLabel, $translationParameters, $translationDomain);
}
$pluralLabel = $crudDto->getEntityLabelInPlural($entityInstance, $pageName)
?? ($dashboardDto->isUseEntityTranslations() && null !== $this->entityTranslationIdGenerator
? $this->entityTranslationIdGenerator->generateForEntity($crudDto->getEntityFqcn(), false)
: $translationParameters['%entity_name%']
);
if (!$pluralLabel instanceof TranslatableInterface) {
$pluralLabel = t($pluralLabel, $translationParameters, $translationDomain);
}
$crudDto->setEntityLabelInSingular($singularLabel);
$crudDto->setEntityLabelInPlural($pluralLabel);
$translationParameters['%entity_label_singular%'] = $singularLabel->trans($this->translator, $locale);
$translationParameters['%entity_label_plural%'] = $pluralLabel->trans($this->translator, $locale);
}
return new I18nDto($locale, $textDirection, $translationDomain, $translationParameters);
}
public function getSearchDto(Request $request, ?CrudDto $crudDto): ?SearchDto
{
if (null === $crudDto) {
return null;
}
$searchableProperties = $crudDto->getSearchFields();
$query = $request->query->has(EA::QUERY) ? (string) $request->query->get(EA::QUERY) : null;
$defaultSort = $crudDto->getDefaultSort();
$customSort = $request->query->all(EA::SORT);
$appliedFilters = $request->query->all(EA::FILTERS);
$searchMode = $crudDto->getSearchMode();
return new SearchDto($request, $searchableProperties, $query, $defaultSort, $customSort, $appliedFilters, $searchMode);
}
// Copied from https://github.com/symfony/twig-bridge/blob/master/AppVariable.php
// (c) Fabien Potencier <fabien@symfony.com> - MIT License
private function getUser(?TokenStorageInterface $tokenStorage): ?UserInterface
{
if (null === $token = $tokenStorage?->getToken()) {
return null;
}
$user = $token->getUser();
return \is_object($user) ? $user : null;
}
private function getEntityDto(Request $request, ?CrudDto $crudDto): ?EntityDto
{
if (null === $crudDto) {
return null;
}
$entityId = EntityIdReader::fromRequest($request);
return $this->entityFactory->create($crudDto->getEntityFqcn(), $entityId, $crudDto->getEntityPermission());
}
}