1 | <?php |
||
27 | class ORM extends Component implements ORMInterface, SingletonInterface |
||
28 | { |
||
29 | /** |
||
30 | * Memory section to store ORM schema. |
||
31 | */ |
||
32 | const MEMORY = 'orm.schema'; |
||
33 | |||
34 | /** |
||
35 | * @invisible |
||
36 | * @var EntityMap|null |
||
37 | */ |
||
38 | private $map = null; |
||
39 | |||
40 | /** |
||
41 | * @var LocatorInterface |
||
42 | */ |
||
43 | private $locator; |
||
44 | |||
45 | /** |
||
46 | * Already created instantiators. |
||
47 | * |
||
48 | * @invisible |
||
49 | * @var InstantiatorInterface[] |
||
50 | */ |
||
51 | private $instantiators = []; |
||
52 | |||
53 | /** |
||
54 | * ORM schema. |
||
55 | * |
||
56 | * @invisible |
||
57 | * @var array |
||
58 | */ |
||
59 | private $schema = []; |
||
60 | |||
61 | /** |
||
62 | * @var DatabaseManager |
||
63 | */ |
||
64 | protected $manager; |
||
65 | |||
66 | /** |
||
67 | * @var RelationsConfig |
||
68 | */ |
||
69 | protected $config; |
||
70 | |||
71 | /** |
||
72 | * @invisible |
||
73 | * @var MemoryInterface |
||
74 | */ |
||
75 | protected $memory; |
||
76 | |||
77 | /** |
||
78 | * Container defines working scope for all Documents and DocumentEntities. |
||
79 | * |
||
80 | * @var ContainerInterface |
||
81 | */ |
||
82 | protected $container; |
||
83 | |||
84 | /** |
||
85 | * @param DatabaseManager $manager |
||
86 | * @param RelationsConfig $config |
||
87 | * @param LocatorInterface|null $locator |
||
88 | * @param EntityMap|null $map |
||
89 | * @param MemoryInterface|null $memory |
||
90 | * @param ContainerInterface|null $container |
||
91 | */ |
||
92 | public function __construct( |
||
93 | DatabaseManager $manager, |
||
94 | RelationsConfig $config, |
||
95 | //Following arguments can be resolved automatically |
||
96 | LocatorInterface $locator = null, |
||
97 | EntityMap $map = null, |
||
98 | MemoryInterface $memory = null, |
||
99 | ContainerInterface $container = null |
||
100 | ) { |
||
101 | $this->manager = $manager; |
||
102 | $this->config = $config; |
||
103 | |||
104 | //If null is passed = no caching is expected |
||
105 | $this->map = $map; |
||
106 | |||
107 | $this->locator = $locator ?? new NullLocator(); |
||
108 | $this->memory = $memory ?? new NullMemory(); |
||
109 | $this->container = $container ?? new Container(); |
||
110 | |||
111 | //Loading schema from memory (if any) |
||
112 | $this->schema = $this->loadSchema(); |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Create version of ORM with different initial map or disable caching. |
||
117 | * |
||
118 | * @param EntityMap|null $map |
||
119 | * |
||
120 | * @return ORM |
||
121 | */ |
||
122 | public function withMap(EntityMap $map = null): ORM |
||
123 | { |
||
124 | $orm = clone $this; |
||
125 | $orm->map = $map; |
||
126 | |||
127 | return $orm; |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Check if ORM has associated entity cache. |
||
132 | * |
||
133 | * @return bool |
||
134 | */ |
||
135 | public function hasMap(): bool |
||
136 | { |
||
137 | return !empty($this->map); |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * Create instance of ORM SchemaBuilder. |
||
142 | * |
||
143 | * @param bool $locate Set to true to automatically locate available records and record sources |
||
144 | * sources in a project files (based on tokenizer scope). |
||
145 | * |
||
146 | * @return SchemaBuilder |
||
147 | * |
||
148 | * @throws SchemaException |
||
149 | */ |
||
150 | public function schemaBuilder(bool $locate = true): SchemaBuilder |
||
169 | |||
170 | /** |
||
171 | * Specify behaviour schema for ORM to be used. Attention, you have to call renderSchema() |
||
172 | * prior to passing builder into this method. |
||
173 | * |
||
174 | * @param SchemaBuilder $builder |
||
175 | * @param bool $remember Set to true to remember packed schema in memory. |
||
176 | */ |
||
177 | public function buildSchema(SchemaBuilder $builder, bool $remember = false) |
||
185 | |||
186 | /** |
||
187 | * {@inheritdoc} |
||
188 | */ |
||
189 | public function define(string $class, int $property) |
||
206 | |||
207 | /** |
||
208 | * Get source (selection repository) for specific entity class. |
||
209 | * |
||
210 | * @param string $class |
||
211 | * |
||
212 | * @return RecordSource |
||
213 | */ |
||
214 | public function source(string $class): RecordSource |
||
234 | |||
235 | /** |
||
236 | * {@inheritdoc} |
||
237 | */ |
||
238 | public function selector(string $class): RecordSelector |
||
243 | |||
244 | /** |
||
245 | * {@inheritdoc} |
||
246 | */ |
||
247 | public function table(string $class): Table |
||
255 | |||
256 | /** |
||
257 | * {@inheritdoc} |
||
258 | */ |
||
259 | public function make( |
||
297 | |||
298 | /** |
||
299 | * {@inheritdoc} |
||
300 | */ |
||
301 | public function makeLoader(string $class, string $relation): LoaderInterface |
||
326 | |||
327 | /** |
||
328 | * {@inheritdoc} |
||
329 | */ |
||
330 | public function makeRelation(string $class, string $relation): RelationInterface |
||
354 | |||
355 | /** |
||
356 | * When ORM is cloned we are automatically cloning it's cache as well to create |
||
357 | * new isolated area. Basically we have cache enabled per selection. |
||
358 | * |
||
359 | * @see RecordSelector::getIterator() |
||
360 | */ |
||
361 | public function __clone() |
||
368 | |||
369 | /** |
||
370 | * Get object responsible for class instantiation. |
||
371 | * |
||
372 | * @param string $class |
||
373 | * |
||
374 | * @return InstantiatorInterface |
||
375 | */ |
||
376 | protected function instantiator(string $class): InstantiatorInterface |
||
395 | |||
396 | /** |
||
397 | * Load packed schema from memory. |
||
398 | * |
||
399 | * @return array |
||
400 | */ |
||
401 | protected function loadSchema(): array |
||
405 | |||
406 | /** |
||
407 | * Get ODM specific factory. |
||
408 | * |
||
409 | * @return FactoryInterface |
||
410 | */ |
||
411 | protected function getFactory(): FactoryInterface |
||
419 | } |
||
420 |