1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace Classes; |
4
|
|
|
|
5
|
|
|
use Classes\AdapterConfig\None; |
6
|
|
|
use Classes\AdapterConfig\Phalcon; |
7
|
|
|
use Classes\AdapterConfig\ZendFrameworkOne; |
8
|
|
|
use Classes\AdapterMakerFile\AbstractAdapter; |
9
|
|
|
use Classes\AdaptersDriver\Dblib; |
10
|
|
|
use Classes\AdaptersDriver\Mssql; |
11
|
|
|
use Classes\AdaptersDriver\Mysql; |
12
|
|
|
use Classes\AdaptersDriver\Pgsql; |
13
|
|
|
use Classes\AdaptersDriver\Sqlsrv; |
14
|
|
|
|
15
|
|
|
require_once 'AdapterConfig/None.php'; |
16
|
|
|
require_once 'AdapterConfig/Phalcon.php'; |
17
|
|
|
require_once 'AdapterConfig/ZendFrameworkOne.php'; |
18
|
|
|
require_once 'AdaptersDriver/Dblib.php'; |
19
|
|
|
require_once 'AdaptersDriver/Mssql.php'; |
20
|
|
|
require_once 'AdaptersDriver/Mysql.php'; |
21
|
|
|
require_once 'AdaptersDriver/Pgsql.php'; |
22
|
|
|
require_once 'AdaptersDriver/Sqlsrv.php'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @author Pedro Alarcao <[email protected]> |
26
|
|
|
* @link https://github.com/pedro151/orm-generator |
27
|
|
|
*/ |
28
|
|
|
class Config |
29
|
|
|
{ |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
public static $version = "1.4.9"; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* String that separates the parent section name |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $sectionSeparator = ':'; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
private $configIniDefault = '/configs/config.ini'; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
public $_basePath; |
52
|
|
|
/** |
53
|
|
|
* @var array |
54
|
|
|
*/ |
55
|
|
|
private $argv = array (); |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var \Classes\AdapterConfig\AbstractAdapter |
59
|
|
|
*/ |
60
|
|
|
private $adapterConfig; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var \Classes\AdaptersDriver\AbsractAdapter |
64
|
|
|
*/ |
65
|
|
|
private $adapterDriver; |
66
|
|
|
|
67
|
|
|
private $frameworkList = array ( |
68
|
|
|
'none' , |
69
|
|
|
'zf1' , |
70
|
|
|
'phalcon' |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
private $parameterList = array ( |
74
|
|
|
'init' => 'Creates the necessary configuration file to start using the orm-generator.' , |
75
|
|
|
'name-ini' => 'reference to another .ini file configuration (relative path).' , |
76
|
|
|
'config-env' => 'orm-generator configuration environment.' , |
77
|
|
|
'framework' => 'name framework used, which has the contents of the database configurations and framework template.' , |
78
|
|
|
'driver' => 'database driver name (Ex.: pgsql).' , |
79
|
|
|
'database' => 'database name.' , |
80
|
|
|
'schema' => 'database schema name (one or more than one).' , |
81
|
|
|
'tables' => 'table name (parameter can be used more then once).' , |
82
|
|
|
'clean-trash' => 'delete all files that do not belong to your Database due' , |
83
|
|
|
'status' => 'show status of implementation carried out after completing the process.' , |
84
|
|
|
'version' => 'shows the version of orm-generator.' , |
85
|
|
|
'help' => "help command explaining all the options and manner of use." , |
86
|
|
|
'path' => "specify where to create the files (default is current directory)." , |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
public function __construct ( $argv , $basePath , $numArgs ) |
90
|
|
|
{ |
91
|
|
|
if ( array_key_exists ( 'help' , $argv ) or ( $numArgs > 1 |
|
|
|
|
92
|
|
|
&& count ( $argv ) < 1 ) |
93
|
|
|
) |
94
|
|
|
{ |
95
|
|
|
die ( $this->getUsage () ); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
if ( array_key_exists ( 'version' , $argv ) ) |
98
|
|
|
{ |
99
|
|
|
die ( $this->getVersion () ); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
if ( array_key_exists ( 'status' , $argv ) ) |
102
|
|
|
{ |
103
|
|
|
$argv[ 'status' ] = true; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$this->argv = $this->parseConfig ( $basePath , $argv ); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Lista de ajuda quando digita 'help' |
111
|
|
|
* |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
public function getUsage () |
115
|
|
|
{ |
116
|
|
|
$version = $this->getVersion (); |
117
|
|
|
$list = $this->renderParam (); |
118
|
|
|
|
119
|
|
|
return <<<EOF |
120
|
|
|
parameters: |
121
|
|
|
$list |
122
|
|
|
example: php generate.php --framework=zf1 --database=foo --table=foobar --status |
123
|
|
|
|
124
|
|
|
$version |
125
|
|
|
EOF; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function renderParam () |
129
|
|
|
{ |
130
|
|
|
$return = ""; |
131
|
|
|
foreach ( $this->parameterList as $param => $desc ) |
132
|
|
|
{ |
133
|
|
|
if ( strlen ( $param ) < 5 ) |
134
|
|
|
{ |
135
|
|
|
$return .= "\t--" . $param . "\t\t: " . $desc . "\n"; |
136
|
|
|
} else |
137
|
|
|
{ |
138
|
|
|
$return .= "\t--" . $param . "\t: " . $desc . "\n"; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $return; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function checkHasNewVersion () |
147
|
|
|
{ |
148
|
|
|
if ( ! ini_get ( 'file_get_contents' ) ) |
149
|
|
|
{ |
150
|
|
|
return; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$opts = array ( |
154
|
|
|
'http' => array ( |
155
|
|
|
'method' => 'GET' , |
156
|
|
|
'header' => array ( |
157
|
|
|
'User-Agent: PHP' |
158
|
|
|
) |
159
|
|
|
) |
160
|
|
|
); |
161
|
|
|
$context = stream_context_create($opts); |
162
|
|
|
$tags = json_decode ( file_get_contents ( "https://api.github.com/repos/pedro151/orm-generator/tags" , false, $context) ); |
163
|
|
|
|
164
|
|
|
|
165
|
|
|
$lastVersion = preg_replace("/[^0-9.]/", "", $tags[ 0 ]->name); |
166
|
|
|
if ( $lastVersion > static::$version ) |
167
|
|
|
{ |
168
|
|
|
return "\033[0;31mThere is a new version $lastVersion available:\033[0m https://github.com/pedro151/orm-generator\n"; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function getVersion () |
173
|
|
|
{ |
174
|
|
|
$version = static::$version; |
175
|
|
|
|
176
|
|
|
return "ORM Generator By: Pedro Alarcao Version: $version\n".$this->checkHasNewVersion(); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Analisa e estrutura a Configuracao do generate |
181
|
|
|
* |
182
|
|
|
* @param string $basePath |
183
|
|
|
* @param array $argv |
184
|
|
|
* |
185
|
|
|
* @return array |
186
|
|
|
* @throws \Exception |
187
|
|
|
*/ |
188
|
|
|
private function parseConfig ( $basePath , $argv ) |
189
|
|
|
{ |
190
|
|
|
$this->_basePath = $basePath; |
191
|
|
|
|
192
|
|
|
$configIni = isset( $argv[ 'name-ini' ] ) |
193
|
|
|
? $argv[ 'name-ini' ] |
194
|
|
|
: $this->_basePath |
195
|
|
|
. $this->configIniDefault; |
196
|
|
|
|
197
|
|
|
$configTemp = $this->loadIniFile ( realpath ( $configIni ) ); |
198
|
|
|
$configCurrent = self::parseConfigEnv ( $configTemp , $argv ); |
199
|
|
|
|
200
|
|
|
if ( ! isset( $configCurrent[ 'framework' ] ) ) |
201
|
|
|
{ |
202
|
|
|
throw new \Exception( "configure which framework you want to use! \n" ); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
if ( ! in_array ( $configCurrent[ 'framework' ] , $this->frameworkList ) ) |
206
|
|
|
{ |
207
|
|
|
$frameworks = implode ( "\n\t" , $this->frameworkList ); |
208
|
|
|
throw new \Exception( "list of frameworks: \n\t\033[1;33m" . $frameworks |
209
|
|
|
. "\n\033[0m" ); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
return $argv + array_filter ( $configCurrent ); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* |
217
|
|
|
* @param $configTemp |
218
|
|
|
* @param $argv |
219
|
|
|
* |
220
|
|
|
* @return string |
221
|
|
|
*/ |
222
|
|
|
private static function parseConfigEnv ( $configTemp , $argv ) |
223
|
|
|
{ |
224
|
|
|
$thisSection = isset( $configTemp[ key ( $configTemp ) ][ 'config-env' ] ) |
225
|
|
|
? $configTemp[ key ( |
226
|
|
|
$configTemp |
227
|
|
|
) ][ 'config-env' ] : null; |
228
|
|
|
|
229
|
|
|
$thisSection = isset( $argv[ 'config-env' ] ) ? $argv[ 'config-env' ] |
230
|
|
|
: $thisSection; |
231
|
|
|
|
232
|
|
|
if ( isset( $configTemp[ $thisSection ][ 'extends' ] ) ) |
233
|
|
|
{ |
234
|
|
|
#faz marge da config principal com a config extendida |
235
|
|
|
return $configTemp[ $thisSection ] |
236
|
|
|
+ $configTemp[ $configTemp[ $thisSection ][ 'extends' ] ]; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
return $configTemp[ key ( $configTemp ) ]; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Carregar o arquivo ini e pré-processa o separador de seção ':' |
244
|
|
|
* no nome da seção (que é usado para a extensão seção) de modo a que a |
245
|
|
|
* matriz resultante tem os nomes de seção corretos e as informações de |
246
|
|
|
* extensão é armazenado em uma sub-ch ve |
247
|
|
|
* |
248
|
|
|
* @param string $filename |
249
|
|
|
* |
250
|
|
|
* @throws \Exception |
251
|
|
|
* @return array |
252
|
|
|
*/ |
253
|
|
|
protected function loadIniFile ( $filename ) |
254
|
|
|
{ |
255
|
|
|
if ( ! is_file ( $filename ) ) |
256
|
|
|
{ |
257
|
|
|
throw new \Exception( "\033[0;31mError: configuration file does not exist! \033[0m\n" ); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
$loaded = parse_ini_file ( $filename , true ); |
261
|
|
|
$iniArray = array (); |
262
|
|
|
foreach ( $loaded as $key => $data ) |
263
|
|
|
{ |
264
|
|
|
$pieces = explode ( $this->sectionSeparator , $key ); |
265
|
|
|
$thisSection = trim ( $pieces[ 0 ] ); |
266
|
|
|
switch ( count ( $pieces ) ) |
267
|
|
|
{ |
268
|
|
|
case 1: |
269
|
|
|
$iniArray[ $thisSection ] = $data; |
270
|
|
|
break; |
271
|
|
|
|
272
|
|
|
case 2: |
273
|
|
|
$extendedSection = trim ( $pieces[ 1 ] ); |
274
|
|
|
$iniArray[ $thisSection ] = array_merge ( array ( 'extends' => $extendedSection ) , $data ); |
275
|
|
|
break; |
276
|
|
|
|
277
|
|
|
default: |
278
|
|
|
throw new \Exception( "Section '$thisSection' may not extend multiple sections in $filename" ); |
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
return $iniArray; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* analisa a opção e cria a instancia do Atapter do determinado framework |
287
|
|
|
* |
288
|
|
|
* @return \Classes\AdapterConfig\AbstractAdapter |
289
|
|
|
* |
290
|
|
|
*/ |
291
|
|
|
private function factoryConfig () |
292
|
|
|
{ |
293
|
|
|
switch ( strtolower ( $this->argv[ 'framework' ] ) ) |
294
|
|
|
{ |
295
|
|
|
case 'zf1': |
296
|
|
|
return new ZendFrameworkOne( $this->argv ); |
297
|
|
|
case 'phalcon': |
298
|
|
|
return new Phalcon( $this->argv ); |
299
|
|
|
default: |
300
|
|
|
return new None( $this->argv ); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Analisa a opção e instancia o determinado banco de dados |
307
|
|
|
* |
308
|
|
|
* @param AdapterConfig\AbstractAdapter $config |
309
|
|
|
* |
310
|
|
|
* @return AdaptersDriver\AbsractAdapter |
311
|
|
|
*/ |
312
|
|
|
private function factoryDriver ( AdapterConfig\AbstractAdapter $config ) |
313
|
|
|
{ |
314
|
|
|
switch ( $this->argv[ 'driver' ] ) |
315
|
|
|
{ |
316
|
|
|
case 'pgsql': |
317
|
|
|
case 'pdo_pgsql': |
318
|
|
|
return new Pgsql( $config ); |
319
|
|
|
case 'mysql': |
320
|
|
|
case 'pdo_mysql': |
321
|
|
|
return new Mysql( $config ); |
322
|
|
|
case 'mssql': |
323
|
|
|
return new Mssql( $config ); |
324
|
|
|
case 'dblib': |
325
|
|
|
return new Dblib( $config ); |
326
|
|
|
case 'sqlsrv': |
327
|
|
|
return new Sqlsrv( $config ); |
328
|
|
|
} |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* @return AdapterConfig\AbstractAdapter |
333
|
|
|
*/ |
334
|
|
|
public function getAdapterConfig () |
335
|
|
|
{ |
336
|
|
|
if ( ! $this->adapterConfig instanceof AbstractAdapter ) |
337
|
|
|
{ |
338
|
|
|
$this->adapterConfig = $this->factoryConfig (); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
return $this->adapterConfig; |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* @return AdaptersDriver\AbsractAdapter |
346
|
|
|
*/ |
347
|
|
|
public function getAdapterDriver ( AdapterConfig\AbstractAdapter $config ) |
348
|
|
|
{ |
349
|
|
|
if ( ! $this->adapterDriver ) |
350
|
|
|
{ |
351
|
|
|
$this->adapterDriver = $this->factoryDriver ( $config ); |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
return $this->adapterDriver; |
355
|
|
|
} |
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.