|
1
|
|
|
<?php |
|
2
|
|
|
namespace keeko\tools\services; |
|
3
|
|
|
|
|
4
|
|
|
use phootwork\collection\ArrayList; |
|
5
|
|
|
use Propel\Generator\Model\Table; |
|
6
|
|
|
use Propel\Generator\Model\Database; |
|
7
|
|
|
use Propel\Generator\Util\QuickBuilder; |
|
8
|
|
|
use phootwork\lang\Text; |
|
9
|
|
|
use keeko\core\schema\ActionSchema; |
|
10
|
|
|
use keeko\tools\utils\NamespaceResolver; |
|
11
|
|
|
use keeko\core\schema\PackageSchema; |
|
12
|
|
|
|
|
13
|
|
|
class ModelService extends AbstractService { |
|
14
|
|
|
|
|
15
|
|
|
private $models = null; |
|
|
|
|
|
|
16
|
|
|
private $schema = null; |
|
|
|
|
|
|
17
|
|
|
private $namespace = null; |
|
18
|
|
|
|
|
19
|
|
|
/** @var Database */ |
|
20
|
|
|
private $database = null; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Returns the propel schema. The three locations, where the schema is looked up in: |
|
24
|
|
|
* |
|
25
|
|
|
* 1. --schema option (if available) |
|
26
|
|
|
* 2. database/schema.xml |
|
27
|
|
|
* 3. core/database/schema.xml |
|
28
|
|
|
* |
|
29
|
|
|
* @throws \RuntimeException |
|
30
|
|
|
* @return string the path to the schema |
|
31
|
12 |
|
*/ |
|
32
|
12 |
|
public function getSchema() { |
|
33
|
12 |
|
$input = $this->io->getInput(); |
|
34
|
12 |
|
if ($this->schema === null) { |
|
35
|
12 |
|
$workDir = $this->service->getProject()->getRootPath(); |
|
36
|
|
|
$schema = null; |
|
|
|
|
|
|
37
|
12 |
|
$schemas = [ |
|
38
|
12 |
|
$input->hasOption('schema') ? $input->getOption('schema') : '', |
|
39
|
|
|
$workDir . '/database/schema.xml', |
|
40
|
12 |
|
$workDir . '/core/database/schema.xml', |
|
41
|
12 |
|
$workDir . '/vendor/keeko/core/database/schema.xml' |
|
42
|
12 |
|
]; |
|
43
|
12 |
|
foreach ($schemas as $path) { |
|
44
|
12 |
|
if (file_exists($path)) { |
|
45
|
|
|
$schema = $path; |
|
46
|
12 |
|
break; |
|
47
|
12 |
|
} |
|
48
|
|
|
} |
|
49
|
12 |
|
$this->schema = $schema; |
|
50
|
|
|
|
|
51
|
|
|
if ($schema === null) { |
|
52
|
|
|
$locations = implode(', ', $schemas); |
|
53
|
12 |
|
throw new \RuntimeException(sprintf('Can\'t find schema in these locations: %s', $locations)); |
|
54
|
|
|
} |
|
55
|
12 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
return $this->schema; |
|
58
|
3 |
|
} |
|
59
|
3 |
|
|
|
60
|
|
|
public function isCoreSchema() { |
|
61
|
|
|
return strpos($this->getSchema(), 'core') !== false; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function hasSchema() { |
|
65
|
|
|
$vendorName = $this->packageService->getPackage()->getVendor(); |
|
66
|
|
|
return $this->getSchema() !== null && ($this->isCoreSchema() ? $vendorName == 'keeko' : true); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Returns the propel database |
|
71
|
|
|
* |
|
72
|
12 |
|
* @return Database |
|
73
|
12 |
|
*/ |
|
74
|
12 |
|
public function getDatabase() { |
|
75
|
12 |
|
if ($this->database === null) { |
|
76
|
12 |
|
$builder = new QuickBuilder(); |
|
77
|
12 |
|
$builder->setSchema(file_get_contents($this->getSchema())); |
|
78
|
|
|
$this->database = $builder->getDatabase(); |
|
79
|
12 |
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $this->database; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Returns the tableName for a given name |
|
86
|
|
|
* |
|
87
|
|
|
* @param String $name tableName or modelName |
|
88
|
11 |
|
* @return String tableName |
|
89
|
11 |
|
*/ |
|
90
|
11 |
|
public function getTableName($name) { |
|
91
|
11 |
|
$db = $this->getDatabase(); |
|
92
|
11 |
|
if (!Text::create($name)->startsWith($db->getTablePrefix())) { |
|
93
|
|
|
$name = $db->getTablePrefix() . $name; |
|
94
|
11 |
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $name; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Returns all model names |
|
101
|
|
|
* |
|
102
|
|
|
* @return String[] an array of modelName |
|
103
|
|
|
*/ |
|
104
|
|
|
public function getModelNames() { |
|
105
|
|
|
$names = []; |
|
|
|
|
|
|
106
|
|
|
$database = $this->getDatabase(); |
|
107
|
|
|
foreach ($database->getTables() as $table) { |
|
108
|
|
|
$names[] = $table->getOriginCommonName(); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return $names; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Returns the propel models from the database, where table namespace matches package namespace |
|
116
|
|
|
* |
|
117
|
1 |
|
* @return ArrayList<Table> |
|
|
|
|
|
|
118
|
1 |
|
*/ |
|
119
|
1 |
|
public function getModels() { |
|
|
|
|
|
|
120
|
1 |
|
if ($this->models === null) { |
|
121
|
|
|
$namespace = str_replace('\\\\', '\\', $this->getRootNamespace() . '\\model'); |
|
122
|
1 |
|
$propel = $this->getDatabase(); |
|
|
|
|
|
|
123
|
|
|
|
|
124
|
1 |
|
$this->models = new ArrayList(); |
|
125
|
1 |
|
|
|
126
|
1 |
|
foreach ($propel->getTables() as $table) { |
|
127
|
1 |
|
if (!$table->isCrossRef() && $table->getNamespace() == $namespace) { |
|
128
|
1 |
|
$this->models->add($table); |
|
129
|
1 |
|
} |
|
130
|
|
|
} |
|
131
|
1 |
|
} |
|
132
|
|
|
|
|
133
|
|
|
return $this->models; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Returns the model for the given name |
|
138
|
|
|
* |
|
139
|
|
|
* @param String $name modelName or tableName |
|
140
|
7 |
|
* @return Table |
|
141
|
7 |
|
*/ |
|
142
|
7 |
|
public function getModel($name) { |
|
143
|
|
|
$tableName = $this->getTableName($name); |
|
144
|
|
|
$db = $this->getDatabase(); |
|
|
|
|
|
|
145
|
|
|
// echo $db->getNamespace(); |
|
|
|
|
|
|
146
|
|
|
// foreach ($db->getTables() as $table) { |
|
147
|
7 |
|
// echo $table->getName(); |
|
148
|
|
|
// } |
|
149
|
7 |
|
$table = $db->getTable($tableName); |
|
150
|
|
|
|
|
151
|
|
|
return $table; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Returns the model names for a given package |
|
156
|
|
|
* |
|
157
|
|
|
* @param PackageSchema $package a package to search models for, if omitted global package is used |
|
158
|
8 |
|
* @return array array with string of model names |
|
159
|
8 |
|
*/ |
|
160
|
|
|
public function getPackageModelNames(PackageSchema $package = null) { |
|
161
|
|
|
if ($package === null) { |
|
162
|
|
|
$package = $this->packageService->getPackage(); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
$models = []; |
|
166
|
|
|
// if this is a core-module, find the related model |
|
167
|
1 |
|
if ($package->getVendor() == 'keeko' && $this->isCoreSchema()) { |
|
168
|
1 |
|
$model = $package->getName(); |
|
169
|
1 |
|
if ($this->hasModel($model)) { |
|
170
|
1 |
|
$models []= $model; |
|
|
|
|
|
|
171
|
1 |
|
} |
|
172
|
1 |
|
} |
|
173
|
1 |
|
|
|
174
|
1 |
|
// anyway, generate all |
|
175
|
1 |
|
else { |
|
176
|
1 |
|
foreach ($this->getModels() as $model) { |
|
177
|
|
|
$models []= $model->getOriginCommonName(); |
|
|
|
|
|
|
178
|
1 |
|
} |
|
179
|
1 |
|
} |
|
180
|
|
|
|
|
181
|
1 |
|
return $models; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Checks whether the given model exists |
|
186
|
|
|
* |
|
187
|
|
|
* @param String $name tableName or modelName |
|
188
|
|
|
* @return boolean |
|
189
|
|
|
*/ |
|
190
|
|
|
public function hasModel($name) { |
|
191
|
|
|
return $this->getDatabase()->hasTable($this->getTableName($name), true); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Returns the root namespace for this package |
|
196
|
|
|
* |
|
197
|
|
|
* @return string the namespace |
|
198
|
|
|
*/ |
|
199
|
|
|
public function getRootNamespace() { |
|
200
|
|
|
if ($this->namespace === null) { |
|
201
|
|
|
$input = $this->io->getInput(); |
|
202
|
|
|
$ns = $input->hasOption('namespace') |
|
|
|
|
|
|
203
|
|
|
? $input->getOption('namespace') |
|
204
|
|
|
: null; |
|
205
|
|
|
if ($ns === null) { |
|
206
|
|
|
$package = $this->service->getPackageService()->getPackage(); |
|
207
|
|
|
$ns = NamespaceResolver::getNamespace('src', $package); |
|
|
|
|
|
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
$this->namespace = $ns; |
|
211
|
|
|
} |
|
212
|
10 |
|
|
|
213
|
10 |
|
return $this->namespace; |
|
214
|
10 |
|
} |
|
215
|
10 |
|
|
|
216
|
10 |
|
/** |
|
217
|
10 |
|
* Retrieves the model name for the given package in two steps: |
|
218
|
10 |
|
* |
|
219
|
|
|
* 1. Check if it is passed as cli parameter |
|
220
|
|
|
* 2. Retrieve it from the package name |
|
221
|
|
|
* |
|
222
|
|
|
* @return String |
|
223
|
|
|
*/ |
|
224
|
|
|
public function getModelName() { |
|
225
|
|
|
$input = $this->io->getInput(); |
|
|
|
|
|
|
226
|
|
|
$modelName = $input->hasOption('model') ? $input->getOption('model') : null; |
|
227
|
5 |
|
if ($modelName === null && $this->isCoreSchema()) { |
|
228
|
5 |
|
$package = $this->service->getPackageService()->getPackage(); |
|
|
|
|
|
|
229
|
5 |
|
$packageName = $package->getName(); |
|
230
|
5 |
|
|
|
231
|
5 |
|
if ($this->hasModel($packageName)) { |
|
232
|
|
|
$modelName = $packageName; |
|
233
|
5 |
|
} |
|
234
|
|
|
} |
|
235
|
|
|
return $modelName; |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* Parses the model name from a given action name |
|
240
|
|
|
* |
|
241
|
|
|
* @param ActionSchema $action |
|
242
|
|
|
* @return String modelName |
|
243
|
|
|
*/ |
|
244
|
|
View Code Duplication |
public function getModelNameByAction(ActionSchema $action) { |
|
|
|
|
|
|
245
|
|
|
$actionName = $action->getName(); |
|
246
|
|
|
$modelName = null; |
|
|
|
|
|
|
247
|
|
|
if (($pos = strpos($actionName, '-')) !== false) { |
|
248
|
|
|
$modelName = substr($actionName, 0, $pos); |
|
249
|
|
|
} |
|
250
|
|
|
return $modelName; |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* Returns the full model object name, including namespace |
|
255
|
|
|
* |
|
256
|
|
|
* @param ActionSchema $action |
|
257
|
|
|
* @return String fullModelObjectName |
|
258
|
|
|
*/ |
|
259
|
|
|
public function getFullModelObjectName(ActionSchema $action) { |
|
260
|
|
|
$database = $this->getDatabase(); |
|
|
|
|
|
|
261
|
|
|
$modelName = $this->getModelNameByAction($action); |
|
|
|
|
|
|
262
|
5 |
|
$model = $this->getModel($modelName); |
|
|
|
|
|
|
263
|
5 |
|
$modelObjectName = $model->getPhpName(); |
|
264
|
5 |
|
|
|
265
|
|
|
return $database->getNamespace() . '\\' . $modelObjectName; |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* Returns the operation (verb) of the action (if existent) |
|
270
|
|
|
* |
|
271
|
|
|
* @param ActionSchema $action |
|
272
|
|
|
* @return string|null |
|
273
|
|
|
*/ |
|
274
|
|
View Code Duplication |
public function getOperationByAction(ActionSchema $action) { |
|
|
|
|
|
|
275
|
|
|
$actionName = $action->getName(); |
|
276
|
|
|
$operation = null; |
|
|
|
|
|
|
277
|
|
|
if (($pos = strpos($actionName, '-')) !== false) { |
|
278
|
|
|
$operation = substr($actionName, $pos + 1); |
|
279
|
|
|
} |
|
280
|
|
|
return $operation; |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* Returns wether the given action refers to a model. |
|
285
|
|
|
* |
|
286
|
|
|
* Examples: |
|
287
|
|
|
* |
|
288
|
|
|
* Action: user-create => model: user |
|
289
|
|
|
* Action: recover-password => no model |
|
290
|
|
|
* |
|
291
|
|
|
* @param ActionSchema $action |
|
292
|
|
|
* @return boolean |
|
293
|
|
|
*/ |
|
294
|
|
|
public function isModelAction(ActionSchema $action) { |
|
295
|
|
|
$modelName = $this->getModelNameByAction($action); |
|
296
|
|
|
return $this->hasModel($modelName); |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
/** |
|
300
|
|
|
* Returns whether this is a crud operation action |
|
301
|
|
|
* (create, read, update, delete, list) |
|
302
|
|
|
* |
|
303
|
|
|
* @param ActionSchema $action |
|
304
|
|
|
* @return boolean |
|
305
|
|
|
*/ |
|
306
|
|
|
public function isCrudAction(ActionSchema $action) { |
|
307
|
|
|
$operation = $this->getOperationByAction($action); |
|
308
|
|
|
|
|
309
|
|
|
return in_array($operation, ['create', 'read', 'update', 'delete', 'list']); |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
/** |
|
313
|
|
|
* Returns all model relationships. |
|
314
|
|
|
* |
|
315
|
|
|
* |
|
316
|
|
|
* The returned array looks like: |
|
317
|
|
|
* [ |
|
318
|
|
|
* 'one' => [ |
|
319
|
|
|
* [ |
|
320
|
|
|
* 'type' => 'one', |
|
321
|
|
|
* 'fk' => $fk |
|
322
|
|
|
* ], |
|
323
|
|
|
* [ |
|
324
|
|
|
* 'type' => 'one', |
|
325
|
|
|
* 'fk' => $fk2 |
|
326
|
|
|
* ], |
|
327
|
|
|
* ... |
|
328
|
|
|
* ], |
|
329
|
|
|
* 'many' => [ |
|
330
|
|
|
* [ |
|
331
|
|
|
* 'type' => 'many', |
|
332
|
|
|
* 'fk' => $fk3, |
|
333
|
|
|
* 'cfk' => $cfk |
|
334
|
|
|
* ], |
|
335
|
|
|
* [ |
|
336
|
|
|
* 'type' => 'many', |
|
337
|
|
|
* 'fk' => $fk4, |
|
338
|
|
|
* 'cfk' => $cfk2 |
|
339
|
|
|
* ], |
|
340
|
|
|
* ... |
|
341
|
|
|
* ], |
|
342
|
|
|
* 'all' => [...] // both of above |
|
343
|
|
|
* ] |
|
344
|
|
|
* |
|
345
|
|
|
* |
|
346
|
|
|
* @param Table $model |
|
347
|
|
|
* @return array |
|
348
|
|
|
*/ |
|
349
|
|
|
public function getRelationships(Table $model) { |
|
350
|
|
|
$all = []; |
|
351
|
|
|
|
|
352
|
|
|
// to-one relationships |
|
353
|
|
|
$one = []; |
|
354
|
|
|
$fks = $model->getForeignKeys(); |
|
355
|
|
|
foreach ($fks as $fk) { |
|
356
|
|
|
$item = [ |
|
|
|
|
|
|
357
|
|
|
'type' => 'one', |
|
358
|
|
|
'fk' => $fk |
|
359
|
|
|
]; |
|
360
|
|
|
$one[] = $item; |
|
361
|
|
|
$all[] = $item; |
|
362
|
|
|
} |
|
363
|
|
|
|
|
364
|
|
|
// to-many relationships |
|
365
|
|
|
$many = []; |
|
366
|
|
|
$cfks = $model->getCrossFks(); |
|
367
|
|
|
foreach ($cfks as $cfk) { |
|
368
|
|
|
foreach ($cfk->getMiddleTable()->getForeignKeys() as $fk) { |
|
369
|
|
|
if ($fk->getForeignTable() != $model) { |
|
370
|
|
|
$item = [ |
|
|
|
|
|
|
371
|
|
|
'type' => 'many', |
|
372
|
|
|
'fk' => $fk, |
|
373
|
|
|
'cfk' => $cfk |
|
374
|
|
|
]; |
|
375
|
|
|
$many[] = $item; |
|
376
|
|
|
$all[] = $item; |
|
|
|
|
|
|
377
|
|
|
break; |
|
378
|
|
|
} |
|
379
|
|
|
} |
|
380
|
|
|
} |
|
381
|
|
|
|
|
382
|
|
|
return [ |
|
383
|
|
|
'one' => $one, |
|
384
|
|
|
'many' => $many, |
|
385
|
|
|
'all' => $all |
|
386
|
|
|
]; |
|
387
|
|
|
} |
|
388
|
|
|
} |
|
389
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.