GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( f4782c...853a03 )
by Pedro
06:07 queued 03:22
created

Config::parseConfig()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 10
Bugs 2 Features 1
Metric Value
c 10
b 2
f 1
dl 0
loc 23
rs 8.7972
cc 4
eloc 14
nc 6
nop 2
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 28 and the first side effect is on line 15.

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.

Loading history...
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.3.1";
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
    public function __construct ( $argv, $basePath )
74
    {
75
        if ( array_key_exists ( 'help', $argv ) ) {
76
            die ( $this->getUsage () );
0 ignored issues
show
Coding Style Compatibility introduced by
The method __construct() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
77
        }
78
        if ( array_key_exists ( 'status', $argv ) ) {
79
            $argv[ 'status' ] = true;
80
        }
81
82
        $this->argv = $this->parseConfig ( $basePath, $argv );
83
    }
84
85
    /**
86
     * Lista de ajuda quando digita 'help'
87
     *
88
     * @return string
89
     */
90
    public function getUsage ()
91
    {
92
        $version = $this->getVersion ();
93
94
        return <<<EOF
95
parameters:
96
97
    --init                : Creates the necessary configuration file to start using the orm-generator.
98
    --config-ini          : reference to another .ini file configuration (relative path).
99
    --config-env          : orm-generator configuration environment.
100
    --framework           : name framework used, which has the contents of the database configurations and framework template.
101
    --driver              : database driver name (Ex.: pgsql).
102
    --database            : database name.
103
 *  --schema              : database schema name (one or more than one).
104
    --status              : show status of implementation carried out after completing the process.
105
    --version             : shows the version of orm-generator.
106
    --help                : help command explaining all the options and manner of use.
107
    --path                  specify where to create the files (default is current directory).
108
109
 example: php generate.php --framework=zf1 --database=foo --table=foobar --status
110
111
$version
112
EOF;
113
    }
114
115
    public function getVersion ()
116
    {
117
        $version = static::$version;
118
119
        return "ORM Generator By: Pedro Alarcao Version: $version\n";
120
    }
121
122
    /**
123
     * Analisa e estrutura a Configuracao do generate
124
     *
125
     * @param  string $basePath
126
     * @param array   $argv
127
     *
128
     * @return array
129
     * @throws \Exception
130
     */
131
    private function parseConfig ( $basePath, $argv )
132
    {
133
        $this->_basePath = dirname ( $basePath );
134
135
        $configIni = isset( $argv[ 'config-ini' ] )
136
            ? $argv[ 'config-ini' ]
137
            : $this->_basePath
138
              . $this->configIniDefault;
139
140
        $configTemp    = $this->loadIniFile ( realpath ( $configIni ) );
141
        $configCurrent = self::parseConfigEnv ( $configTemp, $argv );
142
143
        if ( !isset( $configCurrent[ 'framework' ] ) ) {
144
            throw new \Exception( "configure which framework you want to use! \n" );
145
        }
146
147
        if ( !in_array ( $configCurrent[ 'framework' ], $this->frameworkList ) ) {
148
            $frameworks = implode ( "\n\t", $this->frameworkList );
149
            throw new \Exception( "list of frameworks: \n\t\033[1;33m" . $frameworks . "\n\033[0m" );
150
        }
151
152
        return $argv + array_filter ( $configCurrent );
153
    }
154
155
    /**
156
     *
157
     * @param $configTemp
158
     * @param $argv
159
     *
160
     * @return string
161
     */
162
    private static function parseConfigEnv ( $configTemp, $argv )
163
    {
164
        $thisSection = isset( $configTemp[ key ( $configTemp ) ][ 'config-env' ] ) ? $configTemp[ key (
165
            $configTemp
166
        ) ][ 'config-env' ] : null;
167
168
        $thisSection = isset( $argv[ 'config-env' ] ) ? $argv[ 'config-env' ] : $thisSection;
169
170
        if ( isset( $configTemp[ $thisSection ][ 'extends' ] ) ) {
171
            #faz marge da config principal com a config extendida
172
            return $configTemp[ $thisSection ] + $configTemp[ $configTemp[ $thisSection ][ 'extends' ] ];
173
        }
174
175
        return $configTemp[ key ( $configTemp ) ];
176
    }
