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
|
|
|
|
12
|
|
|
class ModelService extends AbstractService { |
13
|
|
|
|
14
|
|
|
private $models = null; |
|
|
|
|
15
|
|
|
private $schema = null; |
|
|
|
|
16
|
|
|
private $namespace = null; |
17
|
|
|
|
18
|
|
|
/** @var Database */ |
19
|
|
|
private $database = null; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Returns the propel schema. The three locations, where the schema is looked up in: |
23
|
|
|
* |
24
|
|
|
* 1. --schema option (if available) |
25
|
|
|
* 2. database/schema.xml |
26
|
|
|
* 3. core/database/schema.xml |
27
|
|
|
* |
28
|
|
|
* @throws \RuntimeException |
29
|
|
|
* @return string the path to the schema |
30
|
|
|
*/ |
31
|
12 |
|
public function getSchema() { |
32
|
12 |
|
$input = $this->io->getInput(); |
33
|
12 |
|
if ($this->schema === null) { |
34
|
12 |
|
$workDir = $this->service->getProject()->getRootPath(); |
35
|
12 |
|
$schema = null; |
|
|
|
|
36
|
|
|
$schemas = [ |
37
|
12 |
|
$input->hasOption('schema') ? $input->getOption('schema') : '', |
38
|
12 |
|
$workDir . '/database/schema.xml', |
39
|
|
|
$workDir . '/core/database/schema.xml' |
40
|
12 |
|
]; |
41
|
12 |
|
foreach ($schemas as $path) { |
42
|
12 |
|
if (file_exists($path)) { |
43
|
12 |
|
$schema = $path; |
44
|
12 |
|
break; |
45
|
|
|
} |
46
|
12 |
|
} |
47
|
12 |
|
$this->schema = $schema; |
48
|
|
|
|
49
|
12 |
|
if ($schema === null) { |
50
|
|
|
$locations = implode(', ', $schemas); |
51
|
|
|
throw new \RuntimeException(sprintf('Can\'t find schema in these locations: %s', $locations)); |
52
|
|
|
} |
53
|
12 |
|
} |
54
|
|
|
|
55
|
12 |
|
return $this->schema; |
56
|
|
|
} |
57
|
|
|
|
58
|
3 |
|
public function isCoreSchema() { |
59
|
3 |
|
return strpos($this->getSchema(), 'core') !== false; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function hasSchema() { |
63
|
|
|
$vendorName = $this->packageService->getPackage()->getVendor(); |
64
|
|
|
return $this->getSchema() !== null && ($this->isCoreSchema() ? $vendorName == 'keeko' : true); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Returns the propel database |
69
|
|
|
* |
70
|
|
|
* @return Database |
71
|
|
|
*/ |
72
|
12 |
|
public function getDatabase() { |
73
|
12 |
|
if ($this->database === null) { |
74
|
12 |
|
$builder = new QuickBuilder(); |
75
|
12 |
|
$builder->setSchema(file_get_contents($this->getSchema())); |
76
|
12 |
|
$this->database = $builder->getDatabase(); |
77
|
12 |
|
} |
78
|
|
|
|
79
|
12 |
|
return $this->database; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Returns the tableName for a given name |
84
|
|
|
* |
85
|
|
|
* @param String $name tableName or modelName |
86
|
|
|
* @return String tableName |
87
|
|
|
*/ |
88
|
11 |
|
public function getTableName($name) { |
89
|
11 |
|
$db = $this->getDatabase(); |
90
|
11 |
|
if (!Text::create($name)->startsWith($db->getTablePrefix())) { |
91
|
11 |
|
$name = $db->getTablePrefix() . $name; |
92
|
11 |
|
} |
93
|
|
|
|
94
|
11 |
|
return $name; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Returns all model names |
99
|
|
|
* |
100
|
|
|
* @return String[] an array of modelName |
101
|
|
|
*/ |
102
|
|
|
public function getModelNames() { |
103
|
|
|
$names = []; |
|
|
|
|
104
|
|
|
$database = $this->getDatabase(); |
105
|
|
|
foreach ($database->getTables() as $table) { |
106
|
|
|
$names[] = $table->getOriginCommonName(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $names; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Returns the propel models from the database, where table namespace matches package namespace |
114
|
|
|
* |
115
|
|
|
* @return ArrayList<Table> |
|
|
|
|
116
|
|
|
*/ |
117
|
1 |
|
public function getModels() { |
|
|
|
|
118
|
1 |
|
if ($this->models === null) { |
119
|
1 |
|
$namespace = str_replace('\\\\', '\\', $this->getRootNamespace() . '\\model'); |
120
|
1 |
|
$propel = $this->getDatabase(); |
|
|
|
|
121
|
|
|
|
122
|
1 |
|
$this->models = new ArrayList(); |
123
|
|
|
|
124
|
1 |
|
foreach ($propel->getTables() as $table) { |
125
|
1 |
|
if (!$table->isCrossRef() && $table->getNamespace() == $namespace) { |
126
|
1 |
|
$this->models->add($table); |
127
|
1 |
|
} |
128
|
1 |
|
} |
129
|
1 |
|
} |
130
|
|
|
|
131
|
1 |
|
return $this->models; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Returns the model for the given name |
136
|
|
|
* |
137
|
|
|
* @param String $name modelName or tableName |
138
|
|
|
* @return Table |
139
|
|
|
*/ |
140
|
7 |
|
public function getModel($name) { |
141
|
7 |
|
$tableName = $this->getTableName($name); |
142
|
7 |
|
$db = $this->getDatabase(); |
|
|
|
|
143
|
|
|
// echo $db->getNamespace(); |
|
|
|
|
144
|
|
|
// foreach ($db->getTables() as $table) { |
145
|
|
|
// echo $table->getName(); |
146
|
|
|
// } |
147
|
7 |
|
$table = $db->getTable($tableName); |
148
|
|
|
|
149
|
7 |
|
return $table; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Checks whether the given model exists |
154
|
|
|
* |
155
|
|
|
* @param String $name tableName or modelName |
156
|
|
|
* @return boolean |
157
|
|
|
*/ |
158
|
8 |
|
public function hasModel($name) { |
159
|
8 |
|
return $this->getDatabase()->hasTable($this->getTableName($name), true); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Returns the root namespace for this package |
164
|
|
|
* |
165
|
|
|
* @return string the namespace |
166
|
|
|
*/ |
167
|
1 |
|
public function getRootNamespace() { |
168
|
1 |
|
if ($this->namespace === null) { |
169
|
1 |
|
$input = $this->io->getInput(); |
170
|
1 |
|
$ns = $input->hasOption('namespace') |
|
|
|
|
171
|
1 |
|
? $input->getOption('namespace') |
172
|
1 |
|
: null; |
173
|
1 |
|
if ($ns === null) { |
174
|
1 |
|
$package = $this->service->getPackageService()->getPackage(); |
175
|
1 |
|
$ns = NamespaceResolver::getNamespace('src', $package); |
|
|
|
|
176
|
1 |
|
} |
177
|
|
|
|
178
|
1 |
|
$this->namespace = $ns; |
179
|
1 |
|
} |
180
|
|
|
|
181
|
1 |
|
return $this->namespace; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Retrieves the model name for the given package in two steps: |
186
|
|
|
* |
187
|
|
|
* 1. Check if it is passed as cli parameter |
188
|
|
|
* 2. Retrieve it from the package name |
189
|
|
|
* |
190
|
|
|
* @return String |
191
|
|
|
*/ |
192
|
|
|
public function getModelName() { |
193
|
|
|
$input = $this->io->getInput(); |
|
|
|
|
194
|
|
|
$modelName = $input->hasOption('model') ? $input->getOption('model') : null; |
195
|
|
|
if ($modelName === null && $this->isCoreSchema()) { |
196
|
|
|
$package = $this->service->getPackageService()->getPackage(); |
|
|
|
|
197
|
|
|
$packageName = $package->getName(); |
198
|
|
|
|
199
|
|
|
if ($this->hasModel($packageName)) { |
200
|
|
|
$modelName = $packageName; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
return $modelName; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Parses the model name from a given action name |
208
|
|
|
* |
209
|
|
|
* @param ActionSchema $action |
210
|
|
|
* @return String modelName |
211
|
|
|
*/ |
212
|
10 |
View Code Duplication |
public function getModelNameByAction(ActionSchema $action) { |
|
|
|
|
213
|
10 |
|
$actionName = $action->getName(); |
214
|
10 |
|
$modelName = null; |
|
|
|
|
215
|
10 |
|
if (($pos = strpos($actionName, '-')) !== false) { |
216
|
10 |
|
$modelName = substr($actionName, 0, $pos); |
217
|
10 |
|
} |
218
|
10 |
|
return $modelName; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Returns the full model object name, including namespace |
223
|
|
|
* |
224
|
|
|
* @param ActionSchema $action |
225
|
|
|
* @return String fullModelObjectName |
226
|
|
|
*/ |
227
|
5 |
|
public function getFullModelObjectName(ActionSchema $action) { |
228
|
5 |
|
$database = $this->getDatabase(); |
|
|
|
|
229
|
5 |
|
$modelName = $this->getModelNameByAction($action); |
|
|
|
|
230
|
5 |
|
$model = $this->getModel($modelName); |
|
|
|
|
231
|
5 |
|
$modelObjectName = $model->getPhpName(); |
232
|
|
|
|
233
|
5 |
|
return $database->getNamespace() . '\\' . $modelObjectName; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Returns the operation (verb) of the action (if existent) |
238
|
|
|
* |
239
|
|
|
* @param ActionSchema $action |
240
|
|
|
* @return string|null |
241
|
|
|
*/ |
242
|
|
View Code Duplication |
public function getOperationByAction(ActionSchema $action) { |
|
|
|
|
243
|
|
|
$actionName = $action->getName(); |
244
|
|
|
$operation = null; |
|
|
|
|
245
|
|
|
if (($pos = strpos($actionName, '-')) !== false) { |
246
|
|
|
$operation = substr($actionName, $pos + 1); |
247
|
|
|
} |
248
|
|
|
return $operation; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Returns wether the given action refers to a model. |
253
|
|
|
* |
254
|
|
|
* Examples: |
255
|
|
|
* |
256
|
|
|
* Action: user-create => model: user |
257
|
|
|
* Action: recover-password => no model |
258
|
|
|
* |
259
|
|
|
* @param ActionSchema $action |
260
|
|
|
* @return boolean |
261
|
|
|
*/ |
262
|
5 |
|
public function isModelAction(ActionSchema $action) { |
263
|
5 |
|
$modelName = $this->getModelNameByAction($action); |
264
|
5 |
|
return $this->hasModel($modelName); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Returns whether this is a crud operation action |
269
|
|
|
* (create, read, update, delete, list) |
270
|
|
|
* |
271
|
|
|
* @param ActionSchema $action |
272
|
|
|
* @return boolean |
273
|
|
|
*/ |
274
|
|
|
public function isCrudAction(ActionSchema $action) { |
275
|
|
|
$operation = $this->getOperationByAction($action); |
276
|
|
|
|
277
|
|
|
return in_array($operation, ['create', 'read', 'update', 'delete', 'list']); |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
|
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.