Skip to content

Commit 89c8869

Browse files
michaelKaeferjaviereguiluz
authored andcommitted
Deprecate 4 methods to cleanup EntityFactory
1 parent d643a71 commit 89c8869

File tree

7 files changed

+106
-46
lines changed

7 files changed

+106
-46
lines changed

config/services.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,9 @@
251251
->arg(4, service('event_dispatcher'))
252252

253253
->set(EntityFactory::class)
254-
->arg(0, service(FieldFactory::class))
255-
->arg(1, service(ActionFactory::class))
256-
->arg(2, service(AuthorizationChecker::class))
257-
->arg(3, service('doctrine'))
258-
->arg(4, service('event_dispatcher'))
254+
->arg(0, service(AuthorizationChecker::class))
255+
->arg(1, service('doctrine'))
256+
->arg(2, service('event_dispatcher'))
259257

260258
->set(EntityPaginator::class)
261259
->arg(0, service(AdminUrlGenerator::class))
@@ -337,6 +335,7 @@
337335
->arg(1, new Reference(AdminUrlGenerator::class))
338336
->arg(2, service('request_stack'))
339337
->arg(3, service(ControllerFactory::class))
338+
->arg(4, new Reference(FieldFactory::class))
340339

341340
->set(AvatarConfigurator::class)
342341

@@ -396,6 +395,7 @@
396395
->arg(0, service('request_stack'))
397396
->arg(1, service(EntityFactory::class))
398397
->arg(2, service(ControllerFactory::class))
398+
->arg(3, new Reference(FieldFactory::class))
399399

400400
->set(SlugConfigurator::class)
401401

src/Controller/AbstractCrudController.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
use EasyCorp\Bundle\EasyAdminBundle\Factory\ActionFactory;
3737
use EasyCorp\Bundle\EasyAdminBundle\Factory\ControllerFactory;
3838
use EasyCorp\Bundle\EasyAdminBundle\Factory\EntityFactory;
39+
use EasyCorp\Bundle\EasyAdminBundle\Factory\FieldFactory;
3940
use EasyCorp\Bundle\EasyAdminBundle\Factory\FilterFactory;
4041
use EasyCorp\Bundle\EasyAdminBundle\Factory\FormFactory;
4142
use EasyCorp\Bundle\EasyAdminBundle\Factory\PaginatorFactory;
@@ -115,6 +116,7 @@ public static function getSubscribedServices(): array
115116
FilterFactory::class => '?'.FilterFactory::class,
116117
FormFactory::class => '?'.FormFactory::class,
117118
PaginatorFactory::class => '?'.PaginatorFactory::class,
119+
FieldFactory::class => '?'.FieldFactory::class,
118120
]);
119121
}
120122

@@ -144,10 +146,10 @@ public function index(AdminContext $context)
144146
}
145147

146148
$entities = $this->container->get(EntityFactory::class)->createCollection($context->getEntity(), $paginator->getResults());
147-
$this->container->get(EntityFactory::class)->processFieldsForAll($entities, $fields, Crud::PAGE_INDEX);
148-
$procesedFields = $entities->first()?->getFields() ?? FieldCollection::new([]);
149-
$context->getCrud()->setFieldAssets($this->getFieldAssets($procesedFields));
150-
$actions = $this->container->get(EntityFactory::class)->processActionsForAll($entities, $context->getCrud()->getActionsConfig());
149+
$this->container->get(FieldFactory::class)->processFieldsForAll($entities, $fields, Crud::PAGE_INDEX);
150+
$processedFields = $entities->first()?->getFields() ?? FieldCollection::new([]);
151+
$context->getCrud()->setFieldAssets($this->getFieldAssets($processedFields));
152+
$actions = $this->container->get(ActionFactory::class)->processGlobalActionsAndEntityActionsForAll($entities, $context->getCrud()->getActionsConfig());
151153