177
178
    /**
179
     * Carregar o arquivo ini e pré-processa o separador de seção ':'
180
     * no nome da seção (que é usado para a extensão seção) de modo a que a
181
     * matriz resultante tem os nomes de seção corretos e as informações de
182
     * extensão é armazenado em uma sub-ch ve
183
     *
184
     * @param string $filename
185
     *
186
     * @throws \Exception
187
     * @return array
188
     */
189
    protected function loadIniFile ( $filename )
190
    {
191
        if ( !is_file ( $filename ) ) {
192
            throw new \Exception( "configuration file does not exist! \n" );
193
        }
194
195
        $loaded   = parse_ini_file ( $filename, true );
196
        $iniArray = array ();
197
        foreach ( $loaded as $key => $data ) {
198
            $pieces      = explode ( $this->sectionSeparator, $key );
199
            $thisSection = trim ( $pieces[ 0 ] );
200
            switch ( count ( $pieces ) ) {
201
                case 1:
202
                    $iniArray[ $thisSection ] = $data;
203
                    break;
204
205
                case 2:
206
                    $extendedSection          = trim ( $pieces[ 1 ] );
207
                    $iniArray[ $thisSection ] = array_merge ( array ( 'extends' => $extendedSection ), $data );
208
                    break;
209
210
                default:
211
                    throw new \Exception( "Section '$thisSection' may not extend multiple sections in $filename" );
212
            }
213
        }
214
215
        return $iniArray;
216
    }
217
218
    /**
219
     * analisa a opção e cria a instancia do Atapter do determinado framework
220
     *
221
     * @return \Classes\AdapterConfig\AbstractAdapter
222
     *
223
     */
224
    private function factoryConfig ()
225
    {
226
        switch ( strtolower ( $this->argv[ 'framework' ] ) ) {
227
            case 'zf1':
228
                return new ZendFrameworkOne( $this->argv );
229
            case 'phalcon':
230
                return new Phalcon( $this->argv );
231
            default:
232
                return new None( $this->argv );
233
        }
234
235
    }
236
237
    /**
238
     * Analisa a opção e instancia o determinado banco de dados
239
     *
240
     * @param AdapterConfig\AbstractAdapter $config
241
     *
242
     * @return AdaptersDriver\AbsractAdapter
243
     */
244
    private function factoryDriver ( AdapterConfig\AbstractAdapter $config )
245
    {
246
        switch ( $this->argv[ 'driver' ] ) {
247
            case 'pgsql':
248
            case 'pdo_pgsql':
249
                return new Pgsql( $config );
250
            case 'mysql':
251
            case 'pdo_mysql':
252
                return new Mysql( $config );
253
            case 'mssql':
254
                return new Mssql( $config );
255
            case 'dblib':
256
                return new Dblib( $config );
257
            case 'sqlsrv':
258
                return new Sqlsrv( $config );
259
        }
260
    }
261
262
    /**
263
     * @return AdapterConfig\AbstractAdapter
264
     */
265
    public function getAdapterConfig ()
266
    {
267
        if ( !$this->adapterConfig instanceof AbstractAdapter ) {
268
            $this->adapterConfig = $this->factoryConfig ();
269
        }
270
271
        return $this->adapterConfig;
272
    }
273
274
    /**
275
     * @return AdaptersDriver\AbsractAdapter
276
     */
277
    public function getAdapterDriver ( AdapterConfig\AbstractAdapter $config )
278
    {
279
        if ( !$this->adapterDriver ) {
280
            $this->adapterDriver = $this->factoryDriver ( $config );
281
        }
282
283
        return $this->adapterDriver;
284
    }
285
286
}
287