-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEntityManager.php
190 lines (160 loc) · 5.16 KB
/
EntityManager.php
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
<?php
namespace Bankiru\Api\Doctrine;
use Bankiru\Api\Doctrine\Mapping\ApiMetadata;
use Bankiru\Api\Doctrine\Mapping\EntityMetadata;
use Bankiru\Api\Doctrine\Proxy\ApiCollection;
use Bankiru\Api\Doctrine\Proxy\ProxyFactory;
use Bankiru\Api\Doctrine\Utility\IdentifierFixer;
use Doctrine\Common\Persistence\ObjectRepository;
use Doctrine\Common\Proxy\Proxy;
class EntityManager implements ApiEntityManager
{
/** @var EntityMetadataFactory */
private $metadataFactory;
/** @var Configuration */
private $configuration;
/** @var ObjectRepository[] */
private $repositories = [];
/** @var UnitOfWork */
private $unitOfWork;
/** @var ProxyFactory */
private $proxyFactory;
/**
* EntityManager constructor.
*
* @param Configuration $configuration
*/
public function __construct(Configuration $configuration)
{
$this->configuration = $configuration;
$this->metadataFactory = $configuration->getMetadataFactory();
$this->metadataFactory->setEntityManager($this);
$this->unitOfWork = new UnitOfWork($this);
$this->proxyFactory = new ProxyFactory($this);
}
/** {@inheritdoc} */
public function getConfiguration()
{
return $this->configuration;
}
/** {@inheritdoc} */
public function find($className, $id)
{
$metadata = $this->getClassMetadata($className);
$id = IdentifierFixer::fixScalarId($id, $metadata);
/** @var EntityMetadata $metadata */
if (false !== ($entity = $this->getUnitOfWork()->tryGetById($id, $metadata->rootEntityName))) {
return $entity instanceof $metadata->name ? $entity : null;
}
return $this->getUnitOfWork()->getEntityPersister($className)->loadById($id);
}
/**
* {@inheritdoc}
* @return ApiMetadata
*/
public function getClassMetadata($className)
{
return $this->getMetadataFactory()->getMetadataFor($className);
}
/** {@inheritdoc} */
public function getMetadataFactory()
{
return $this->metadataFactory;
}
/** {@inheritdoc} */
public function getUnitOfWork()
{
return $this->unitOfWork;
}
/** {@inheritdoc} */
public function persist($object)
{
if (!is_object($object)) {
throw new \InvalidArgumentException('Not an object to persist');
}
$this->getUnitOfWork()->persist($object);
}
/** {@inheritdoc} */
public function remove($object)
{
$this->getUnitOfWork()->getEntityPersister(get_class($object))
->delete($object);
}
/** {@inheritdoc} */
public function merge($object)
{
throw new \BadMethodCallException('Merge is not supported');
}
/** {@inheritdoc} */
public function clear($objectName = null)
{
$this->getUnitOfWork()->clear($objectName);
}
/** {@inheritdoc} */
public function detach($object)
{
$this->getUnitOfWork()->detach($object);
}
/** {@inheritdoc} */
public function refresh($object)
{
$this->getRepository(get_class($object))->find($object->getId());
}
/** {@inheritdoc} */
public function getRepository($className)
{
if (!array_key_exists($className, $this->repositories)) {
/** @var ApiMetadata $metadata */
$metadata = $this->getClassMetadata($className);
$repositoryClass = $metadata->getRepositoryClass();
/** @noinspection PhpInternalEntityUsedInspection */
$this->repositories[$className] = new $repositoryClass($this, $className);
}
return $this->repositories[$className];
}
/** {@inheritdoc} */
public function flush($entity = null)
{
$this->unitOfWork->commit($entity);
}
/** {@inheritdoc} */
public function initializeObject($obj)
{
if ($obj instanceof Proxy && !$obj->__isInitialized()) {
$obj->__load();
} elseif ($obj instanceof ApiCollection) {
$obj->initialize();
}
}
/** {@inheritdoc} */
public function contains($object)
{
return false;
}
/**
* Gets a reference to the entity identified by the given type and identifier
* without actually loading it, if the entity is not yet loaded.
*
* @param string $entityName The name of the entity type.
* @param mixed $id The entity identifier.
*
* @return object The entity reference.
*/
public function getReference($entityName, $id)
{
/** @var EntityMetadata $metadata */
$metadata = $this->getClassMetadata($entityName);
$id = IdentifierFixer::fixScalarId($id, $metadata);
if (false !== ($entity = $this->getUnitOfWork()->tryGetById($id, $metadata->rootEntityName))) {
return $entity instanceof $metadata->name ? $entity : null;
}
$proxy = $this->getProxyFactory()->getProxy($entityName, $id);
$this->getUnitOfWork()->registerManaged($proxy, $id, null);
return $proxy;
}
/** {@inheritdoc} */
public function getProxyFactory()
{
return $this->proxyFactory;
}
}