1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace Classes\AdapterConfig; |
4
|
|
|
|
5
|
|
|
use Classes\Config; |
6
|
|
|
use Classes\Maker\AbstractMaker; |
7
|
|
|
|
8
|
|
|
require_once "Classes/Maker/AbstractMaker.php"; |
9
|
|
|
require_once "Classes/Config.php"; |
10
|
|
|
require_once 'Exception.php'; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @author Pedro Alarcao <[email protected]> |
14
|
|
|
* @link https://github.com/pedro151/orm-generator |
15
|
|
|
*/ |
16
|
|
|
abstract class AbstractAdapter |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
protected $arrConfig = array ( |
20
|
|
|
############################# DATABASE |
21
|
|
|
//Driver do banco de dados |
22
|
|
|
'driver' => null, |
23
|
|
|
//Nome do banco de dados |
24
|
|
|
'database' => null, |
25
|
|
|
//Host do banco |
26
|
|
|
'host' => 'localhost', |
27
|
|
|
//Port do banco |
28
|
|
|
'port' => '', |
29
|
|
|
//usuario do banco |
30
|
|
|
'username' => null, |
31
|
|
|
//senha do banco |
32
|
|
|
'password' => null, |
33
|
|
|
// lista de schemas do banco de dados |
34
|
|
|
'schema' => array (), |
35
|
|
|
'version' => '', |
36
|
|
|
'socket' => null, |
37
|
|
|
|
38
|
|
|
########################### DOCS |
39
|
|
|
// autor que gerou o script |
40
|
|
|
'author' => "Pedro", |
41
|
|
|
'license' => "New BSD License", |
42
|
|
|
'copyright' => "ORM Generator - Pedro151", |
43
|
|
|
'link' => 'https://github.com/pedro151/orm-generator', |
44
|
|
|
'version' => '', |
45
|
|
|
// data que foi gerado o script |
46
|
|
|
'last_modify' => null, |
47
|
|
|
|
48
|
|
|
########################## Ambiente/Arquivos |
49
|
|
|
|
50
|
|
|
// Nome do framework para o adapter |
51
|
|
|
'framework' => null, |
52
|
|
|
// namespace das classes |
53
|
|
|
'namespace' => "", |
54
|
|
|
// caminho onde os arquivos devem ser criados |
55
|
|
|
'path' => 'models', |
56
|
|
|
// flag para gerar pasta com o nome do driver do banco de dados |
57
|
|
|
'folder-database' => 0, |
58
|
|
|
// string com o nome da pastar personalizada |
59
|
|
|
'folder-name' => '', |
60
|
|
|
|
61
|
|
|
############################## Comandos adicionais |
62
|
|
|
//flag para mostrar o status da execução ao termino do processo |
63
|
|
|
'status' => false, |
64
|
|
|
// flags para criar todas as tabelas ou nao |
65
|
|
|
'allTables' => true, |
66
|
|
|
//Lista de tabelas a serem ignoradas |
67
|
|
|
'ignoreTable' => array (), |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var string[] um array com todos os campos obrigatorios |
72
|
|
|
*/ |
73
|
|
|
protected $attRequered = array ( |
74
|
|
|
'driver' => true, |
75
|
|
|
'database' => true, |
76
|
|
|
'host' => true, |
77
|
|
|
'username' => true, |
78
|
|
|
'path' => true |
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
protected $arrFunc = array (); |
82
|
|
|
|
83
|
|
|
private $framworkFiles = array (); |
84
|
|
|
|
85
|
|
|
public $reservedWord = array (); |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
private static $dataTypesDefault = array ( |
89
|
|
|
'int' => 'int', |
90
|
|
|
'float' => 'float', |
91
|
|
|
'string' => 'string', |
92
|
|
|
'text' => 'string', |
93
|
|
|
'date' => 'date', |
94
|
|
|
'datetime' => 'date', |
95
|
|
|
'boolean' => 'boolean' |
96
|
|
|
); |
97
|
|
|
|
98
|
|
|
protected $dataTypes = array (); |
99
|
|
|
|
100
|
|
|
const SEPARETOR = ""; |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* verifica se todos valores obrigatorios tem valor |
104
|
|
|
* |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
|
|
protected function checkConfig () |
108
|
|
|
{ |
109
|
|
|
if ( array_diff_key ( $this->attRequered, array_filter ( $this->arrConfig ) ) ) { |
110
|
|
|
return false; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return true; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* retorna os parametros da configuração do framework |
118
|
|
|
* |
119
|
|
|
* @return array |
120
|
|
|
*/ |
121
|
|
|
abstract protected function getParams (); |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Popula as config do generater com as configuraçoes do framework |
125
|
|
|
* |
126
|
|
|
* @return mixed |
127
|
|
|
*/ |
128
|
|
|
abstract protected function parseFrameworkConfig (); |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Cria Instancias dos arquivos que devem ser gerados |
132
|
|
|
* |
133
|
|
|
* @return \Classes\AdapterMakerFile\AbstractAdapter[] |
134
|
|
|
*/ |
135
|
|
|
abstract public function getMakeFileInstances (); |
136
|
|
|
|
137
|
|
|
abstract protected function init (); |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* retorna a base do Namespace |
141
|
|
|
* |
142
|
|
|
* @return array |
143
|
|
|
*/ |
144
|
|
|
protected function getBaseNamespace () |
145
|
|
|
{ |
146
|
|
|
return array ( |
147
|
|
|
$this->arrConfig[ 'namespace' ], |
148
|
|
|
'Model' |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param \Classes\Db\DbTable|\Classes\Db\Constrant $table |
154
|
|
|
* |
155
|
|
|
* @return mixed |
156
|
|
|
*/ |
157
|
|
|
|
158
|
|
|
public function createClassNamespace ( $table ) |
159
|
|
|
{ |
160
|
|
|
$arrNames = $this->getBaseNamespace (); |
161
|
|
|
|
162
|
|
|
if ( isset( $this->arrConfig[ 'folder-database' ] ) |
163
|
|
|
&& $this->arrConfig[ 'folder-database' ] |
164
|
|
|
) { |
165
|
|
|
$arrNames[] = AbstractMaker::getClassName ( $this->arrConfig[ 'driver' ] ); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
if ( isset( $this->arrConfig[ 'folder-name' ] ) |
169
|
|
|
&& $this->arrConfig[ 'folder-name' ] |
170
|
|
|
) { |
171
|
|
|
$arrNames[] = AbstractMaker::getClassName ( $this->arrConfig[ 'folder-name' ] ); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
if ( $table->hasSchema () ) { |
175
|
|
|
$arrNames[] = $this->replaceReservedWord ( AbstractMaker::getClassName ( $table->getSchema () ) ); |
176
|
|
|
} |
177
|
|
|
else { |
178
|
|
|
$arrNames[] = $this->replaceReservedWord ( AbstractMaker::getClassName ( $table->getDatabase () ) ); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return implode ( static::SEPARETOR, array_filter ( $arrNames ) ); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function __construct ( $array ) |
185
|
|
|
{ |
186
|
|
|
$this->dataTypes = $this->dataTypes + self::$dataTypesDefault; |
187
|
|
|
$array += array ( |
188
|
|
|
'version' => Config::$version, |
189
|
|
|
'author' => ucfirst ( get_current_user () ), |
190
|
|
|
'last_modify' => date ( "d-m-Y H:i:s." ) |
191
|
|
|
); |
192
|
|
|
|
193
|
|
|
$this->setFrameworkFiles ( $array ); |
194
|
|
|
$this->parseFrameworkConfig (); |
195
|
|
|
$this->setParams ( $this->getParams () ); |
196
|
|
|
$this->setParams ( $array ); |
197
|
|
|
$this->init (); |
198
|
|
|
if ( !$this->isValid () ) { |
199
|
|
|
$var = array_diff_key ( $this->attRequered, array_filter ( $this->arrConfig ) ); |
200
|
|
|
throw new Exception( $var ); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Set os arquivos de configuracao do framework |
206
|
|
|
* |
207
|
|
|
* @param $array |
208
|
|
|
*/ |
209
|
|
|
public function setFrameworkFiles ( $array ) |
210
|
|
|
{ |
211
|
|
|
$this->framworkFiles[ 'library' ] = isset( $array[ 'framework-path-library' ] ) ? $array[ 'framework-path-library' ] : null; |
212
|
|
|
|
213
|
|
|
$this->framworkFiles[ 'ini' ] = isset( $array[ 'framework-ini' ] ) ? $array[ 'framework-ini' ] : null; |
214
|
|
|
|
215
|
|
|
$this->framworkFiles[ 'environment' ] = isset( $array[ 'environment' ] ) ? $array[ 'environment' ] : 'production'; |
216
|
|
|
|
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
protected function isValidFrameworkFiles () |
220
|
|
|
{ |
221
|
|
|
if ( !is_file ( $this->framworkFiles[ 'ini' ] ) ) { |
222
|
|
|
return false; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
if ( !is_dir ( $this->framworkFiles[ 'library' ] ) ) { |
226
|
|
|
return false; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
if ( !isset ( $this->framworkFiles[ 'environment' ] ) or empty( $this->framworkFiles[ 'environment' ] ) ) { |
|
|
|
|
230
|
|
|
return false; |
231
|
|
|
} |
232
|
|
|
set_include_path ( |
233
|
|
|
implode ( |
234
|
|
|
PATH_SEPARATOR, |
235
|
|
|
array ( |
236
|
|
|
realpath ( $this->framworkFiles[ 'library' ] ), |
237
|
|
|
get_include_path (), |
238
|
|
|
) |
239
|
|
|
) |
240
|
|
|
); |
241
|
|
|
|
242
|
|
|
return true; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
protected function getFrameworkIni () |
246
|
|
|
{ |
247
|
|
|
return $this->framworkFiles[ 'ini' ]; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
protected function getEnvironment () |
251
|
|
|
{ |
252
|
|
|
return $this->framworkFiles[ 'environment' ]; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Popula as variaveis de acordo com o arquivo de configuração do seu framework |
257
|
|
|
*/ |
258
|
|
|
protected function isValid () |
259
|
|
|
{ |
260
|
|
|
return $this->checkConfig (); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
private function setParams ( $array ) |
264
|
|
|
{ |
265
|
|
|
if ( count ( $array ) > 0 ) { |
266
|
|
|
$this->arrConfig = array_filter ( $array ) + $this->arrConfig; |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* @return string |
272
|
|
|
*/ |
273
|
|
|
public function getDatabase () |
274
|
|
|
{ |
275
|
|
|
return $this->arrConfig[ 'database' ]; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* @return bool |
280
|
|
|
*/ |
281
|
|
|
public function hasSchemas () |
282
|
|
|
{ |
283
|
|
|
return !empty ( $this->arrConfig[ 'schema' ] ); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* @return string[] |
288
|
|
|
*/ |
289
|
|
|
public function getSchemas () |
290
|
|
|
{ |
291
|
|
|
if ( is_string ( $this->arrConfig[ 'schema' ] ) ) { |
292
|
|
|
return array ( $this->arrConfig[ 'schema' ] ); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
return $this->arrConfig[ 'schema' ]; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
public function setSchema ( $schema ) |
299
|
|
|
{ |
300
|
|
|
$this->arrConfig[ 'schema' ] = $schema; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* @return string |
305
|
|
|
*/ |
306
|
|
|
public function getHost () |
307
|
|
|
{ |
308
|
|
|
return $this->arrConfig[ 'host' ]; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* @return int |
313
|
|
|
*/ |
314
|
|
|
public function getPort () |
315
|
|
|
{ |
316
|
|
|
return $this->arrConfig[ 'port' ]; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* @return boolean |
321
|
|
|
*/ |
322
|
|
|
public function hasPort () |
323
|
|
|
{ |
324
|
|
|
return !empty( $this->arrConfig[ 'port' ] ); |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* @return string |
329
|
|
|
*/ |
330
|
|
|
public function getSocket () |
331
|
|
|
{ |
332
|
|
|
return $this->arrConfig[ 'socket' ]; |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* @return string |
337
|
|
|
*/ |
338
|
|
|
public function getUser () |
339
|
|
|
{ |
340
|
|
|
return $this->arrConfig[ 'username' ]; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* @return string |
345
|
|
|
*/ |
346
|
|
|
public function getPassword () |
347
|
|
|
{ |
348
|
|
|
return $this->arrConfig[ 'password' ]; |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* @param string $schema |
|
|
|
|
353
|
|
|
* |
354
|
|
|
* @return string |
355
|
|
|
*/ |
356
|
|
|
public function replaceReservedWord ( $palavra ) |
357
|
|
|
{ |
358
|
|
|
if ( isset( $this->reservedWord[ strtolower ( $palavra ) ] ) ) { |
359
|
|
|
return $this->reservedWord[ strtolower ( $palavra ) ]; |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
return $palavra; |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* @return bool |
367
|
|
|
*/ |
368
|
|
|
public function isStatusEnabled () |
369
|
|
|
{ |
370
|
|
|
return (bool) $this->arrConfig[ 'status' ]; |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* @param $str |
375
|
|
|
* |
376
|
|
|
* @return string |
377
|
|
|
*/ |
378
|
|
|
public function __get ( $str ) |
379
|
|
|
{ |
380
|
|
|
$arr = array ( |
381
|
|
|
'namespace', |
382
|
|
|
'framework', |
383
|
|
|
'author', |
384
|
|
|
'license', |
385
|
|
|
'version', |
386
|
|
|
'copyright', |
387
|
|
|
'link', |
388
|
|
|
'last_modify', |
389
|
|
|
'path', |
390
|
|
|
'folder-database', |
391
|
|
|
'folder-name' |
392
|
|
|
); |
393
|
|
|
|
394
|
|
|
if ( in_array ( $str, $arr ) ) { |
395
|
|
|
return $this->arrConfig[ strtolower ( $str ) ]; |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
return; |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
/** |
402
|
|
|
* @param string $type |
403
|
|
|
* |
404
|
|
|
* @return string |
405
|
|
|
*/ |
406
|
|
|
public static function convertTypeToPHP ( $type ) |
407
|
|
|
{ |
408
|
|
|
return self::$dataTypesDefault[ $type ]; |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
/** |
412
|
|
|
* @param string $type |
413
|
|
|
* |
414
|
|
|
* @return string |
415
|
|
|
*/ |
416
|
|
|
public function convertTypeToTypeFramework ( $type ) |
417
|
|
|
{ |
418
|
|
|
return $this->dataTypes[ $type ]; |
419
|
|
|
} |
420
|
|
|
} |
421
|
|
|
|
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.