1
|
|
|
<?php |
2
|
|
|
namespace PSFS\services; |
3
|
|
|
|
4
|
|
|
use Propel\Generator\Config\GeneratorConfig; |
5
|
|
|
use Propel\Generator\Manager\AbstractManager; |
6
|
|
|
use Propel\Generator\Manager\ModelManager; |
7
|
|
|
use Propel\Generator\Manager\SqlManager; |
8
|
|
|
use PSFS\base\Cache; |
9
|
|
|
use PSFS\base\config\Config; |
10
|
|
|
use PSFS\base\exception\ConfigException; |
11
|
|
|
use PSFS\base\Logger; |
12
|
|
|
use PSFS\base\Service; |
13
|
|
|
use PSFS\base\types\helpers\GeneratorHelper; |
14
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
15
|
|
|
|
16
|
|
|
class GeneratorService extends Service |
17
|
|
|
{ |
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
|
|
|
* @Injectable |
30
|
|
|
* @var \PSFS\base\Template Servicio de gestión de plantillas |
31
|
|
|
*/ |
32
|
|
|
protected $tpl; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Método que revisa las traducciones directorio a directorio |
36
|
|
|
* @param $path |
37
|
|
|
* @param $locale |
38
|
|
|
* @return array |
39
|
|
|
*/ |
40
|
|
|
public static function findTranslations($path, $locale) |
41
|
|
|
{ |
42
|
|
|
$localePath = realpath(BASE_DIR . DIRECTORY_SEPARATOR . 'locale'); |
43
|
|
|
$localePath .= DIRECTORY_SEPARATOR . $locale . DIRECTORY_SEPARATOR . 'LC_MESSAGES' . DIRECTORY_SEPARATOR; |
44
|
|
|
|
45
|
|
|
$translations = array(); |
46
|
|
|
if (file_exists($path)) { |
47
|
|
|
$d = dir($path); |
48
|
|
|
while (false !== ($dir = $d->read())) { |
49
|
|
|
GeneratorHelper::createDir($localePath); |
50
|
|
|
if (!file_exists($localePath . 'translations.po')) { |
51
|
|
|
file_put_contents($localePath . 'translations.po', ''); |
52
|
|
|
} |
53
|
|
|
$inspect_path = realpath($path . DIRECTORY_SEPARATOR . $dir); |
54
|
|
|
$cmd_php = "export PATH=\$PATH:/opt/local/bin; xgettext " . |
55
|
|
|
$inspect_path . DIRECTORY_SEPARATOR . |
56
|
|
|
"*.php --from-code=UTF-8 -j -L PHP --debug --force-po -o {$localePath}translations.po"; |
57
|
|
|
if (is_dir($path . DIRECTORY_SEPARATOR . $dir) && preg_match('/^\./', $dir) == 0) { |
58
|
|
|
$res = t('Revisando directorio: ') . $inspect_path; |
59
|
|
|
$res .= t('Comando ejecutado: ') . $cmd_php; |
60
|
|
|
$res .= shell_exec($cmd_php); |
61
|
|
|
usleep(10); |
62
|
|
|
$translations[] = $res; |
63
|
|
|
$translations = array_merge($translations, self::findTranslations($inspect_path, $locale)); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
return $translations; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Servicio que genera la estructura de un módulo o lo actualiza en caso de ser necesario |
72
|
|
|
* @param string $module |
73
|
|
|
* @param boolean $force |
74
|
|
|
* @param string $type |
75
|
|
|
* @param string $apiClass |
76
|
|
|
* @return mixed |
77
|
|
|
*/ |
78
|
|
|
public function createStructureModule($module, $force = false, $type = "", $apiClass = "") |
79
|
|
|
{ |
80
|
|
|
$mod_path = CORE_DIR . DIRECTORY_SEPARATOR; |
81
|
|
|
$module = ucfirst($module); |
82
|
|
|
$this->createModulePath($module, $mod_path); |
83
|
|
|
$this->createModulePathTree($module, $mod_path); |
|
|
|
|
84
|
|
|
$this->createModuleBaseFiles($module, $mod_path, $force, $type); |
85
|
|
|
$this->createModuleModels($module, $mod_path); |
86
|
|
|
$this->generateBaseApiTemplate($module, $mod_path, $force, $apiClass); |
87
|
|
|
//Redireccionamos al home definido |
88
|
|
|
$this->log->addLog("Módulo generado correctamente"); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Service that creates the root paths for the modules |
93
|
|
|
* @param string $module |
94
|
|
|
* @param string $mod_path |
95
|
|
|
*/ |
96
|
|
|
private function createModulePath($module, $mod_path) |
97
|
|
|
{ |
98
|
|
|
// Creates the src folder |
99
|
|
|
GeneratorHelper::createDir($mod_path); |
100
|
|
|
// Create module path |
101
|
|
|
GeneratorHelper::createDir($mod_path . $module); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Servicio que genera la estructura base |
106
|
|
|
* @param string $module |
107
|
|
|
* @param boolean $mod_path |
108
|
|
|
* @return boolean |
109
|
|
|
*/ |
110
|
|
|
private function createModulePathTree($module, $mod_path) |
111
|
|
|
{ |
112
|
|
|
//Creamos las carpetas CORE del módulo |
113
|
|
|
$this->log->addLog("Generamos la estructura"); |
114
|
|
|
$paths = [ |
115
|
|
|
"Api", "Config", "Controller", "Models", "Public", "Templates", "Services", "Test", "Doc", |
116
|
|
|
"Locale", "Locale/" . Config::getParam('default.locale', 'es_ES'), "Locale/" . Config::getParam('default.locale', 'es_ES') . "/LC_MESSAGES" |
117
|
|
|
]; |
118
|
|
|
$module_path = $mod_path . $module; |
119
|
|
|
foreach ($paths as $path) { |
120
|
|
|
GeneratorHelper::createDir($module_path . DIRECTORY_SEPARATOR . $path); |
121
|
|
|
} |
122
|
|
|
//Creamos las carpetas de los assets |
123
|
|
|
$htmlPaths = array("css", "js", "img", "media", "font"); |
124
|
|
|
foreach ($htmlPaths as $path) { |
125
|
|
|
GeneratorHelper::createDir($module_path . DIRECTORY_SEPARATOR . "Public" . DIRECTORY_SEPARATOR . $path); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Servicio que genera las plantillas básicas de ficheros del módulo |
131
|
|
|
* @param string $module |
132
|
|
|
* @param string $mod_path |
133
|
|
|
* @param boolean $force |
134
|
|
|
* @param string $controllerType |
135
|
|
|
*/ |
136
|
|
|
private function createModuleBaseFiles($module, $mod_path, $force = false, $controllerType = '') |
137
|
|
|
{ |
138
|
|
|
$module_path = $mod_path . $module; |
139
|
|
|
$this->generateControllerTemplate($module, $module_path, $force, $controllerType); |
140
|
|
|
$this->generateServiceTemplate($module, $module_path, $force); |
141
|
|
|
$this->genereateAutoloaderTemplate($module, $module_path, $force); |
142
|
|
|
$this->generateSchemaTemplate($module, $module_path, $force); |
143
|
|
|
$this->generatePropertiesTemplate($module, $module_path, $force); |
144
|
|
|
$this->generateConfigTemplate($module_path, $force); |
145
|
|
|
$this->generateIndexTemplate($module, $module_path, $force); |
146
|
|
|
$this->generatePublicTemplates($module_path, $force); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Servicio que ejecuta Propel y genera el modelo de datos |
151
|
|
|
* @param string $module |
152
|
|
|
* @param string $path |
153
|
|
|
*/ |
154
|
|
|
private function createModuleModels($module, $path) |
155
|
|
|
{ |
156
|
|
|
$module_path = $path . $module; |
157
|
|
|
$module_path = str_replace(CORE_DIR . DIRECTORY_SEPARATOR, '', $module_path); |
158
|
|
|
|
159
|
|
|
$configGenerator = $this->getConfigGenerator($module_path); |
160
|
|
|
|
161
|
|
|
$this->buildModels($configGenerator); |
162
|
|
|
$this->buildSql($configGenerator); |
163
|
|
|
|
164
|
|
|
$configTemplate = $this->tpl->dump("generator/config.propel.template.twig", array( |
165
|
|
|
"module" => $module, |
166
|
|
|
)); |
167
|
|
|
$this->writeTemplateToFile($configTemplate, CORE_DIR . DIRECTORY_SEPARATOR . $module_path . DIRECTORY_SEPARATOR . "Config" . |
168
|
|
|
DIRECTORY_SEPARATOR . "config.php", true); |
169
|
|
|
$this->log->addLog("Generado config genérico para propel"); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param string $module |
175
|
|
|
* @param string $mod_path |
176
|
|
|
* @param boolean $force |
177
|
|
|
* @param string $controllerType |
178
|
|
|
* @return boolean |
179
|
|
|
*/ |
180
|
|
|
private function generateControllerTemplate($module, $mod_path, $force = false, $controllerType = "") |
181
|
|
|
{ |
182
|
|
|
//Generamos el controlador base |
183
|
|
|
$this->log->addLog("Generamos el controlador BASE"); |
184
|
|
|
$class = preg_replace('/(\\\|\/)/', '', $module); |
185
|
|
|
$controllerBody = $this->tpl->dump("generator/controller.template.twig", array( |
186
|
|
|
"module" => $module, |
187
|
|
|
"namespace" => preg_replace('/(\\\|\/)/', '\\', $module), |
188
|
|
|
"url" => preg_replace('/(\\\|\/)/', '/', $module), |
189
|
|
|
"class" => $class, |
190
|
|
|
"controllerType" => $class . "Base", |
191
|
|
|
"is_base" => false |
192
|
|
|
)); |
193
|
|
|
$controller = $this->writeTemplateToFile($controllerBody, $mod_path . DIRECTORY_SEPARATOR . "Controller" . |
194
|
|
|
DIRECTORY_SEPARATOR . "{$class}Controller.php", $force); |
195
|
|
|
|
196
|
|
|
$controllerBody = $this->tpl->dump("generator/controller.template.twig", array( |
197
|
|
|
"module" => $module, |
198
|
|
|
"namespace" => preg_replace('/(\\\|\/)/', '\\', $module), |
199
|
|
|
"url" => preg_replace('/(\\\|\/)/', '/', $module), |
200
|
|
|
"class" => $class . "Base", |
201
|
|
|
"service" => $class, |
202
|
|
|
"controllerType" => $controllerType, |
203
|
|
|
"is_base" => true, |
204
|
|
|
"domain" => $class, |
205
|
|
|
)); |
206
|
|
|
$controllerBase = $this->writeTemplateToFile($controllerBody, $mod_path . DIRECTORY_SEPARATOR . "Controller" . |
207
|
|
|
DIRECTORY_SEPARATOR . "base" . DIRECTORY_SEPARATOR . "{$class}BaseController.php", true); |
208
|
|
|
|
209
|
|
|
$filename = $mod_path . DIRECTORY_SEPARATOR . "Test" . DIRECTORY_SEPARATOR . "{$class}Test.php"; |
210
|
|
|
$test = true; |
211
|
|
|
if(!file_exists($filename)) { |
212
|
|
|
$testTemplate = $this->tpl->dump("generator/testCase.template.twig", array( |
213
|
|
|
"module" => $module, |
214
|
|
|
"namespace" => preg_replace('/(\\\|\/)/', '\\', $module), |
215
|
|
|
"class" => $class, |
216
|
|
|
)); |
217
|
|
|
$test = $this->writeTemplateToFile($testTemplate, $filename, true); |
218
|
|
|
} |
219
|
|
|
return ($controller && $controllerBase && $test); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @param string $module |
224
|
|
|
* @param string $modPath |
225
|
|
|
* @param boolean $force |
226
|
|
|
* @param string $apiClass |
227
|
|
|
* @return boolean |
228
|
|
|
*/ |
229
|
|
|
private function generateBaseApiTemplate($module, $modPath, $force = false, $apiClass = "") |
230
|
|
|
{ |
231
|
|
|
$created = true; |
232
|
|
|
$modelPath = $modPath . $module . DIRECTORY_SEPARATOR . 'Models'; |
233
|
|
|
$apiPath = $modPath . $module . DIRECTORY_SEPARATOR . 'Api'; |
234
|
|
|
if (file_exists($modelPath)) { |
235
|
|
|
$dir = dir($modelPath); |
236
|
|
|
$this->generateApiFiles($module, $force, $apiClass, $dir, $apiPath); |
|
|
|
|
237
|
|
|
} |
238
|
|
|
return $created; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @param string $mod_path |
243
|
|
|
* @param boolean $force |
244
|
|
|
* @return boolean |
245
|
|
|
*/ |
246
|
|
|
private function generateConfigTemplate($mod_path, $force = false) |
247
|
|
|
{ |
248
|
|
|
//Generamos el fichero de configuración |
249
|
|
|
$this->log->addLog("Generamos fichero vacío de configuración"); |
250
|
|
|
return $this->writeTemplateToFile("<?php\n\t", |
251
|
|
|
$mod_path . DIRECTORY_SEPARATOR . "Config" . DIRECTORY_SEPARATOR . "config.php", |
252
|
|
|
$force); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @param string $mod_path |
257
|
|
|
* @param string $mod_path |
258
|
|
|
* @param boolean $force |
259
|
|
|
* @return boolean |
260
|
|
|
*/ |
261
|
|
|
private function generatePublicTemplates($mod_path, $force = false) |
262
|
|
|
{ |
263
|
|
|
//Generamos el fichero de configuración |
264
|
|
|
$this->log->addLog("Generamos ficheros para assets base"); |
265
|
|
|
$css = $this->writeTemplateToFile("/* CSS3 STYLES */\n\n", |
266
|
|
|
$mod_path . DIRECTORY_SEPARATOR . "Public" . DIRECTORY_SEPARATOR . "css" . DIRECTORY_SEPARATOR . "styles.css", |
267
|
|
|
$force); |
268
|
|
|
$js = $this->writeTemplateToFile("/* APP MODULE JS */\n\n(function() {\n\t'use strict';\n})();", |
269
|
|
|
$mod_path . DIRECTORY_SEPARATOR . "Public" . DIRECTORY_SEPARATOR . "js" . DIRECTORY_SEPARATOR . "app.js", |
270
|
|
|
$force); |
271
|
|
|
return ($css && $js); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* @param string $module |
276
|
|
|
* @param string $mod_path |
277
|
|
|
* @param boolean $force |
278
|
|
|
* @return boolean |
279
|
|
|
*/ |
280
|
|
|
private function generateServiceTemplate($module, $mod_path, $force = false) |
281
|
|
|
{ |
282
|
|
|
//Generamos el controlador base |
283
|
|
|
$this->log->addLog("Generamos el servicio BASE"); |
284
|
|
|
$class = preg_replace('/(\\\|\/)/', '', $module); |
285
|
|
|
$controller = $this->tpl->dump("generator/service.template.twig", array( |
286
|
|
|
"module" => $module, |
287
|
|
|
"namespace" => preg_replace('/(\\\|\/)/', '\\', $module), |
288
|
|
|
"class" => $class, |
289
|
|
|
)); |
290
|
|
|
return $this->writeTemplateToFile($controller, |
291
|
|
|
$mod_path . DIRECTORY_SEPARATOR . "Services" . DIRECTORY_SEPARATOR . "{$class}Service.php", |
292
|
|
|
$force); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* @param string $module |
297
|
|
|
* @param string $mod_path |
298
|
|
|
* @param boolean $force |
299
|
|
|
* @return boolean |
300
|
|
|
*/ |
301
|
|
|
private function genereateAutoloaderTemplate($module, $mod_path, $force = false) |
302
|
|
|
{ |
303
|
|
|
//Generamos el autoloader del módulo |
304
|
|
|
$this->log->addLog("Generamos el autoloader"); |
305
|
|
|
$autoloader = $this->tpl->dump("generator/autoloader.template.twig", array( |
306
|
|
|
"module" => $module, |
307
|
|
|
"autoloader" => preg_replace('/(\\\|\/)/', '_', $module), |
308
|
|
|
"regex" => preg_replace('/(\\\|\/)/m', '\\\\\\\\\\\\', $module), |
309
|
|
|
)); |
310
|
|
|
$autoload = $this->writeTemplateToFile($autoloader, $mod_path . DIRECTORY_SEPARATOR . "autoload.php", $force); |
311
|
|
|
|
312
|
|
|
$this->log->addLog("Generamos el phpunit"); |
313
|
|
|
$phpUnitTemplate = $this->tpl->dump("generator/phpunit.template.twig", array( |
314
|
|
|
"module" => $module, |
315
|
|
|
)); |
316
|
|
|
$phpunit = $this->writeTemplateToFile($phpUnitTemplate, $mod_path . DIRECTORY_SEPARATOR . "phpunit.xml.dist", $force); |
317
|
|
|
return $autoload && $phpunit; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* @param string $module |
322
|
|
|
* @param string $mod_path |
323
|
|
|
* @param boolean $force |
324
|
|
|
* @return boolean |
325
|
|
|
*/ |
326
|
|
|
private function generateSchemaTemplate($module, $mod_path, $force = false) |
327
|
|
|
{ |
328
|
|
|
//Generamos el autoloader del módulo |
329
|
|
|
$this->log->addLog("Generamos el schema"); |
330
|
|
|
$schema = $this->tpl->dump("generator/schema.propel.twig", array( |
331
|
|
|
"module" => $module, |
332
|
|
|
"namespace" => preg_replace('/(\\\|\/)/', '', $module), |
333
|
|
|
"prefix" => preg_replace('/(\\\|\/)/', '', $module), |
334
|
|
|
"db" => $this->config->get("db_name"), |
335
|
|
|
)); |
336
|
|
|
return $this->writeTemplateToFile($schema, |
337
|
|
|
$mod_path . DIRECTORY_SEPARATOR . "Config" . DIRECTORY_SEPARATOR . "schema.xml", |
338
|
|
|
$force); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* @param string $module |
343
|
|
|
* @param string $mod_path |
344
|
|
|
* @param boolean $force |
345
|
|
|
* @return boolean |
346
|
|
|
*/ |
347
|
|
|
private function generatePropertiesTemplate($module, $mod_path, $force = false) |
348
|
|
|
{ |
349
|
|
|
$this->log->addLog("Generamos la configuración de Propel"); |
350
|
|
|
$build_properties = $this->tpl->dump("generator/build.properties.twig", array( |
351
|
|
|
"module" => $module, |
352
|
|
|
"namespace" => preg_replace('/(\\\|\/)/', '', $module), |
353
|
|
|
)); |
354
|
|
|
return $this->writeTemplateToFile($build_properties, |
355
|
|
|
$mod_path . DIRECTORY_SEPARATOR . "Config" . DIRECTORY_SEPARATOR . "propel.yml", |
356
|
|
|
$force); |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* @param string $module |
361
|
|
|
* @param string $mod_path |
362
|
|
|
* @param boolean $force |
363
|
|
|
* @return boolean |
364
|
|
|
*/ |
365
|
|
|
private function generateIndexTemplate($module, $mod_path, $force = false) |
366
|
|
|
{ |
367
|
|
|
//Generamos la plantilla de index |
368
|
|
|
$this->log->addLog("Generamos una plantilla base por defecto"); |
369
|
|
|
$index = $this->tpl->dump("generator/index.template.twig", array( |
370
|
|
|
"module" => $module, |
371
|
|
|
)); |
372
|
|
|
return $this->writeTemplateToFile($index, |
373
|
|
|
$mod_path . DIRECTORY_SEPARATOR . "Templates" . DIRECTORY_SEPARATOR . "index.html.twig", |
374
|
|
|
$force); |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* Método que graba el contenido de una plantilla en un fichero |
379
|
|
|
* @param string $fileContent |
380
|
|
|
* @param string $filename |
381
|
|
|
* @param boolean $force |
382
|
|
|
* @return boolean |
383
|
|
|
*/ |
384
|
|
|
private function writeTemplateToFile($fileContent, $filename, $force = false) |
385
|
|
|
{ |
386
|
|
|
$created = false; |
387
|
|
|
if ($force || !file_exists($filename)) { |
388
|
|
|
try { |
389
|
|
|
$this->cache->storeData($filename, $fileContent, Cache::TEXT, true); |
390
|
|
|
$created = true; |
391
|
|
|
} catch (\Exception $e) { |
392
|
|
|
Logger::log($e->getMessage(), LOG_ERR); |
393
|
|
|
} |
394
|
|
|
} else { |
395
|
|
|
Logger::log($filename . t(' not exists or cant write'), LOG_ERR); |
396
|
|
|
} |
397
|
|
|
return $created; |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* Create ApiBase |
402
|
|
|
* @param string $module |
403
|
|
|
* @param string $mod_path |
404
|
|
|
* @param string $api |
405
|
|
|
* @param string $apiClass |
406
|
|
|
* @param string $package |
407
|
|
|
* |
408
|
|
|
* @return bool |
409
|
|
|
*/ |
410
|
|
|
private function createApiBaseFile($module, $mod_path, $api, $apiClass = '', $package = null) |
411
|
|
|
{ |
412
|
|
|
$class = preg_replace('/(\\\|\/)/', '', $module); |
413
|
|
|
$customClass = GeneratorHelper::extractClassFromNamespace($apiClass); |
414
|
|
|
$controller = $this->tpl->dump("generator/api.base.template.twig", array( |
415
|
|
|
"module" => $module, |
416
|
|
|
"api" => $api, |
417
|
|
|
"namespace" => preg_replace('/(\\\|\/)/', '\\', $module), |
418
|
|
|
"url" => preg_replace('/(\\\|\/)/', '/', $module), |
419
|
|
|
"class" => $class, |
420
|
|
|
'customClass' => $customClass, |
421
|
|
|
'customNamespace' => $apiClass, |
422
|
|
|
'package' => $package, |
423
|
|
|
)); |
424
|
|
|
|
425
|
|
|
return $this->writeTemplateToFile($controller, |
426
|
|
|
$mod_path . DIRECTORY_SEPARATOR . 'base' . DIRECTORY_SEPARATOR . "{$api}BaseApi.php", true); |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
/** |
430
|
|
|
* Create Api |
431
|
|
|
* @param string $module |
432
|
|
|
* @param string $mod_path |
433
|
|
|
* @param bool $force |
434
|
|
|
* @param string $api |
435
|
|
|
* @param string $package |
436
|
|
|
* |
437
|
|
|
* @return bool |
438
|
|
|
*/ |
439
|
|
|
private function createApi($module, $mod_path, $force, $api, $package = null) |
440
|
|
|
{ |
441
|
|
|
$class = preg_replace('/(\\\|\/)/', '', $module); |
442
|
|
|
$controller = $this->tpl->dump("generator/api.template.twig", array( |
443
|
|
|
"module" => $module, |
444
|
|
|
"api" => $api, |
445
|
|
|
"namespace" => preg_replace('/(\\\|\/)/', '\\', $module), |
446
|
|
|
"url" => preg_replace('/(\\\|\/)/', '/', $module), |
447
|
|
|
"class" => $class, |
448
|
|
|
"package" => $package, |
449
|
|
|
)); |
450
|
|
|
|
451
|
|
|
return $this->writeTemplateToFile($controller, $mod_path . DIRECTORY_SEPARATOR . "{$api}.php", $force); |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
/** |
455
|
|
|
* Method that copy resources recursively |
456
|
|
|
* @param string $dest |
457
|
|
|
* @param boolean $force |
458
|
|
|
* @param $filename_path |
459
|
|
|
* @param boolean $debug |
460
|
|
|
*/ |
461
|
1 |
|
public static function copyResources($dest, $force, $filename_path, $debug) |
462
|
|
|
{ |
463
|
1 |
|
if (file_exists($filename_path)) { |
464
|
1 |
|
$destfolder = basename($filename_path); |
465
|
1 |
|
if (!file_exists(WEB_DIR . $dest . DIRECTORY_SEPARATOR . $destfolder) || $debug || $force) { |
466
|
1 |
|
if (is_dir($filename_path)) { |
467
|
1 |
|
self::copyr($filename_path, WEB_DIR . $dest . DIRECTORY_SEPARATOR . $destfolder); |
468
|
|
|
} else { |
469
|
|
|
if (@copy($filename_path, WEB_DIR . $dest . DIRECTORY_SEPARATOR . $destfolder) === FALSE) { |
470
|
|
|
throw new ConfigException("Can't copy " . $filename_path . " to " . WEB_DIR . $dest . DIRECTORY_SEPARATOR . $destfolder); |
471
|
|
|
} |
472
|
|
|
} |
473
|
|
|
} |
474
|
|
|
} |
475
|
1 |
|
} |
476
|
|
|
|
477
|
|
|
/** |
478
|
|
|
* Method that copy a resource |
479
|
|
|
* @param string $src |
480
|
|
|
* @param string $dst |
481
|
|
|
* @throws ConfigException |
482
|
|
|
*/ |
483
|
1 |
|
public static function copyr($src, $dst) |
484
|
|
|
{ |
485
|
1 |
|
$dir = opendir($src); |
486
|
1 |
|
GeneratorHelper::createDir($dst); |
487
|
1 |
|
while (false !== ($file = readdir($dir))) { |
|
|
|
|
488
|
1 |
|
if (($file != '.') && ($file != '..')) { |
489
|
1 |
|
if (is_dir($src . '/' . $file)) { |
490
|
1 |
|
self::copyr($src . '/' . $file, $dst . '/' . $file); |
491
|
1 |
|
} elseif (@copy($src . '/' . $file, $dst . '/' . $file) === false) { |
492
|
|
|
throw new ConfigException("Can't copy " . $src . " to " . $dst); |
493
|
|
|
} |
494
|
|
|
} |
495
|
|
|
} |
496
|
1 |
|
closedir($dir); |
|
|
|
|
497
|
1 |
|
} |
498
|
|
|
|
499
|
|
|
/** |
500
|
|
|
* @param $module_path |
501
|
|
|
* @return array |
502
|
|
|
*/ |
503
|
|
|
private function getPropelPaths($module_path) |
504
|
|
|
{ |
505
|
|
|
$moduleDir = CORE_DIR . DIRECTORY_SEPARATOR . $module_path; |
506
|
|
|
GeneratorHelper::createDir($moduleDir); |
507
|
|
|
$moduleDir = realpath($moduleDir); |
508
|
|
|
$configDir = $moduleDir . DIRECTORY_SEPARATOR . 'Config'; |
509
|
|
|
$sqlDir = $moduleDir . DIRECTORY_SEPARATOR . 'Config' . DIRECTORY_SEPARATOR . 'Sql'; |
510
|
|
|
$migrationDir = $moduleDir . DIRECTORY_SEPARATOR . 'Config' . DIRECTORY_SEPARATOR . 'Migrations'; |
511
|
|
|
$paths = [ |
512
|
|
|
'projectDir' => $moduleDir, |
513
|
|
|
'outputDir' => $moduleDir, |
514
|
|
|
'phpDir' => $moduleDir, |
515
|
|
|
'phpConfDir' => $configDir, |
516
|
|
|
'sqlDir' => $sqlDir, |
517
|
|
|
'migrationDir' => $migrationDir, |
518
|
|
|
'schemaDir' => $configDir, |
519
|
|
|
]; |
520
|
|
|
return $paths; |
521
|
|
|
} |
522
|
|
|
|
523
|
|
|
/** |
524
|
|
|
* @param string $module_path |
525
|
|
|
* @return GeneratorConfig |
526
|
|
|
*/ |
527
|
|
|
private function getConfigGenerator($module_path) |
528
|
|
|
{ |
529
|
|
|
// Generate the configurator |
530
|
|
|
$paths = $this->getPropelPaths($module_path); |
531
|
|
|
foreach ($paths as $path) { |
532
|
|
|
GeneratorHelper::createDir($path); |
533
|
|
|
} |
534
|
|
|
$configGenerator = new GeneratorConfig($paths['phpConfDir'], [ |
535
|
|
|
'propel' => [ |
536
|
|
|
'paths' => $paths, |
537
|
|
|
] |
538
|
|
|
]); |
539
|
|
|
return $configGenerator; |
540
|
|
|
} |
541
|
|
|
|
542
|
|
|
/** |
543
|
|
|
* @param GeneratorConfig $configGenerator |
544
|
|
|
*/ |
545
|
|
|
private function buildModels(GeneratorConfig $configGenerator) |
546
|
|
|
{ |
547
|
|
|
$manager = new ModelManager(); |
548
|
|
|
$manager->setFilesystem(new Filesystem()); |
549
|
|
|
$this->setupManager($configGenerator, $manager); |
550
|
|
|
$manager->build(); |
551
|
|
|
} |
552
|
|
|
|
553
|
|
|
/** |
554
|
|
|
* @param GeneratorConfig $configGenerator |
555
|
|
|
*/ |
556
|
|
|
private function buildSql(GeneratorConfig $configGenerator) |
557
|
|
|
{ |
558
|
|
|
$manager = new SqlManager(); |
559
|
|
|
$connections = $configGenerator->getBuildConnections(); |
560
|
|
|
$manager->setConnections($connections); |
561
|
|
|
$manager->setValidate(true); |
562
|
|
|
$this->setupManager($configGenerator, $manager, $configGenerator->getSection('paths')['sqlDir']); |
563
|
|
|
|
564
|
|
|
$manager->buildSql(); |
565
|
|
|
} |
566
|
|
|
|
567
|
|
|
/** |
568
|
|
|
* @param GeneratorConfig $configGenerator |
569
|
|
|
* @param AbstractManager $manager |
570
|
|
|
* @param string $workingDir |
571
|
|
|
*/ |
572
|
|
|
private function setupManager(GeneratorConfig $configGenerator, AbstractManager &$manager, $workingDir = CORE_DIR) |
573
|
|
|
{ |
574
|
|
|
$manager->setGeneratorConfig($configGenerator); |
575
|
|
|
$schemaFile = new \SplFileInfo($configGenerator->getSection('paths')['schemaDir'] . DIRECTORY_SEPARATOR . 'schema.xml'); |
576
|
|
|
$manager->setSchemas([$schemaFile]); |
577
|
|
|
$manager->setLoggerClosure(function ($message) { |
578
|
|
|
Logger::log($message, LOG_INFO); |
579
|
|
|
}); |
580
|
|
|
$manager->setWorkingDirectory($workingDir); |
581
|
|
|
} |
582
|
|
|
|
583
|
|
|
/** |
584
|
|
|
* @param $module |
585
|
|
|
* @param $force |
586
|
|
|
* @param $apiClass |
587
|
|
|
* @param \Directory|null $dir |
588
|
|
|
* @param string $apiPath |
589
|
|
|
* @param string $package |
590
|
|
|
*/ |
591
|
|
|
private function generateApiFiles($module, $force, $apiClass, \Directory $dir, string $apiPath, $package = null) |
592
|
|
|
{ |
593
|
|
|
$base = $dir->path; |
594
|
|
|
while ($file = $dir->read()) { |
595
|
|
|
if (!in_array(strtolower($file), ['.', '..', 'base', 'map'])) { |
596
|
|
|
if (is_dir($base . DIRECTORY_SEPARATOR . $file)) { |
597
|
|
|
$this->generateApiFiles($module, $force, $apiClass, dir($base . DIRECTORY_SEPARATOR . $file), $apiPath . DIRECTORY_SEPARATOR . $file, $file); |
|
|
|
|
598
|
|
|
} else if (!preg_match('/Query\.php$/i', $file) |
599
|
|
|
&& !preg_match('/I18n\.php$/i', $file) |
600
|
|
|
&& preg_match('/\.php$/i', $file) |
601
|
|
|
) { |
602
|
|
|
$filename = str_replace(".php", "", $file); |
603
|
|
|
$this->log->addLog("Generamos Api BASES para {$filename}"); |
604
|
|
|
if($this->checkIfIsModel($module, $filename, $package)) { |
605
|
|
|
$this->createApiBaseFile($module, $apiPath, $filename, $apiClass, $package); |
606
|
|
|
$this->createApi($module, $apiPath, $force, $filename, $package); |
607
|
|
|
} |
608
|
|
|
} |
609
|
|
|
} |
610
|
|
|
} |
611
|
|
|
} |
612
|
|
|
|
613
|
|
|
/** |
614
|
|
|
* @param string $module |
615
|
|
|
* @param string $package |
616
|
|
|
* @param string $filename |
617
|
|
|
* @return bool |
618
|
|
|
* @throws \ReflectionException |
619
|
|
|
*/ |
620
|
|
|
private function checkIfIsModel($module, $filename, $package = null) |
621
|
|
|
{ |
622
|
|
|
$parts = [$module, 'Models']; |
623
|
|
|
if (strlen($package)) { |
624
|
|
|
$parts[] = $package; |
625
|
|
|
} |
626
|
|
|
$parts[] = $filename; |
627
|
|
|
$namespace = '\\' . implode('\\', $parts); |
628
|
|
|
$reflectorClass = new \ReflectionClass($namespace); |
629
|
|
|
$isModel = $reflectorClass->isInstantiable(); |
630
|
|
|
return $isModel; |
631
|
|
|
} |
632
|
|
|
} |
633
|
|
|
|