1 | <?php |
||
22 | class SchemaBuilder |
||
23 | { |
||
24 | /** |
||
25 | * @var DatabaseManager |
||
26 | */ |
||
27 | private $manager; |
||
28 | |||
29 | /** |
||
30 | * @var RelationManager |
||
31 | */ |
||
32 | private $relations; |
||
33 | |||
34 | /** |
||
35 | * @var AbstractTable[] |
||
36 | */ |
||
37 | private $tables = []; |
||
38 | |||
39 | /** |
||
40 | * @var SchemaInterface[] |
||
41 | */ |
||
42 | private $schemas = []; |
||
43 | |||
44 | /** |
||
45 | * Class names of sources associated with specific class. |
||
46 | * |
||
47 | * @var array |
||
48 | */ |
||
49 | private $sources = []; |
||
50 | |||
51 | /** |
||
52 | * @param DatabaseManager $manager |
||
53 | * @param RelationManager $relations |
||
54 | */ |
||
55 | public function __construct(DatabaseManager $manager, RelationManager $relations) |
||
60 | |||
61 | /** |
||
62 | * Add new model schema into pool. |
||
63 | * |
||
64 | * @param SchemaInterface $schema |
||
65 | * |
||
66 | * @return self|$this |
||
67 | */ |
||
68 | public function addSchema(SchemaInterface $schema): SchemaBuilder |
||
74 | |||
75 | /** |
||
76 | * @param string $class |
||
77 | * |
||
78 | * @return bool |
||
79 | */ |
||
80 | public function hasSchema(string $class): bool |
||
84 | |||
85 | /** |
||
86 | * @param string $class |
||
87 | * |
||
88 | * @return SchemaInterface |
||
89 | * |
||
90 | * @throws SchemaException |
||
91 | */ |
||
92 | public function getSchema(string $class): SchemaInterface |
||
100 | |||
101 | /** |
||
102 | * All available document schemas. |
||
103 | * |
||
104 | * @return SchemaInterface[] |
||
105 | */ |
||
106 | public function getSchemas(): array |
||
110 | |||
111 | /** |
||
112 | * Associate source class with entity class. Source will be automatically associated with given |
||
113 | * class and all classes from the same collection which extends it. |
||
114 | * |
||
115 | * @param string $class |
||
116 | * @param string $source |
||
117 | * |
||
118 | * @return SchemaBuilder |
||
119 | * |
||
120 | * @throws SchemaException |
||
121 | */ |
||
122 | public function addSource(string $class, string $source): SchemaBuilder |
||
132 | |||
133 | /** |
||
134 | * Check if given entity has associated source. |
||
135 | * |
||
136 | * @param string $class |
||
137 | * |
||
138 | * @return bool |
||
139 | */ |
||
140 | public function hasSource(string $class): bool |
||
144 | |||
145 | /** |
||
146 | * Get source associated with specific class, if any. |
||
147 | * |
||
148 | * @param string $class |
||
149 | * |
||
150 | * @return string|null |
||
151 | */ |
||
152 | public function getSource(string $class) |
||
160 | |||
161 | /** |
||
162 | * Process all added schemas and relations in order to created needed tables, indexes and etc. |
||
163 | * Attention, this method will return new instance of SchemaBuilder without affecting original |
||
164 | * object. You MUST call this method before calling packSchema() method. |
||
165 | * |
||
166 | * Attention, this methods DOES NOT write anything into database, use pushSchema() to push |
||
167 | * changes into database using automatic diff generation. You can also access list of |
||
168 | * generated/changed tables via getTables() to create your own migrations. |
||
169 | * |
||
170 | * @see packSchema() |
||
171 | * @see pushSchema() |
||
172 | * @see getTables() |
||
173 | * |
||
174 | * @return SchemaBuilder |
||
175 | * |
||
176 | * @throws SchemaException |
||
177 | */ |
||
178 | public function renderSchema(): SchemaBuilder |
||
196 | |||
197 | /** |
||
198 | * Request table schema by name/database combination. Attention, you can only save table by |
||
199 | * pushing it back using pushTable() method. |
||
200 | * |
||
201 | * @see setTable() |
||
202 | * |
||
203 | * @param string $table |
||
204 | * @param string|null $database |
||
205 | * @param bool $resetState When set to true current table state will be reset in order |
||
206 | * to allow model to redefine it's schema. |
||
207 | * @param bool $unique Set to true (default), to throw an exception when table |
||
208 | * already referenced by another model. |
||
209 | * |
||
210 | * @return AbstractTable Unlinked. |
||
211 | * |
||
212 | * @throws DoubleReferenceException When two records refers to same table and unique option |
||
213 | * set. |
||
214 | */ |
||
215 | public function requestTable( |
||
244 | |||
245 | /** |
||
246 | * Get all defined tables, make sure to call renderSchema() first. Attention, all given tables |
||
247 | * will be returned in detached state. |
||
248 | * |
||
249 | * @return AbstractTable[] |
||
250 | * |
||
251 | * @throws SchemaException |
||
252 | */ |
||
253 | public function getTables(): array |
||
269 | |||
270 | /** |
||
271 | * Indication that tables in database require syncing before being matched with ORM models. |
||
272 | * |
||
273 | * @return bool |
||
274 | */ |
||
275 | public function hasChanges(): bool |
||
285 | |||
286 | /** |
||
287 | * Save every change made to generated tables. Method utilizes default DBAL diff mechanism, |
||
288 | * use getTables() method in order to generate your own migrations. |
||
289 | * |
||
290 | * @param LoggerInterface|null $logger |
||
291 | * |
||
292 | * @throws SchemaException |
||
293 | * @throws DBALException |
||
294 | * @throws QueryException |
||
295 | * @throws DriverException |
||
296 | */ |
||
297 | public function pushSchema(LoggerInterface $logger = null) |
||
302 | |||
303 | /** |
||
304 | * Pack declared schemas in a normalized form, make sure to call renderSchema() first. |
||
305 | * |
||
306 | * @return array |
||
307 | * |
||
308 | * @throws SchemaException |
||
309 | */ |
||
310 | public function packSchema(): array |
||
344 | |||
345 | /** |
||
346 | * Walk thought schemas and define structure of associated tables. |
||
347 | * |
||
348 | * @throws SchemaException |
||
349 | * @throws DBALException |
||
350 | */ |
||
351 | protected function renderModels() |
||
378 | |||
379 | /** |
||
380 | * Walk thought all record schemas, fetch and declare needed relations using relation manager. |
||
381 | * |
||
382 | * @throws SchemaException |
||
383 | * @throws DefinitionException |
||
384 | */ |
||
385 | protected function renderRelations() |
||
415 | |||
416 | /** |
||
417 | * Update table state. |
||
418 | * |
||
419 | * @param AbstractTable $schema |
||
420 | * @param string|null $database |
||
421 | * |
||
422 | * @throws SchemaException |
||
423 | */ |
||
424 | protected function setTable(AbstractTable $schema, string $database = null) |
||
435 | } |