-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathEntityFactory.php
More file actions
201 lines (168 loc) · 7.28 KB
/
EntityFactory.php
File metadata and controls
201 lines (168 loc) · 7.28 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
<?php
namespace EasyCorp\Bundle\EasyAdminBundle\Factory;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Persistence\ObjectManager;
use Doctrine\Persistence\Proxy;
use EasyCorp\Bundle\EasyAdminBundle\Collection\ActionCollection;
use EasyCorp\Bundle\EasyAdminBundle\Collection\EntityCollection;
use EasyCorp\Bundle\EasyAdminBundle\Collection\FieldCollection;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Factory\ActionFactoryInterface;
use EasyCorp\Bundle\EasyAdminBundle\Dto\ActionConfigDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityBuiltEvent;
use EasyCorp\Bundle\EasyAdminBundle\Exception\EntityNotFoundException;
use EasyCorp\Bundle\EasyAdminBundle\Security\Permission;
use Symfony\Component\ExpressionLanguage\Expression;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
/**
* @author Javier Eguiluz <javier.eguiluz@gmail.com>
*/
final class EntityFactory
{
private FieldFactory $fieldFactory;
private ActionFactoryInterface $actionFactory;
private AuthorizationCheckerInterface $authorizationChecker;
private ManagerRegistry $doctrine;
private EventDispatcherInterface $eventDispatcher;
public function __construct(FieldFactory $fieldFactory, ActionFactoryInterface $actionFactory, AuthorizationCheckerInterface $authorizationChecker, ManagerRegistry $doctrine, EventDispatcherInterface $eventDispatcher)
{
$this->fieldFactory = $fieldFactory;
$this->actionFactory = $actionFactory;
$this->authorizationChecker = $authorizationChecker;
$this->doctrine = $doctrine;
$this->eventDispatcher = $eventDispatcher;
}
public function processFields(EntityDto $entityDto, FieldCollection $fields): void
{
$this->fieldFactory->processFields($entityDto, $fields);
}
public function processFieldsForAll(EntityCollection $entities, FieldCollection $fields): void
{
foreach ($entities as $entity) {
$this->processFields($entity, clone $fields);
$entities->set($entity);
}
}
public function processActions(EntityDto $entityDto, ActionConfigDto $actionConfigDto): void
{
$this->actionFactory->processEntityActions($entityDto, $actionConfigDto);
}
public function processActionsForAll(EntityCollection $entities, ActionConfigDto $actionConfigDto): ActionCollection
{
foreach ($entities as $entity) {
$this->processActions($entity, clone $actionConfigDto);
}
return $this->actionFactory->processGlobalActions($actionConfigDto);
}
/**
* @param class-string $entityFqcn
*/
public function create(string $entityFqcn, mixed $entityId = null, string|Expression|null $entityPermission = null): EntityDto
{
return $this->doCreate($entityFqcn, $entityId, $entityPermission);
}
/**
* @param object $entityInstance
*/
public function createForEntityInstance($entityInstance): EntityDto
{
return $this->doCreate(null, null, null, $entityInstance);
}
/**
* @param iterable<object>|null $entityInstances
*/
public function createCollection(EntityDto $entityDto, ?iterable $entityInstances): EntityCollection
{
$entityDtos = [];
foreach ($entityInstances as $entityInstance) {
$newEntityDto = $entityDto->newWithInstance($entityInstance);
$newEntityId = $newEntityDto->getPrimaryKeyValueAsString();
if (!$this->authorizationChecker->isGranted(Permission::EA_ACCESS_ENTITY, $newEntityDto)) {
$newEntityDto->markAsInaccessible();
}
$entityDtos[$newEntityId] = $newEntityDto;
}
return EntityCollection::new($entityDtos);
}
/**
* @template TEntity of object
*
* @param class-string<TEntity> $entityFqcn
*
* @return ClassMetadata<TEntity>
*/
public function getEntityMetadata(string $entityFqcn): ClassMetadata
{
$entityManager = $this->getEntityManager($entityFqcn);
/** @var ClassMetadata<TEntity> $entityMetadata */
$entityMetadata = $entityManager->getClassMetadata($entityFqcn);
if (1 !== \count($entityMetadata->getIdentifierFieldNames())) {
throw new \RuntimeException(sprintf('EasyAdmin does not support Doctrine entities with composite primary keys (such as the ones used in the "%s" entity).', $entityFqcn));
}
return $entityMetadata;
}
/**
* @param class-string|null $entityFqcn
*/
private function doCreate(?string $entityFqcn = null, mixed $entityId = null, string|Expression|null $entityPermission = null, ?object $entityInstance = null): EntityDto
{
if (null === $entityInstance && null !== $entityFqcn) {
$entityInstance = null === $entityId ? null : $this->getEntityInstance($entityFqcn, $entityId);
}
if (null !== $entityInstance && null === $entityFqcn) {
if ($entityInstance instanceof Proxy) {
$entityInstance->__load();
}
$entityFqcn = $this->getRealClass($entityInstance::class);
}
$entityMetadata = $this->getEntityMetadata($entityFqcn);
$entityDto = new EntityDto($entityFqcn, $entityMetadata, $entityPermission, $entityInstance);
if (!$this->authorizationChecker->isGranted(Permission::EA_ACCESS_ENTITY, $entityDto)) {
$entityDto->markAsInaccessible();
}
$this->eventDispatcher->dispatch(new AfterEntityBuiltEvent($entityDto));
return $entityDto;
}
/**
* @param class-string $entityFqcn
*/
private function getEntityManager(string $entityFqcn): ObjectManager
{
if (null === $entityManager = $this->doctrine->getManagerForClass($entityFqcn)) {
throw new \RuntimeException(sprintf('There is no Doctrine Entity Manager defined for the "%s" class', $entityFqcn));
}
return $entityManager;
}
/**
* @param class-string $entityFqcn
*/
private function getEntityInstance(string $entityFqcn, mixed $entityIdValue): object
{
$entityManager = $this->getEntityManager($entityFqcn);
if (null === $entityInstance = $entityManager->getRepository($entityFqcn)->find($entityIdValue)) {
$entityIdName = $entityManager->getClassMetadata($entityFqcn)->getIdentifierFieldNames()[0];
throw new EntityNotFoundException(['entity_name' => $entityFqcn, 'entity_id_name' => $entityIdName, 'entity_id_value' => $entityIdValue]);
}
return $entityInstance;
}
/**
* Code copied from Symfony\Bridge\Doctrine\Form\DoctrineOrmTypeGuesser
* because Doctrine ORM 3.x removed the ClassUtil class where this method was defined
* (c) Fabien Potencier <fabien@symfony.com> - MIT License.
*
* @param class-string $class
*
* @return class-string
*/
private function getRealClass(string $class): string
{
if (false === $pos = strrpos($class, '\\'.Proxy::MARKER.'\\')) {
return $class;
}
/** @var class-string $realClass */
$realClass = substr($class, $pos + Proxy::MARKER_LENGTH + 2);
return $realClass;
}
}