1
|
|
|
<?php |
2
|
|
|
namespace PSFS\services; |
3
|
|
|
|
4
|
|
|
use PSFS\base\Logger; |
5
|
|
|
use PSFS\base\types\helpers\GeneratorHelper; |
6
|
|
|
use PSFS\base\types\SimpleService; |
7
|
|
|
use PSFS\base\types\traits\Generator\PropelHelperTrait; |
8
|
|
|
use PSFS\base\types\traits\Generator\StructureTrait; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class GeneratorService |
12
|
|
|
* @package PSFS\services |
13
|
|
|
*/ |
14
|
|
|
class GeneratorService extends SimpleService |
15
|
|
|
{ |
16
|
|
|
use PropelHelperTrait; |
17
|
|
|
use StructureTrait; |
18
|
|
|
/** |
19
|
|
|
* @Injectable |
20
|
|
|
* @var \PSFS\base\config\Config Servicio de configuración |
21
|
|
|
*/ |
22
|
|
|
protected $config; |
23
|
|
|
/** |
24
|
|
|
* @Injectable |
25
|
|
|
* @var \PSFS\base\Security Servicio de autenticación |
26
|
|
|
*/ |
27
|
|
|
protected $security; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Servicio que genera la estructura de un módulo o lo actualiza en caso de ser necesario |
31
|
|
|
* @param string $module |
32
|
|
|
* @param boolean $force |
33
|
|
|
* @param string $type |
34
|
|
|
* @param string $apiClass |
35
|
|
|
* @return mixed |
36
|
|
|
* @throws \PSFS\base\exception\GeneratorException |
37
|
|
|
* @throws \ReflectionException |
38
|
|
|
*/ |
39
|
1 |
|
public function createStructureModule($module, $force = false, $type = "", $apiClass = "") |
40
|
|
|
{ |
41
|
1 |
|
$modPath = CORE_DIR . DIRECTORY_SEPARATOR; |
42
|
1 |
|
$module = ucfirst($module); |
43
|
1 |
|
$this->createModulePath($module, $modPath); |
44
|
1 |
|
$this->createModulePathTree($module, $modPath); |
|
|
|
|
45
|
1 |
|
$this->createModuleBaseFiles($module, $modPath, $force, $type); |
46
|
1 |
|
$this->createModuleModels($module, $modPath); |
47
|
1 |
|
$this->generateBaseApiTemplate($module, $modPath, $force, $apiClass); |
48
|
|
|
//Redireccionamos al home definido |
49
|
1 |
|
Logger::log("Módulo generado correctamente"); |
50
|
1 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Servicio que genera las plantillas básicas de ficheros del módulo |
54
|
|
|
* @param string $module |
55
|
|
|
* @param string $modPath |
56
|
|
|
* @param boolean $force |
57
|
|
|
* @param string $controllerType |
58
|
|
|
* @throws \PSFS\base\exception\GeneratorException |
59
|
|
|
*/ |
60
|
1 |
|
private function createModuleBaseFiles($module, $modPath, $force = false, $controllerType = '') |
61
|
|
|
{ |
62
|
1 |
|
$modulePath = $modPath . $module; |
63
|
1 |
|
$this->generateControllerTemplate($module, $modulePath, $force, $controllerType); |
64
|
1 |
|
$this->generateServiceTemplate($module, $modulePath, $force); |
65
|
1 |
|
$this->genereateAutoloaderTemplate($module, $modulePath, $force); |
66
|
1 |
|
$this->generateSchemaTemplate($module, $modulePath, $force); |
67
|
1 |
|
$this->generatePropertiesTemplate($module, $modulePath, $force); |
68
|
1 |
|
$this->generateConfigTemplate($modulePath, $force); |
69
|
1 |
|
$this->generateIndexTemplate($module, $modulePath, $force); |
70
|
1 |
|
$this->generatePublicTemplates($modulePath, $force); |
71
|
1 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $module |
75
|
|
|
* @param string $modPath |
76
|
|
|
* @param boolean $force |
77
|
|
|
* @param string $controllerType |
78
|
|
|
* @return boolean |
79
|
|
|
*/ |
80
|
1 |
|
private function generateControllerTemplate($module, $modPath, $force = false, $controllerType = "") |
81
|
|
|
{ |
82
|
|
|
//Generamos el controlador base |
83
|
1 |
|
Logger::log("Generamos el controlador BASE"); |
84
|
1 |
|
$class = preg_replace('/(\\\|\/)/', '', $module); |
85
|
1 |
|
$controllerBody = $this->tpl->dump("generator/controller.template.twig", array( |
86
|
1 |
|
"module" => $module, |
87
|
1 |
|
"namespace" => preg_replace('/(\\\|\/)/', '\\', $module), |
88
|
1 |
|
"url" => preg_replace('/(\\\|\/)/', '/', $module), |
89
|
1 |
|
"class" => $class, |
90
|
1 |
|
"controllerType" => $class . "Base", |
91
|
|
|
"is_base" => false |
92
|
|
|
)); |
93
|
1 |
|
$controller = $this->writeTemplateToFile($controllerBody, $modPath . DIRECTORY_SEPARATOR . "Controller" . |
94
|
1 |
|
DIRECTORY_SEPARATOR . "{$class}Controller.php", $force); |
95
|
|
|
|
96
|
1 |
|
$controllerBody = $this->tpl->dump("generator/controller.template.twig", array( |
97
|
1 |
|
"module" => $module, |
98
|
1 |
|
"namespace" => preg_replace('/(\\\|\/)/', '\\', $module), |
99
|
1 |
|
"url" => preg_replace('/(\\\|\/)/', '/', $module), |
100
|
1 |
|
"class" => $class . "Base", |
101
|
1 |
|
"service" => $class, |
102
|
1 |
|
"controllerType" => $controllerType, |
103
|
|
|
"is_base" => true, |
104
|
1 |
|
"domain" => $class, |
105
|
|
|
)); |
106
|
1 |
|
$controllerBase = $this->writeTemplateToFile($controllerBody, $modPath . DIRECTORY_SEPARATOR . "Controller" . |
107
|
1 |
|
DIRECTORY_SEPARATOR . "base" . DIRECTORY_SEPARATOR . "{$class}BaseController.php", true); |
108
|
|
|
|
109
|
1 |
|
$filename = $modPath . DIRECTORY_SEPARATOR . "Test" . DIRECTORY_SEPARATOR . "{$class}Test.php"; |
110
|
1 |
|
$test = true; |
111
|
1 |
|
if(!file_exists($filename)) { |
112
|
1 |
|
$testTemplate = $this->tpl->dump("generator/testCase.template.twig", array( |
113
|
1 |
|
"module" => $module, |
114
|
1 |
|
"namespace" => preg_replace('/(\\\|\/)/', '\\', $module), |
115
|
1 |
|
"class" => $class, |
116
|
|
|
)); |
117
|
1 |
|
$test = $this->writeTemplateToFile($testTemplate, $filename, true); |
118
|
|
|
} |
119
|
1 |
|
return ($controller && $controllerBase && $test); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Servicio que ejecuta Propel y genera el modelo de datos |
124
|
|
|
* @param string $module |
125
|
|
|
* @param string $path |
126
|
|
|
* @throws \PSFS\base\exception\GeneratorException |
127
|
|
|
*/ |
128
|
1 |
|
private function createModuleModels($module, $path) |
129
|
|
|
{ |
130
|
1 |
|
$modulePath = $path . $module; |
131
|
1 |
|
$modulePath = str_replace(CORE_DIR . DIRECTORY_SEPARATOR, '', $modulePath); |
132
|
|
|
|
133
|
1 |
|
$configGenerator = $this->getConfigGenerator($modulePath); |
134
|
|
|
|
135
|
1 |
|
$this->buildModels($configGenerator); |
136
|
1 |
|
$this->buildSql($configGenerator); |
137
|
|
|
|
138
|
1 |
|
$configTemplate = $this->tpl->dump("generator/config.propel.template.twig", array( |
139
|
1 |
|
"module" => $module, |
140
|
|
|
)); |
141
|
1 |
|
$this->writeTemplateToFile($configTemplate, CORE_DIR . DIRECTORY_SEPARATOR . $modulePath . DIRECTORY_SEPARATOR . "Config" . |
142
|
1 |
|
DIRECTORY_SEPARATOR . "config.php", true); |
143
|
1 |
|
Logger::log("Generado config genérico para propel"); |
144
|
1 |
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param string $module |
148
|
|
|
* @param string $modPath |
149
|
|
|
* @param boolean $force |
150
|
|
|
* @param string $apiClass |
151
|
|
|
* @return boolean |
152
|
|
|
* @throws \ReflectionException |
153
|
|
|
*/ |
154
|
1 |
|
private function generateBaseApiTemplate($module, $modPath, $force = false, $apiClass = "") |
155
|
|
|
{ |
156
|
1 |
|
$created = true; |
157
|
1 |
|
$modelPath = $modPath . $module . DIRECTORY_SEPARATOR . 'Models'; |
158
|
1 |
|
$apiPath = $modPath . $module . DIRECTORY_SEPARATOR . 'Api'; |
159
|
1 |
|
if (file_exists($modelPath)) { |
160
|
1 |
|
$dir = dir($modelPath); |
161
|
1 |
|
$this->generateApiFiles($module, $force, $apiClass, $dir, $apiPath); |
|
|
|
|
162
|
|
|
} |
163
|
1 |
|
return $created; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param string $modPath |
168
|
|
|
* @param boolean $force |
169
|
|
|
* @return boolean |
170
|
|
|
*/ |
171
|
1 |
|
private function generateConfigTemplate($modPath, $force = false) |
172
|
|
|
{ |
173
|
|
|
//Generamos el fichero de configuración |
174
|
1 |
|
Logger::log("Generamos fichero vacío de configuración"); |
175
|
1 |
|
return $this->writeTemplateToFile("<?php\n\t", |
176
|
1 |
|
$modPath . DIRECTORY_SEPARATOR . "Config" . DIRECTORY_SEPARATOR . "config.php", |
177
|
1 |
|
$force); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param string $module |
182
|
|
|
* @param string $modPath |
183
|
|
|
* @param boolean $force |
184
|
|
|
* @return boolean |
185
|
|
|
*/ |
186
|
1 |
|
private function generateSchemaTemplate($module, $modPath, $force = false) |
187
|
|
|
{ |
188
|
|
|
//Generamos el autoloader del módulo |
189
|
1 |
|
Logger::log("Generamos el schema"); |
190
|
1 |
|
$schema = $this->tpl->dump("generator/schema.propel.twig", array( |
191
|
1 |
|
"module" => $module, |
192
|
1 |
|
"namespace" => preg_replace('/(\\\|\/)/', '', $module), |
193
|
1 |
|
"prefix" => preg_replace('/(\\\|\/)/', '', $module), |
194
|
1 |
|
"db" => $this->config->get("db_name"), |
195
|
|
|
)); |
196
|
1 |
|
return $this->writeTemplateToFile($schema, |
197
|
1 |
|
$modPath . DIRECTORY_SEPARATOR . "Config" . DIRECTORY_SEPARATOR . "schema.xml", |
198
|
1 |
|
$force); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @param string $module |
203
|
|
|
* @param string $modPath |
204
|
|
|
* @param boolean $force |
205
|
|
|
* @return boolean |
206
|
|
|
*/ |
207
|
1 |
|
private function generatePropertiesTemplate($module, $modPath, $force = false) |
208
|
|
|
{ |
209
|
1 |
|
Logger::log("Generamos la configuración de Propel"); |
210
|
1 |
|
$buildProperties = $this->tpl->dump("generator/build.properties.twig", array( |
211
|
1 |
|
"module" => $module, |
212
|
1 |
|
"namespace" => preg_replace('/(\\\|\/)/', '', $module), |
213
|
|
|
)); |
214
|
1 |
|
return $this->writeTemplateToFile($buildProperties, |
215
|
1 |
|
$modPath . DIRECTORY_SEPARATOR . "Config" . DIRECTORY_SEPARATOR . "propel.yml", |
216
|
1 |
|
$force); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @param string $module |
221
|
|
|
* @param string $modPath |
222
|
|
|
* @param boolean $force |
223
|
|
|
* @return boolean |
224
|
|
|
*/ |
225
|
1 |
|
private function generateIndexTemplate($module, $modPath, $force = false) |
226
|
|
|
{ |
227
|
|
|
//Generamos la plantilla de index |
228
|
1 |
|
Logger::log("Generamos una plantilla base por defecto"); |
229
|
1 |
|
$index = $this->tpl->dump("generator/index.template.twig", array( |
230
|
1 |
|
"module" => $module, |
231
|
|
|
)); |
232
|
1 |
|
return $this->writeTemplateToFile($index, |
233
|
1 |
|
$modPath . DIRECTORY_SEPARATOR . "Templates" . DIRECTORY_SEPARATOR . "index.html.twig", |
234
|
1 |
|
$force); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Create ApiBase |
239
|
|
|
* @param string $module |
240
|
|
|
* @param string $modPath |
241
|
|
|
* @param string $api |
242
|
|
|
* @param string $apiClass |
243
|
|
|
* @param string $package |
244
|
|
|
* |
245
|
|
|
* @return bool |
246
|
|
|
*/ |
247
|
1 |
|
private function createApiBaseFile($module, $modPath, $api, $apiClass = '', $package = null) |
248
|
|
|
{ |
249
|
1 |
|
$class = preg_replace('/(\\\|\/)/', '', $module); |
250
|
1 |
|
$customClass = GeneratorHelper::extractClassFromNamespace($apiClass); |
251
|
1 |
|
$controller = $this->tpl->dump("generator/api.base.template.twig", array( |
252
|
1 |
|
"module" => $module, |
253
|
1 |
|
"api" => $api, |
254
|
1 |
|
"namespace" => preg_replace('/(\\\|\/)/', '\\', $module), |
255
|
1 |
|
"url" => preg_replace('/(\\\|\/)/', '/', $module), |
256
|
1 |
|
"class" => $class, |
257
|
1 |
|
'customClass' => $customClass, |
258
|
1 |
|
'customNamespace' => $apiClass, |
259
|
1 |
|
'package' => $package, |
260
|
|
|
)); |
261
|
|
|
|
262
|
1 |
|
return $this->writeTemplateToFile($controller, |
263
|
1 |
|
$modPath . DIRECTORY_SEPARATOR . 'base' . DIRECTORY_SEPARATOR . "{$api}BaseApi.php", true); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Create Api |
268
|
|
|
* @param string $module |
269
|
|
|
* @param string $modPath |
270
|
|
|
* @param bool $force |
271
|
|
|
* @param string $api |
272
|
|
|
* @param string $package |
273
|
|
|
* |
274
|
|
|
* @return bool |
275
|
|
|
*/ |
276
|
1 |
|
private function createApi($module, $modPath, $force, $api, $package = null) |
277
|
|
|
{ |
278
|
1 |
|
$class = preg_replace('/(\\\|\/)/', '', $module); |
279
|
1 |
|
$controller = $this->tpl->dump("generator/api.template.twig", array( |
280
|
1 |
|
"module" => $module, |
281
|
1 |
|
"api" => $api, |
282
|
1 |
|
"namespace" => preg_replace('/(\\\|\/)/', '\\', $module), |
283
|
1 |
|
"url" => preg_replace('/(\\\|\/)/', '/', $module), |
284
|
1 |
|
"class" => $class, |
285
|
1 |
|
"package" => $package, |
286
|
|
|
)); |
287
|
|
|
|
288
|
1 |
|
return $this->writeTemplateToFile($controller, $modPath . DIRECTORY_SEPARATOR . "{$api}.php", $force); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* @param $module |
293
|
|
|
* @param $force |
294
|
|
|
* @param $apiClass |
295
|
|
|
* @param \Directory|null $dir |
296
|
|
|
* @param string $apiPath |
297
|
|
|
* @param string $package |
298
|
|
|
* @throws \ReflectionException |
299
|
|
|
*/ |
300
|
1 |
|
private function generateApiFiles($module, $force, $apiClass, \Directory $dir, string $apiPath, $package = null) |
301
|
|
|
{ |
302
|
1 |
|
$base = $dir->path; |
303
|
1 |
|
while ($file = $dir->read()) { |
304
|
1 |
|
if (!in_array(strtolower($file), ['.', '..', 'base', 'map'])) { |
305
|
1 |
|
if (is_dir($base . DIRECTORY_SEPARATOR . $file)) { |
306
|
1 |
|
$this->generateApiFiles($module, $force, $apiClass, dir($base . DIRECTORY_SEPARATOR . $file), $apiPath . DIRECTORY_SEPARATOR . $file, $file); |
|
|
|
|
307
|
1 |
|
} else if (!preg_match('/Query\.php$/i', $file) |
308
|
1 |
|
&& !preg_match('/I18n\.php$/i', $file) |
309
|
1 |
|
&& preg_match('/\.php$/i', $file) |
310
|
|
|
) { |
311
|
1 |
|
$filename = str_replace(".php", "", $file); |
312
|
1 |
|
$this->log->addLog("Generamos Api BASES para {$filename}"); |
313
|
1 |
|
if($this->checkIfIsModel($module, $filename, $package)) { |
314
|
1 |
|
$this->createApiBaseFile($module, $apiPath, $filename, $apiClass, $package); |
315
|
1 |
|
$this->createApi($module, $apiPath, $force, $filename, $package); |
316
|
|
|
} |
317
|
|
|
} |
318
|
|
|
} |
319
|
|
|
} |
320
|
1 |
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* @param string $module |
324
|
|
|
* @param string $package |
325
|
|
|
* @param string $filename |
326
|
|
|
* @return bool |
327
|
|
|
* @throws \ReflectionException |
328
|
|
|
*/ |
329
|
1 |
|
private function checkIfIsModel($module, $filename, $package = null) |
330
|
|
|
{ |
331
|
1 |
|
$parts = [$module, 'Models']; |
332
|
1 |
|
if (strlen($package)) { |
333
|
1 |
|
$parts[] = $package; |
334
|
|
|
} |
335
|
1 |
|
$parts[] = $filename; |
336
|
1 |
|
$namespace = '\\' . implode('\\', $parts); |
337
|
1 |
|
$reflectorClass = new \ReflectionClass($namespace); |
338
|
1 |
|
$isModel = $reflectorClass->isInstantiable(); |
339
|
1 |
|
return $isModel; |
340
|
|
|
} |
341
|
|
|
} |
342
|
|
|
|