1 | <?php |
||
20 | class SchemaBuilder |
||
21 | { |
||
22 | /** |
||
23 | * @var DatabaseManager |
||
24 | */ |
||
25 | private $manager; |
||
26 | |||
27 | /** |
||
28 | * @var AbstractTable[] |
||
29 | */ |
||
30 | private $tables = []; |
||
31 | |||
32 | /** |
||
33 | * @var SchemaInterface[] |
||
34 | */ |
||
35 | private $schemas = []; |
||
36 | |||
37 | /** |
||
38 | * Class names of sources associated with specific class. |
||
39 | * |
||
40 | * @var array |
||
41 | */ |
||
42 | private $sources = []; |
||
43 | |||
44 | /** |
||
45 | * @param DatabaseManager $manager |
||
46 | */ |
||
47 | public function __construct(DatabaseManager $manager) |
||
51 | |||
52 | /** |
||
53 | * Add new model schema into pool. |
||
54 | * |
||
55 | * @param SchemaInterface $schema |
||
56 | * |
||
57 | * @return self|$this |
||
58 | */ |
||
59 | public function addSchema(SchemaInterface $schema): SchemaBuilder |
||
65 | |||
66 | /** |
||
67 | * @param string $class |
||
68 | * |
||
69 | * @return bool |
||
70 | */ |
||
71 | public function hasSchema(string $class): bool |
||
75 | |||
76 | /** |
||
77 | * @param string $class |
||
78 | * |
||
79 | * @return SchemaInterface |
||
80 | * |
||
81 | * @throws SchemaException |
||
82 | */ |
||
83 | public function getSchema(string $class): SchemaInterface |
||
91 | |||
92 | /** |
||
93 | * All available document schemas. |
||
94 | * |
||
95 | * @return SchemaInterface[] |
||
96 | */ |
||
97 | public function getSchemas(): array |
||
101 | |||
102 | /** |
||
103 | * Associate source class with entity class. Source will be automatically associated with given |
||
104 | * class and all classes from the same collection which extends it. |
||
105 | * |
||
106 | * @param string $class |
||
107 | * @param string $source |
||
108 | * |
||
109 | * @return SchemaBuilder |
||
110 | * |
||
111 | * @throws SchemaException |
||
112 | */ |
||
113 | public function addSource(string $class, string $source): SchemaBuilder |
||
123 | |||
124 | /** |
||
125 | * Check if given entity has associated source. |
||
126 | * |
||
127 | * @param string $class |
||
128 | * |
||
129 | * @return bool |
||
130 | */ |
||
131 | public function hasSource(string $class): bool |
||
135 | |||
136 | /** |
||
137 | * Get source associated with specific class, if any. |
||
138 | * |
||
139 | * @param string $class |
||
140 | * |
||
141 | * @return string|null |
||
142 | */ |
||
143 | public function getSource(string $class) |
||
151 | |||
152 | /** |
||
153 | * Process all added schemas and relations in order to created needed tables, indexes and etc. |
||
154 | * Attention, this method will return new instance of SchemaBuilder without affecting original |
||
155 | * object. You MUST call this method before calling packSchema() method. |
||
156 | * |
||
157 | * Attention, this methods DOES NOT write anything into database, use pushSchema() to push |
||
158 | * changes into database using automatic diff generation. You can also access list of |
||
159 | * generated/changed tables via getTables() to create your own migrations. |
||
160 | * |
||
161 | * @see packSchema() |
||
162 | * @see pushSchema() |
||
163 | * @see getTables() |
||
164 | * |
||
165 | * @return SchemaBuilder |
||
166 | * |
||
167 | * @throws SchemaException |
||
168 | */ |
||
169 | public function renderSchema(): SchemaBuilder |
||
201 | |||
202 | /** |
||
203 | * Get all defined tables, make sure to call renderSchema() first. Attention, all given tables |
||
204 | * will be returned in detached state. |
||
205 | * |
||
206 | * @return AbstractTable[] |
||
207 | * |
||
208 | * @throws SchemaException |
||
209 | */ |
||
210 | public function getTables(): array |
||
226 | |||
227 | /** |
||
228 | * Indication that tables in database require syncing before being matched with ORM models. |
||
229 | * |
||
230 | * @return bool |
||
231 | */ |
||
232 | public function hasChanges(): bool |
||
242 | |||
243 | /** |
||
244 | * Save every change made to generated tables. Method utilizes default DBAL diff mechanism, |
||
245 | * use getTables() method in order to generate your own migrations. |
||
246 | * |
||
247 | * @param LoggerInterface|null $logger |
||
248 | * |
||
249 | * @throws SchemaException |
||
250 | * @throws DBALException |
||
251 | * @throws QueryException |
||
252 | * @throws DriverException |
||
253 | */ |
||
254 | public function pushSchema(LoggerInterface $logger = null) |
||
259 | |||
260 | /** |
||
261 | * Pack declared schemas in a normalized form, make sure to call renderSchema() first. |
||
262 | * |
||
263 | * @return array |
||
264 | * |
||
265 | * @throws SchemaException |
||
266 | */ |
||
267 | public function packSchema(): array |
||
290 | |||
291 | /** |
||
292 | * Request table schema by name/database combination. |
||
293 | * |
||
294 | * @param string $table |
||
295 | * @param string|null $database |
||
296 | * @param bool $resetState When set to true current table state will be reset in order |
||
297 | * to |
||
298 | * allows model to redefine it's schema. |
||
299 | * @param bool $unique Set to true (default), to throw an exception when table |
||
300 | * already referenced by another model. |
||
301 | * |
||
302 | * @return AbstractTable Unlinked. |
||
303 | * |
||
304 | * @throws DoubleReferenceException When two records refers to same table and unique option |
||
305 | * set. |
||
306 | */ |
||
307 | protected function requestTable( |
||
336 | |||
337 | /** |
||
338 | * Update table state. |
||
339 | * |
||
340 | * @param AbstractTable $table |
||
341 | * @param string|null $database |
||
342 | */ |
||
343 | private function pushTable(AbstractTable $table, string $database = null) |
||
347 | } |