1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace Classes; |
4
|
|
|
|
5
|
|
|
use Classes\AdapterMakerFile\AbstractAdapter; |
6
|
|
|
use Classes\AdapterMakerFile\FilesFixeds; |
7
|
|
|
use Classes\Maker\AbstractMaker; |
8
|
|
|
use Classes\Update\ProgressBar; |
9
|
|
|
|
10
|
|
|
require_once 'Classes/AdapterMakerFile/AbstractAdapter.php'; |
11
|
|
|
require_once 'Classes/AdapterMakerFile/FilesFixeds.php'; |
12
|
|
|
require_once 'Classes/Maker/AbstractMaker.php'; |
13
|
|
|
require_once 'Classes/CleanTrash.php'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @author Pedro Alarcao <[email protected]> |
17
|
|
|
* @link https://github.com/pedro151/orm-generator |
18
|
|
|
*/ |
19
|
|
|
class MakerFile extends AbstractMaker |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @type string[] |
24
|
|
|
*/ |
25
|
|
|
public $location = array (); |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* caminho de pastas Base |
29
|
|
|
* |
30
|
|
|
* @type string |
31
|
|
|
*/ |
32
|
|
|
private $baseLocation = ''; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @type \Classes\AdapterConfig\AbstractAdapter |
36
|
|
|
*/ |
37
|
|
|
private $config; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @type \Classes\AdaptersDriver\AbsractAdapter |
41
|
|
|
*/ |
42
|
|
|
private $driver; |
43
|
|
|
|
44
|
|
|
private $countDir; |
45
|
|
|
private $max; |
46
|
|
|
|
47
|
|
|
private $msgReservedWord = "\033[0mPlease enter the value for reserved word \033[0;31m'%index%' \033[1;33m[%config%]:\033[0m "; |
48
|
|
|
|
49
|
|
|
public function __construct ( Config $config ) |
50
|
|
|
{ |
51
|
|
|
$this->config = $config->getAdapterConfig (); |
52
|
|
|
$this->parseReservedWord ( $this->getConfig () ); |
53
|
|
|
$this->driver = $config->getAdapterDriver ( $this->getConfig () ); |
54
|
|
|
$this->parseLocation ( $config->_basePath ); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param AdapterConfig\AbstractAdapter $config |
59
|
|
|
*/ |
60
|
|
|
public function parseReservedWord ( AdapterConfig\AbstractAdapter $config ) |
61
|
|
|
{ |
62
|
|
|
$palavrasReservadas = $config->reservedWord; |
63
|
|
|
if ( ! $palavrasReservadas ) |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
return; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$schema = $config->getSchemas (); |
69
|
|
|
$db = $config->getDatabase (); |
70
|
|
|
$hasSchema = array_intersect ( $schema , array_flip ( $palavrasReservadas ) ); |
71
|
|
|
$hasDatabase = in_array ( $db , $palavrasReservadas ); |
72
|
|
|
if ( ! ( $hasSchema or $hasDatabase ) ) |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
return; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
echo "- database has reserved words\n"; |
78
|
|
View Code Duplication |
foreach ( $palavrasReservadas as $index => $config ) |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
$attribs = array ( |
81
|
|
|
"%index%" => $index , |
82
|
|
|
"%config%" => $config |
83
|
|
|
); |
84
|
|
|
echo strtr ( $this->msgReservedWord , $attribs ); |
85
|
|
|
$line = trim ( fgets ( STDIN ) ); |
86
|
|
|
if ( ! empty( $line ) ) |
87
|
|
|
{ |
88
|
|
|
$this->getConfig ()->reservedWord[ $index ] = $line; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param array $arrFoldersName |
95
|
|
|
* |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
private function filterLocation ( $arrFoldersName ) |
99
|
|
|
{ |
100
|
|
|
foreach ( $arrFoldersName as $index => $folderName ) |
101
|
|
|
{ |
102
|
|
|
$arrFoldersName[ $index ] = $this->getConfig () |
103
|
|
|
->replaceReservedWord ( $folderName ); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return implode ( DIRECTORY_SEPARATOR , array_filter ( $arrFoldersName ) ); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Analisa os caminhos das pastas base |
111
|
|
|
*/ |
112
|
|
|
public function parseLocation ( $basePath ) |
113
|
|
|
{ |
114
|
|
|
|
115
|
|
|
$arrBase = array ( |
116
|
|
|
$basePath , |
117
|
|
|
$this->config->path |
|
|
|
|
118
|
|
|
); |
119
|
|
|
|
120
|
|
|
$this->baseLocation = $this->filterLocation ( $arrBase ); |
121
|
|
|
|
122
|
|
|
# pasta com nome do driver do banco |
123
|
|
|
$driverBase = ''; |
124
|
|
|
if ( (bool) @$this->config->{"folder-database"} ) |
125
|
|
|
{ |
126
|
|
|
$classDriver = explode ( '\\' , get_class ( $this->driver ) ); |
127
|
|
|
$driverBase = end ( $classDriver ); |
128
|
|
|
} |
129
|
|
|
$folderName = ''; |
130
|
|
|
if ( (bool) @$this->config->{"folder-name"} ) |
131
|
|
|
{ |
132
|
|
|
$folderName = $this->getClassName ( trim ( $this->config->{"folder-name"} ) ); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if ( $this->config->hasSchemas () ) |
136
|
|
|
{ |
137
|
|
|
|
138
|
|
|
$schemas = $this->config->getSchemas (); |
139
|
|
|
foreach ( $schemas as $schema ) |
140
|
|
|
{ |
141
|
|
|
$arrUrl = array ( |
142
|
|
|
$this->baseLocation , |
143
|
|
|
$driverBase , |
144
|
|
|
$folderName , |
145
|
|
|
$this->getClassName ( $schema ) |
146
|
|
|
); |
147
|
|
|
|
148
|
|
|
$this->location[ $schema ] = $this->filterLocation ( $arrUrl ); |
149
|
|
|
unset( $arrUrl ); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
} else |
153
|
|
|
{ |
154
|
|
|
$url = array ( |
155
|
|
|
$this->baseLocation , |
156
|
|
|
$driverBase , |
157
|
|
|
$folderName , |
158
|
|
|
$this->getClassName ( |
159
|
|
|
$this->getConfig () |
160
|
|
|
->getDatabase () |
161
|
|
|
) |
162
|
|
|
); |
163
|
|
|
$this->location = array ( $this->filterLocation ( $url ) ); |
164
|
|
|
unset( $url ); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @return AdapterConfig\AbstractAdapter |
170
|
|
|
*/ |
171
|
|
|
public function getConfig () |
172
|
|
|
{ |
173
|
|
|
return $this->config; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/* Get current time */ |
177
|
|
|
public function startTime () |
178
|
|
|
{ |
179
|
|
|
echo "\033[1;32mStarting..\033[0m\n"; |
180
|
|
|
$this->startTime = microtime ( true ); |
|
|
|
|
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
private function getRunTime () |
184
|
|
|
{ |
185
|
|
|
return round ( ( microtime ( true ) - $this->startTime ) , 3 ); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param $objMakeFile |
190
|
|
|
*/ |
191
|
|
|
private function generateFilesFixed ( FilesFixeds $objFilesFixeds ) |
192
|
|
|
{ |
193
|
|
|
if ( $objFilesFixeds->hasData () ) |
194
|
|
|
{ |
195
|
|
|
$file = $this->baseLocation |
196
|
|
|
. DIRECTORY_SEPARATOR |
197
|
|
|
. $objFilesFixeds->getFileName () |
198
|
|
|
. '.php'; |
199
|
|
|
|
200
|
|
|
$tpl = $this->getParsedTplContents ( $objFilesFixeds->getTpl () ); |
201
|
|
|
self::makeSourcer ( $file , $tpl , true ); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Executa o Make, criando arquivos e Diretorios |
207
|
|
|
*/ |
208
|
|
|
public function run () |
209
|
|
|
{ |
210
|
|
|
$cur = 0; |
211
|
|
|
$numFilesCreated = 0; |
212
|
|
|
$numFilesIgnored = 0; |
213
|
|
|
|
214
|
|
|
$this->startTime (); |
215
|
|
|
$this->waitOfDatabase ( $cur ); |
216
|
|
|
|
217
|
|
|
$this->max = $this->driver->getTotalTables () * $this->countDiretory (); |
218
|
|
|
$progressBar = ProgressBar::getInstance ()->setMaxByte ( $this->max ); |
219
|
|
|
|
220
|
|
|
foreach ( $this->location as $schema => $location ) |
221
|
|
|
{ |
222
|
|
|
foreach ( $this->factoryMakerFile () as $objMakeFile ) |
223
|
|
|
{ |
224
|
|
|
$path = $location . DIRECTORY_SEPARATOR . $objMakeFile->getPastName (); |
225
|
|
|
if ( $this->config->isCleanTrash () ) |
226
|
|
|
{ |
227
|
|
|
CleanTrash::getInstance ()->run ( $path , $this->driver , $schema ); |
228
|
|
|
} |
229
|
|
|
self::makeDir ( $path ); |
230
|
|
|
|
231
|
|
|
#Cria as Classes de Exceção e Abstratas |
232
|
|
|
foreach ( $objMakeFile->getListFilesFixed () as $nameObject ) |
233
|
|
|
{ |
234
|
|
|
$this->generateFilesFixed ( $objMakeFile->getFilesFixeds ( $nameObject ) ); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
#Cria as Classes do Banco |
238
|
|
|
foreach ( $this->driver->getTables ( $schema ) as $key => $objTables ) |
239
|
|
|
{ |
240
|
|
|
$file = $path . DIRECTORY_SEPARATOR |
241
|
|
|
. self::getClassName ( $objTables->getName () ) . '.php'; |
242
|
|
|
|
243
|
|
|
$tpl = $this->getParsedTplContents ( |
244
|
|
|
$objMakeFile->getFileTpl () , |
245
|
|
|
$objMakeFile->parseRelation ( $this , $objTables ) , |
246
|
|
|
$objTables , |
247
|
|
|
$objMakeFile |
248
|
|
|
|
249
|
|
|
); |
250
|
|
|
if ( self::makeSourcer ( $file , $tpl , $objMakeFile->isOverwrite () ) ) |
251
|
|
|
{ |
252
|
|
|
++ $numFilesCreated; |
253
|
|
|
} else |
254
|
|
|
{ |
255
|
|
|
++ $numFilesIgnored; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
++$cur; |
259
|
|
|
$progressBar->setProgress ( $cur )->render(); |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
$this->reportProcess ( $numFilesCreated , $numFilesIgnored ); |
265
|
|
|
$progressBar->finish(); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
private function waitOfDatabase ( &$cur ) |
|
|
|
|
269
|
|
|
{ |
270
|
|
|
printf ( "\033[1;33mWait, the database is being analyzed..\033[0m\n" ); |
271
|
|
|
$this->driver->runDatabase (); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
private function reportProcess ( $numFilesCreated = 0 , $numFilesIgnored = 0 ) |
275
|
|
|
{ |
276
|
|
|
if ( $this->config->isStatusEnabled () ) |
277
|
|
|
{ |
278
|
|
|
$databases = count ( $this->location ); |
279
|
|
|
$countDir = $this->countDiretory (); |
280
|
|
|
$totalTable = $this->driver->getTotalTables (); |
281
|
|
|
$totalFiles = $numFilesIgnored + $numFilesCreated; |
282
|
|
|
$totalFilesDeleted = CleanTrash::getInstance ()->getNumFilesDeleted (); |
283
|
|
|
echo "\n------"; |
284
|
|
|
printf ( "\n\r-Files generated/updated: \033[1;33m%s\033[0m" , $numFilesCreated ); |
285
|
|
|
printf ( "\n\r-Files not upgradeable: \033[1;33m%s\033[0m" , $numFilesIgnored ); |
286
|
|
|
printf ( "\n\r-Files deleted: \033[1;33m%s\033[0m" , $totalFilesDeleted ); |
287
|
|
|
printf ( "\n\r-Total files analyzed: \033[1;33m%s of %s\033[0m" , $totalFiles , $this->max ); |
288
|
|
|
printf ( "\n\r-Diretories: \033[1;33m%s\033[0m" , $databases * $countDir ); |
289
|
|
|
printf ( "\n\r-Scanned tables: \033[1;33m%s\033[0m" , $totalTable ); |
290
|
|
|
printf ( "\n\r-Execution time: \033[1;33m%ssec\033[0m" , $this->getRunTime () ); |
291
|
|
|
echo "\n------"; |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Instancia os Modulos de diretorios e tampletes |
297
|
|
|
* |
298
|
|
|
* @return AbstractAdapter[] |
299
|
|
|
*/ |
300
|
|
|
public function factoryMakerFile () |
301
|
|
|
{ |
302
|
|
|
return $this->config->getMakeFileInstances (); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* conta o numero de diretorios que serao criados |
307
|
|
|
* |
308
|
|
|
* @return int |
309
|
|
|
*/ |
310
|
|
|
public function countDiretory () |
311
|
|
|
{ |
312
|
|
|
if ( null === $this->countDir ) |
313
|
|
|
{ |
314
|
|
|
$this->countDir = 1; |
315
|
|
|
foreach ( $this->factoryMakerFile () as $abstractAdapter ) |
316
|
|
|
{ |
317
|
|
|
if ( $abstractAdapter->hasDiretory () ) |
318
|
|
|
{ |
319
|
|
|
++ $this->countDir; |
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
return $this->countDir; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* |
329
|
|
|
* parse a tpl file and return the result |
330
|
|
|
* |
331
|
|
|
* @param String $tplFile |
332
|
|
|
* |
333
|
|
|
* @return String |
334
|
|
|
*/ |
335
|
|
|
protected function getParsedTplContents ( |
336
|
|
|
$tplFile , $vars = array () , \Classes\Db\DbTable $objTables = null , |
337
|
|
|
$objMakeFile = null |
338
|
|
|
){ |
339
|
|
|
|
340
|
|
|
$arrUrl = array ( |
341
|
|
|
__DIR__ , |
342
|
|
|
'templates' , |
343
|
|
|
$this->config->framework , |
|
|
|
|
344
|
|
|
$tplFile |
345
|
|
|
); |
346
|
|
|
|
347
|
|
|
$filePath = implode ( DIRECTORY_SEPARATOR , filter_var_array ( $arrUrl ) ); |
348
|
|
|
|
349
|
|
|
extract ( $vars ); |
350
|
|
|
ob_start (); |
351
|
|
|
require $filePath; |
352
|
|
|
$data = ob_get_contents (); |
353
|
|
|
ob_end_clean (); |
354
|
|
|
|
355
|
|
|
return $data; |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
} |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.