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