152154
$responseParameters = $this->configureResponseParameters(KeyValueStore::new([
153155
'pageName' => Crud::PAGE_INDEX,
@@ -184,9 +186,9 @@ public function detail(AdminContext $context)
184186
throw new InsufficientEntityPermissionException($context);
185187
}
186188

187-
$this->container->get(EntityFactory::class)->processFields($context->getEntity(), FieldCollection::new($this->configureFields(Crud::PAGE_DETAIL)), Crud::PAGE_DETAIL);
189+
$this->container->get(FieldFactory::class)->processFields($context->getEntity(), FieldCollection::new($this->configureFields(Crud::PAGE_DETAIL)), Crud::PAGE_DETAIL);
188190
$context->getCrud()->setFieldAssets($this->getFieldAssets($context->getEntity()->getFields()));
189-
$this->container->get(EntityFactory::class)->processActions($context->getEntity(), $context->getCrud()->getActionsConfig());
191+
$this->container->get(ActionFactory::class)->processEntityActions($context->getEntity(), $context->getCrud()->getActionsConfig());
190192

191193
$responseParameters = $this->configureResponseParameters(KeyValueStore::new([
192194
'pageName' => Crud::PAGE_DETAIL,
@@ -219,9 +221,9 @@ public function edit(AdminContext $context)
219221
throw new InsufficientEntityPermissionException($context);
220222
}
221223

222-
$this->container->get(EntityFactory::class)->processFields($context->getEntity(), FieldCollection::new($this->configureFields(Crud::PAGE_EDIT)), Crud::PAGE_EDIT);
224+
$this->container->get(FieldFactory::class)->processFields($context->getEntity(), FieldCollection::new($this->configureFields(Crud::PAGE_EDIT)), Crud::PAGE_EDIT);
223225
$context->getCrud()->setFieldAssets($this->getFieldAssets($context->getEntity()->getFields()));
224-
$this->container->get(EntityFactory::class)->processActions($context->getEntity(), $context->getCrud()->getActionsConfig());
226+
$this->container->get(ActionFactory::class)->processEntityActions($context->getEntity(), $context->getCrud()->getActionsConfig());
225227
/** @var TEntity $entityInstance */
226228
$entityInstance = $context->getEntity()->getInstance();
227229

@@ -305,9 +307,9 @@ public function new(AdminContext $context)
305307
/** @var class-string<TEntity> $entityFqcn */
306308
$entityFqcn = $context->getEntity()->getFqcn();
307309
$context->getEntity()->setInstance($this->createEntity($entityFqcn));
308-
$this->container->get(EntityFactory::class)->processFields($context->getEntity(), FieldCollection::new($this->configureFields(Crud::PAGE_NEW)), Crud::PAGE_NEW);
310+
$this->container->get(FieldFactory::class)->processFields($context->getEntity(), FieldCollection::new($this->configureFields(Crud::PAGE_NEW)), Crud::PAGE_NEW);
309311
$context->getCrud()->setFieldAssets($this->getFieldAssets($context->getEntity()->getFields()));
310-
$this->container->get(EntityFactory::class)->processActions($context->getEntity(), $context->getCrud()->getActionsConfig());
312+
$this->container->get(ActionFactory::class)->processEntityActions($context->getEntity(), $context->getCrud()->getActionsConfig());
311313

312314
$newForm = $this->createNewForm($context->getEntity(), $context->getCrud()->getNewFormOptions(), $context);
313315
$newForm->handleRequest($context->getRequest());
@@ -495,7 +497,7 @@ public function createIndexQueryBuilder(SearchDto $searchDto, EntityDto $entityD
495497
public function renderFilters(AdminContext $context): KeyValueStore
496498
{
497499
$fields = FieldCollection::new($this->configureFields(Crud::PAGE_INDEX));
498-
$this->container->get(EntityFactory::class)->processFields($context->getEntity(), $fields, Crud::PAGE_INDEX);
500+
$this->container->get(FieldFactory::class)->processFields($context->getEntity(), $fields, Crud::PAGE_INDEX);
499501
$filters = $this->container->get(FilterFactory::class)->create($context->getCrud()->getFiltersConfig(), $context->getEntity()->getFields(), $context->getEntity());
500502

501503
/** @var FormInterface&FiltersFormType $filtersForm */

src/Factory/ActionFactory.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace EasyCorp\Bundle\EasyAdminBundle\Factory;
44

55
use EasyCorp\Bundle\EasyAdminBundle\Collection\ActionCollection;
6+
use EasyCorp\Bundle\EasyAdminBundle\Collection\EntityCollection;
67
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
78
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
89
use EasyCorp\Bundle\EasyAdminBundle\Config\Option\EA;
@@ -161,6 +162,15 @@ public function processGlobalActions(?ActionConfigDto $actionsDto = null): Actio
161162
return ActionCollection::new($processedItems);
162163
}
163164

165+
public function processGlobalActionsAndEntityActionsForAll(EntityCollection $entityDtos, ActionConfigDto $actionConfigDto): ActionCollection
166+
{
167+
foreach ($entityDtos as $entityDto) {
168+
$this->processEntityActions($entityDto, clone $actionConfigDto);
169+
}
170+
171+
return $this->processGlobalActions($actionConfigDto);
172+
}
173+
164174
private function processAction(string $pageName, ActionDto $actionDto, ?EntityDto $entityDto = null): ActionDto
165175
{
166176
$adminContext = $this->adminContextProvider->getContext();

src/Factory/EntityFactory.php

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,55 +24,58 @@
2424
final class EntityFactory
2525
{
2626
public function __construct(
27-
private readonly FieldFactory $fieldFactory,
28-
private readonly ActionFactory $actionFactory,
29-
private readonly AuthorizationCheckerInterface $authorizationChecker,
30-
private readonly ManagerRegistry $doctrine,
31-
private readonly EventDispatcherInterface $eventDispatcher,
27+
private FieldFactory|AuthorizationCheckerInterface|null $fieldFactory,
28+
private ActionFactory|ManagerRegistry|null $actionFactory,
29+
private AuthorizationCheckerInterface|EventDispatcherInterface $authorizationChecker,
30+
private ?ManagerRegistry $doctrine = null,
31+
private ?EventDispatcherInterface $eventDispatcher = null,
3232
) {
33-
}
34-
35-
public function processFields(EntityDto $entityDto, FieldCollection $fields, ?string $pageName = null): void
36-
{
37-
if (null === $pageName) {
33+
if ($this->fieldFactory instanceof FieldFactory) {
3834
trigger_deprecation(
3935
'easycorp/easyadmin-bundle',
4036
'4.27.0',
41-
'Argument "$pageName" is missing. Omitting it will cause an error in 5.0.0.',
37+
'Passing the arguments "$fieldFactory" and "$actionFactory" to the "%s" constructor is deprecated.',
38+
self::class
4239
);
40+
} else {
41+
$this->fieldFactory = null;
42+
$this->actionFactory = null;
43+
$this->authorizationChecker = $fieldFactory;
44+
$this->doctrine = $actionFactory;
45+
$this->eventDispatcher = $authorizationChecker;
4346
}
47+
}
4448

49+
/**
50+
* @deprecated since 4.27.0 and it will be removed in EasyAdmin 5.0.0. Use FieldFactory::processFields() instead
51+
*/
52+
public function processFields(EntityDto $entityDto, FieldCollection $fields, ?string $pageName = null): void
53+
{
4554
$this->fieldFactory->processFields($entityDto, $fields, $pageName);
4655
}
4756

57+
/**
58+
* @deprecated since 4.27.0 and it will be removed in EasyAdmin 5.0.0. Use FieldFactory::processFieldsForAll() instead
59+
*/
4860
public function processFieldsForAll(EntityCollection $entities, FieldCollection $fields, ?string $pageName = null): void
4961
{
50-
if (null === $pageName) {
51-
trigger_deprecation(
52-
'easycorp/easyadmin-bundle',
53-
'4.27.0',
54-
'Argument "$pageName" is missing. Omitting it will cause an error in 5.0.0.',
55-
);
56-
}
57-
58-
foreach ($entities as $entity) {
59-
$this->processFields($entity, clone $fields, $pageName);
60-
$entities->set($entity);
61-
}
62+
$this->fieldFactory->processFieldsForAll($entities, $fields);
6263
}
6364

65+
/**
66+
* @deprecated since 4.27.0 and it will be removed in EasyAdmin 5.0.0. Use ActionFactory::processEntityActions() instead
67+
*/
6468
public function processActions(EntityDto $entityDto, ActionConfigDto $actionConfigDto): void
6569
{
6670
$this->actionFactory->processEntityActions($entityDto, $actionConfigDto);
6771
}
6872

73+
/**
74+
* @deprecated since 4.27.0 and it will be removed in EasyAdmin 5.0.0. Use ActionFactory::processGlobalActionsAndEntityActionsForAll() instead
75+
*/
6976
public function processActionsForAll(EntityCollection $entities, ActionConfigDto $actionConfigDto): ActionCollection
7077
{
71-
foreach ($entities as $entity) {
72-
$this->processActions($entity, clone $actionConfigDto);
73-
}
74-
75-
return $this->actionFactory->processGlobalActions($actionConfigDto);
78+
return $this->actionFactory->processGlobalActionsAndEntityActionsForAll($entities, $actionConfigDto);
7679
}
7780

7881
/**

src/Factory/FieldFactory.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Doctrine\DBAL\Types\Types;
66
use Doctrine\ORM\Mapping\AssociationMapping;
77
use Doctrine\ORM\Mapping\FieldMapping;
8+
use EasyCorp\Bundle\EasyAdminBundle\Collection\EntityCollection;
89
use EasyCorp\Bundle\EasyAdminBundle\Collection\FieldCollection;
910
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
1011
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Field\FieldConfiguratorInterface;
@@ -135,6 +136,22 @@ public function processFields(EntityDto $entityDto, FieldCollection $fields, ?st
135136
$entityDto->setFields($fields);
136137
}
137138

139+
public function processFieldsForAll(EntityCollection $entityDtos, FieldCollection $fields, ?string $currentPage = null): void
140+
{
141+
if (null === $currentPage) {
142+
trigger_deprecation(
143+
'easycorp/easyadmin-bundle',
144+
'4.27.0',
145+
'Argument "$currentPage" is missing. Omitting it will cause an error in 5.0.0.',
146+
);
147+
}
148+
149+
foreach ($entityDtos as $entityDto) {
150+
$this->processFields($entityDto, clone $fields, $currentPage);
151+
$entityDtos->set($entityDto);
152+
}
153+
}
154+
138155
private function replaceGenericFieldsWithSpecificFields(FieldCollection $fields, EntityDto $entityDto): void
139156
{
140157
foreach ($fields as $fieldDto) {

src/Field/Configurator/AssociationConfigurator.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto;
1616
use EasyCorp\Bundle\EasyAdminBundle\Factory\ControllerFactory;
1717
use EasyCorp\Bundle\EasyAdminBundle\Factory\EntityFactory;
18+
use EasyCorp\Bundle\EasyAdminBundle\Factory\FieldFactory;
1819
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
1920
use EasyCorp\Bundle\EasyAdminBundle\Form\Type\CrudAutocompleteType;
2021
use EasyCorp\Bundle\EasyAdminBundle\Form\Type\CrudFormType;
@@ -35,7 +36,16 @@ public function __construct(
3536
private readonly AdminUrlGeneratorInterface $adminUrlGenerator,
3637
private readonly RequestStack $requestStack,
3738
private readonly ControllerFactory $controllerFactory,
39+
private readonly ?FieldFactory $fieldFactory = null,
3840
) {
41+
if (null === $this->fieldFactory) {
42+
trigger_deprecation(
43+
'easycorp/easyadmin-bundle',
44+
'4.27.0',
45+
'Not passing argument "$fieldFactory" to the "%s" constructor is deprecated.',
46+
self::class
47+
);
48+
}
3949
}
4050

4151
public function supports(FieldDto $field, EntityDto $entityDto): bool
@@ -328,7 +338,11 @@ private function createEntityDto(string $entityFqcn, string $crudControllerFqcn,
328338

329339
$fields = $crudController->configureFields($crudControllerPageName);
330340

331-
$this->entityFactory->processFields($entityDto, FieldCollection::new($fields), $crudPageName);
341+
if (null === $this->fieldFactory) {
342+
$this->entityFactory->processFields($entityDto, FieldCollection::new($fields), $crudPageName);
343+
} else {
344+
$this->fieldFactory->processFields($entityDto, FieldCollection::new($fields), $crudPageName);
345+
}
332346

333347
return $entityDto;
334348
}

src/Field/Configurator/CollectionConfigurator.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto;
1313
use EasyCorp\Bundle\EasyAdminBundle\Factory\ControllerFactory;
1414
use EasyCorp\Bundle\EasyAdminBundle\Factory\EntityFactory;
15+
use EasyCorp\Bundle\EasyAdminBundle\Factory\FieldFactory;
1516
use EasyCorp\Bundle\EasyAdminBundle\Field\CollectionField;
1617
use EasyCorp\Bundle\EasyAdminBundle\Form\Type\CrudFormType;
1718
use Symfony\Component\Form\Extension\Core\Type\CountryType;
@@ -32,7 +33,16 @@ public function __construct(
3233
private readonly RequestStack $requestStack,
3334
private readonly EntityFactory $entityFactory,
3435
private readonly ControllerFactory $controllerFactory,
36+
private readonly ?FieldFactory $fieldFactory = null,
3537
) {
38+
if (null === $this->fieldFactory) {
39+
trigger_deprecation(
40+
'easycorp/easyadmin-bundle',
41+
'4.27.0',
42+
'Not passing argument "$fieldFactory" to the "%s" constructor is deprecated.',
43+
self::class
44+
);
45+
}
3646
}
3747

3848
public function supports(FieldDto $field, EntityDto $entityDto): bool
@@ -165,7 +175,11 @@ private function createEntityDto(string $targetEntityFqcn, string $targetCrudCon
165175

166176
$fields = $crudController->configureFields($crudControllerPageName);
167177

168-
$this->entityFactory->processFields($entityDto, FieldCollection::new($fields), $crudPageName);
178+
if (null === $this->fieldFactory) {
179+
$this->entityFactory->processFields($entityDto, FieldCollection::new($fields), $crudPageName);
180+
} else {
181+
$this->fieldFactory->processFields($entityDto, FieldCollection::new($fields), $crudPageName);
182+
}
169183

170184
return $entityDto;
171185
}

0 commit comments

Comments
 (0)