1 | <?php |
||
26 | class ORM extends Component implements ORMInterface, SingletonInterface |
||
27 | { |
||
28 | /** |
||
29 | * Memory section to store ORM schema. |
||
30 | */ |
||
31 | const MEMORY = 'orm.schema'; |
||
32 | |||
33 | /** |
||
34 | * @invisible |
||
35 | * @var EntityCache|null |
||
36 | */ |
||
37 | private $cache = null; |
||
38 | |||
39 | /** |
||
40 | * @var LocatorInterface |
||
41 | */ |
||
42 | private $locator; |
||
43 | |||
44 | /** |
||
45 | * Already created instantiators. |
||
46 | * |
||
47 | * @invisible |
||
48 | * @var InstantiatorInterface[] |
||
49 | */ |
||
50 | private $instantiators = []; |
||
51 | |||
52 | /** |
||
53 | * ORM schema. |
||
54 | * |
||
55 | * @invisible |
||
56 | * @var array |
||
57 | */ |
||
58 | private $schema = []; |
||
59 | |||
60 | /** |
||
61 | * @var DatabaseManager |
||
62 | */ |
||
63 | protected $manager; |
||
64 | |||
65 | /** |
||
66 | * @var RelationsConfig |
||
67 | */ |
||
68 | protected $config; |
||
69 | |||
70 | /** |
||
71 | * @invisible |
||
72 | * @var MemoryInterface |
||
73 | */ |
||
74 | protected $memory; |
||
75 | |||
76 | /** |
||
77 | * Container defines working scope for all Documents and DocumentEntities. |
||
78 | * |
||
79 | * @var ContainerInterface |
||
80 | */ |
||
81 | protected $container; |
||
82 | |||
83 | /** |
||
84 | * @param DatabaseManager $manager |
||
85 | * @param RelationsConfig $config |
||
86 | * @param LocatorInterface|null $locator |
||
87 | * @param EntityCache|null $cache |
||
88 | * @param MemoryInterface|null $memory |
||
89 | * @param ContainerInterface|null $container |
||
90 | */ |
||
91 | public function __construct( |
||
92 | DatabaseManager $manager, |
||
93 | RelationsConfig $config, |
||
94 | |||
95 | LocatorInterface $locator = null, |
||
96 | EntityCache $cache = null, |
||
97 | MemoryInterface $memory = null, |
||
98 | ContainerInterface $container = null |
||
99 | ) { |
||
100 | $this->manager = $manager; |
||
101 | $this->config = $config; |
||
102 | |||
103 | //If null is passed = no caching is expected |
||
104 | $this->cache = $cache; |
||
105 | |||
106 | $this->locator = $locator ?? new NullLocator(); |
||
107 | $this->memory = $memory ?? new NullMemory(); |
||
108 | $this->container = $container ?? new Container(); |
||
109 | |||
110 | //Loading schema from memory (if any) |
||
111 | $this->schema = $this->loadSchema(); |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Create version of ORM with different initial cache or disabled cache. |
||
116 | * |
||
117 | * @param EntityCache|null $cache |
||
118 | * |
||
119 | * @return ORM |
||
120 | */ |
||
121 | public function withCache(EntityCache $cache = null): ORM |
||
122 | { |
||
123 | $orm = clone $this; |
||
124 | $orm->cache = $cache; |
||
125 | |||
126 | return $orm; |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * Check if ORM has associated entity cache. |
||
131 | * |
||
132 | * @return bool |
||
133 | */ |
||
134 | public function hasCache(): bool |
||
135 | { |
||
136 | return !empty($this->cache); |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * Create instance of ORM SchemaBuilder. |
||
141 | * |
||
142 | * @param bool $locate Set to true to automatically locate available records and record sources |
||
143 | * sources in a project files (based on tokenizer scope). |
||
144 | * |
||
145 | * @return SchemaBuilder |
||
146 | * |
||
147 | * @throws SchemaException |
||
148 | */ |
||
149 | public function schemaBuilder(bool $locate = true): SchemaBuilder |
||
150 | { |
||
151 | /** |
||
152 | * @var SchemaBuilder $builder |
||
153 | */ |
||
154 | $builder = $this->getFactory()->make(SchemaBuilder::class, ['manager' => $this->manager]); |
||
155 | |||
156 | if ($locate) { |
||
157 | foreach ($this->locator->locateSchemas() as $schema) { |
||
158 | $builder->addSchema($schema); |
||
159 | } |
||
160 | |||
161 | foreach ($this->locator->locateSources() as $class => $source) { |
||
162 | $builder->addSource($class, $source); |
||
163 | } |
||
164 | } |
||
165 | |||
166 | return $builder; |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Specify behaviour schema for ORM to be used. Attention, you have to call renderSchema() |
||
171 | * prior to passing builder into this method. |
||
172 | * |
||
173 | * @param SchemaBuilder $builder |
||
174 | * @param bool $remember Set to true to remember packed schema in memory. |
||
175 | */ |
||
176 | public function buildSchema(SchemaBuilder $builder, bool $remember = false) |
||
177 | { |
||
178 | $this->schema = $builder->packSchema(); |
||
179 | |||
180 | if ($remember) { |
||
181 | $this->memory->saveData(static::MEMORY, $this->schema); |
||
182 | } |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * {@inheritdoc} |
||
187 | */ |
||
188 | public function define(string $class, int $property) |
||
189 | { |
||
190 | if (empty($this->schema)) { |
||
191 | $this->buildSchema($this->schemaBuilder()->renderSchema(), true); |
||
192 | } |
||
193 | |||
194 | //Check value |
||
195 | if (!isset($this->schema[$class])) { |
||
196 | throw new ORMException("Undefined ORM schema item '{$class}', make sure schema is updated"); |
||
197 | } |
||
198 | |||
199 | if (!array_key_exists($property, $this->schema[$class])) { |
||
200 | throw new ORMException("Undefined ORM schema property '{$class}'.'{$property}'"); |
||
201 | } |
||
202 | |||
203 | return $this->schema[$class][$property]; |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * {@inheritdoc} |
||
208 | */ |
||
209 | public function selector(string $class): RecordSelector |
||
210 | { |
||
211 | //ORM is cloned in order to isolate cache scope. |
||
212 | return new RecordSelector($class, clone $this); |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * {@inheritdoc} |
||
217 | */ |
||
218 | public function table(string $class): Table |
||
226 | |||
227 | public function hasTransaction(): bool |
||
228 | { |
||
229 | |||
230 | } |
||
231 | |||
232 | public function getTransaction(): TransactionInterface |
||
233 | { |
||
234 | |||
236 | |||
237 | public function beginTransaction(): TransactionInterface |
||
241 | |||
242 | /** |
||
243 | * {@inheritdoc} |
||
244 | */ |
||
245 | public function make( |
||
281 | |||
282 | /** |
||
283 | * {@inheritdoc} |
||
284 | */ |
||
285 | public function makeLoader(string $class, string $relation): LoaderInterface |
||
310 | |||
311 | /** |
||
312 | * {@inheritdoc} |
||
313 | */ |
||
314 | public function makeRelation(string $class, string $relation): RelationInterface |
||
338 | |||
339 | /** |
||
340 | * When ORM is cloned we are automatically cloning it's cache as well to create |
||
341 | * new isolated area. Basically we have cache enabled per selection. |
||
342 | * |
||
343 | * @see RecordSelector::getIterator() |
||
344 | */ |
||
345 | public function __clone() |
||
350 | |||
351 | /** |
||
352 | * Get object responsible for class instantiation. |
||
353 | * |
||
354 | * @param string $class |
||
355 | * |
||
356 | * @return InstantiatorInterface |
||
357 | */ |
||
358 | protected function instantiator(string $class): InstantiatorInterface |
||
377 | |||
378 | /** |
||
379 | * Load packed schema from memory. |
||
380 | * |
||
381 | * @return array |
||
382 | */ |
||
383 | protected function loadSchema(): array |
||
387 | |||
388 | /** |
||
389 | * Get ODM specific factory. |
||
390 | * |
||
391 | * @return FactoryInterface |
||
392 | */ |
||
393 | protected function getFactory(): FactoryInterface |
||
401 | } |
||
402 |