|
1
|
|
|
<?php |
|
2
|
|
|
namespace keeko\tools\services; |
|
3
|
|
|
|
|
4
|
|
|
use keeko\framework\schema\ActionSchema; |
|
5
|
|
|
use keeko\framework\schema\PackageSchema; |
|
6
|
|
|
use keeko\tools\model\Project; |
|
7
|
|
|
use keeko\tools\model\Relationship; |
|
8
|
|
|
use keeko\tools\model\Relationships; |
|
9
|
|
|
use phootwork\collection\Map; |
|
10
|
|
|
use phootwork\collection\Set; |
|
11
|
|
|
use phootwork\file\Path; |
|
12
|
|
|
use Propel\Generator\Model\Database; |
|
13
|
|
|
use Propel\Generator\Model\Table; |
|
14
|
|
|
|
|
15
|
|
|
class ModelService extends AbstractService { |
|
16
|
|
|
|
|
17
|
|
|
private $models = null; |
|
|
|
|
|
|
18
|
|
|
private $schema = null; |
|
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
/** @var Database */ |
|
21
|
|
|
private $database = null; |
|
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
private $relationships = null; |
|
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
private $reader = null; |
|
26
|
|
|
|
|
27
|
|
|
public function read(Project $project = null) { |
|
28
|
|
|
if ($project === null) { |
|
29
|
|
|
if ($this->project->hasSchemaFile()) { |
|
30
|
|
|
$project = $this->project; |
|
31
|
12 |
|
} else { |
|
32
|
12 |
|
$project = new Project($this->project->getRootPath() . '/vendor/keeko/core'); |
|
33
|
12 |
|
} |
|
34
|
12 |
|
} |
|
35
|
12 |
|
$this->reader = new ModelReader($project, $this->service); |
|
36
|
|
|
} |
|
37
|
12 |
|
|
|
38
|
12 |
|
/** |
|
39
|
|
|
* @return ModelReader |
|
40
|
12 |
|
*/ |
|
41
|
12 |
|
private function getReader() { |
|
42
|
12 |
|
if ($this->reader === null) { |
|
43
|
12 |
|
$this->read(); |
|
44
|
12 |
|
} |
|
45
|
|
|
return $this->reader; |
|
46
|
12 |
|
} |
|
47
|
12 |
|
|
|
48
|
|
|
/** |
|
49
|
12 |
|
* Returns the propel schema. The three locations, where the schema is looked up in: |
|
50
|
|
|
* |
|
51
|
|
|
* @return string|null the path to the schema |
|
52
|
|
|
*/ |
|
53
|
12 |
|
public function getSchema() { |
|
54
|
|
|
if ($this->getReader()->getProject()->hasSchemaFile()) { |
|
55
|
12 |
|
return $this->getReader()->getProject()->getSchemaFileName(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
3 |
|
return null; |
|
59
|
3 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function isCoreSchema() { |
|
62
|
|
|
return $this->getReader()->getProject()->getPackage()->getFullName() == 'keeko/core'; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function hasSchema() { |
|
66
|
|
|
return $this->getReader()->getProject()->hasSchemaFile(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Returns the propel database |
|
71
|
|
|
* |
|
72
|
12 |
|
* @return Database |
|
73
|
12 |
|
*/ |
|
74
|
12 |
|
public function getDatabase() { |
|
75
|
12 |
|
return $this->getReader()->getDatabase(); |
|
76
|
12 |
|
} |
|
77
|
12 |
|
|
|
78
|
|
|
/** |
|
79
|
12 |
|
* Returns the tableName for a given name |
|
80
|
|
|
* |
|
81
|
|
|
* @param String $name tableName or modelName |
|
82
|
|
|
* @return String tableName |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getTableName($name) { |
|
85
|
|
|
return $this->getReader()->getTableName($name); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
11 |
|
/** |
|
89
|
11 |
|
* Returns all model names |
|
90
|
11 |
|
* |
|
91
|
11 |
|
* @return Set |
|
92
|
11 |
|
*/ |
|
93
|
|
|
public function getModelNames() { |
|
94
|
11 |
|
return $this->getReader()->getModelNames(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Returns the propel models from the database, where table namespace matches package namespace |
|
99
|
|
|
* |
|
100
|
|
|
* @return Map |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getModels() { |
|
103
|
|
|
return $this->getReader()->getModels(); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Returns the model for the given name |
|
108
|
|
|
* |
|
109
|
|
|
* @param String $name modelName or tableName |
|
110
|
|
|
* @return Table |
|
111
|
|
|
*/ |
|
112
|
|
|
public function getModel($name) { |
|
113
|
|
|
return $this->getReader()->getModel($name); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
// /** |
|
117
|
1 |
|
// * Returns the model names for a given package |
|
118
|
1 |
|
// * |
|
119
|
1 |
|
// * @param PackageSchema $package a package to search models for, if omitted global package is used |
|
120
|
1 |
|
// * @return array array with string of model names |
|
121
|
|
|
// */ |
|
122
|
1 |
|
// public function getPackageModelNames(PackageSchema $package = null) { |
|
123
|
|
|
// if ($package === null) { |
|
124
|
1 |
|
// $package = $this->packageService->getPackage(); |
|
125
|
1 |
|
// } |
|
126
|
1 |
|
|
|
127
|
1 |
|
// $models = []; |
|
|
|
|
|
|
128
|
1 |
|
// // if this is a core-module, find the related model |
|
129
|
1 |
|
// if ($package->getVendor() == 'keeko' && $this->isCoreSchema()) { |
|
130
|
|
|
// $model = $package->getName(); |
|
131
|
1 |
|
// if ($this->hasModel($model)) { |
|
132
|
|
|
// $models [] = $model; |
|
133
|
|
|
// } |
|
134
|
|
|
// } |
|
135
|
|
|
|
|
136
|
|
|
// // anyway, generate all |
|
|
|
|
|
|
137
|
|
|
// else { |
|
138
|
|
|
// foreach ($this->getModels() as $model) { |
|
139
|
|
|
// $models [] = $model->getOriginCommonName(); |
|
140
|
7 |
|
// } |
|
141
|
7 |
|
// } |
|
142
|
7 |
|
|
|
143
|
|
|
// return $models; |
|
|
|
|
|
|
144
|
|
|
// } |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
7 |
|
* Checks whether the given model exists |
|
148
|
|
|
* |
|
149
|
7 |
|
* @param String $name tableName or modelName |
|
150
|
|
|
* @return boolean |
|
151
|
|
|
*/ |
|
152
|
|
|
public function hasModel($name) { |
|
153
|
|
|
return $this->getReader()->hasModel($name); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Parses the model name from a given action name |
|
158
|
8 |
|
* |
|
159
|
8 |
|
* @param ActionSchema $action |
|
160
|
|
|
* @return String modelName |
|
161
|
|
|
*/ |
|
162
|
|
|
public function getModelNameByAction(ActionSchema $action) { |
|
163
|
|
|
$actionName = $action->getName(); |
|
164
|
|
|
$modelName = null; |
|
165
|
|
|
if (($pos = strpos($actionName, '-')) !== false) { |
|
166
|
|
|
$modelName = substr($actionName, 0, $pos); |
|
167
|
1 |
|
} |
|
168
|
1 |
|
return $modelName; |
|
169
|
1 |
|
} |
|
170
|
1 |
|
|
|
171
|
1 |
|
/** |
|
172
|
1 |
|
* Returns the full model object name, including namespace |
|
173
|
1 |
|
* |
|
174
|
1 |
|
* @param ActionSchema $action |
|
175
|
1 |
|
* @return String fullModelObjectName |
|
176
|
1 |
|
*/ |
|
177
|
|
|
public function getFullModelObjectName(ActionSchema $action) { |
|
178
|
1 |
|
$database = $this->getDatabase(); |
|
179
|
1 |
|
$modelName = $this->getModelNameByAction($action); |
|
180
|
|
|
$model = $this->getModel($modelName); |
|
181
|
1 |
|
$modelObjectName = $model->getPhpName(); |
|
182
|
|
|
|
|
183
|
|
|
return $database->getNamespace() . '\\' . $modelObjectName; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Returns wether the given action refers to a model. |
|
188
|
|
|
* |
|
189
|
|
|
* Examples: |
|
190
|
|
|
* |
|
191
|
|
|
* Action: user-create => model: user |
|
192
|
|
|
* Action: recover-password => no model |
|
193
|
|
|
* |
|
194
|
|
|
* @param ActionSchema $action |
|
195
|
|
|
* @return boolean |
|
196
|
|
|
*/ |
|
197
|
|
|
public function isModelAction(ActionSchema $action) { |
|
198
|
|
|
$modelName = $this->getModelNameByAction($action); |
|
199
|
|
|
return $this->hasModel($modelName); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Returns all model relationships. |
|
204
|
|
|
* |
|
205
|
|
|
* @param Table $model |
|
206
|
|
|
* @return Relationships |
|
207
|
|
|
*/ |
|
208
|
|
|
public function getRelationships(Table $model) { |
|
209
|
|
|
return $this->getReader()->getRelationships($model); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
10 |
|
/** |
|
213
|
10 |
|
* Returns a relationship for a given related type name on a given model |
|
214
|
10 |
|
* |
|
215
|
10 |
|
* @param Table $model |
|
216
|
10 |
|
* @param string $relatedTypeName |
|
217
|
10 |
|
* @return Relationship |
|
218
|
10 |
|
*/ |
|
219
|
|
|
public function getRelationship(Table $model, $relatedTypeName) { |
|
220
|
|
|
$relationships = $this->getRelationships($model); |
|
221
|
|
|
return $relationships->get($relatedTypeName); |
|
222
|
|
|
} |
|
223
|
|
|
} |
|
224
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.