Completed
Push — master ( 640ac7...39c4b0 )
by Rougin
03:24
created

SparkPlug::defineConstants()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 4
nop 0
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Rougin\SparkPlug;
4
5
/**
6
 * Spark Plug
7
 *
8
 * Returns the instance of CodeIgniter.
9
 *
10
 * @package SparkPlug
11
 * @author  Rougin Royce Gutib <[email protected]>
12
 */
13
class SparkPlug
14
{
15
    /**
16
     * @var array
17
     */
18
    protected $globals = [];
19
20
    /**
21
     * @var string
22
     */
23
    protected $path = '';
24
25
    /**
26
     * @var array
27
     */
28
    protected $server = [];
29
30
    /**
31
     * @param array  $globals
32
     * @param array  $server
33
     * @param string $path
34
     */
35 12
    public function __construct(array &$globals, array $server, $path = '')
36
    {
37 12
        $this->globals =& $globals;
38 12
        $this->path    = (empty($path)) ? getcwd() : $path;
39 12
        $this->server  = $server;
40 12
    }
41
42
    /**
43
     * Returns the instance of CodeIgniter.
44
     *
45
     * @return \CI_Controller
46
     */
47 12
    public function getCodeIgniter()
48
    {
49 12
        $this->setPathConstants();
50 12
        $this->prepareEnvironment();
51 12
        $this->defineConstants();
52 12
        $this->loadCommonClasses();
53 12
        $this->setCharacterSets();
54 12
        $this->setConfigurations();
55 12
        $this->loadCoreClasses();
56
57
        // Loads the get_instance.php for loading libraries
58 12
        require 'get_instance.php';
59
60 12
        $ci = \CI_Controller::get_instance();
61
62 12
        return (empty($ci)) ? new \CI_Controller : $ci;
63
    }
64
65
    /**
66
     * Loads the Common and the Base Controller class.
67
     *
68
     * @return void
69
     */
70 12
    protected function loadCommonClasses()
71
    {
72 12
        require BASEPATH . 'core/Common.php';
73
74 12
        class_exists('CI_Controller') || require BASEPATH . 'core/Controller.php';
75 12
    }
76
77
    /**
78
     * Loads the framework constants.
79
     *
80
     * @return void
81
     */
82 12
    protected function defineConstants()
83
    {
84 12
        $constants   = APPPATH . 'config/constants.php';
85 12
        $environment = APPPATH . 'config/' . ENVIRONMENT . '/constants.php';
86 12
        $filename    = file_exists($environment) ? $environment : $constants;
87
88 12
        defined('FILE_READ_MODE') || require $filename;
89 12
    }
90
91
    /**
92
     * Loads the CodeIgniter's core classes.
93
     *
94
     * @return void
95
     */
96 12
    protected function loadCoreClasses()
97
    {
98 12
        load_class('Loader', 'core');
99 12
        load_class('Router', 'core');
100 12
        load_class('Input', 'core');
101 12
        load_class('Lang', 'core');
102 12
    }
103
104
    /**
105
     * Sets the base path.
106
     *
107
     * @return void
108
     */
109 3
    protected function setBasePath()
110
    {
111 3
        $directory = new \RecursiveDirectoryIterator(getcwd());
112 3
        $iterator  = new \RecursiveIteratorIterator($directory);
113
114 3
        foreach ($iterator as $item) {
115 3
            $core = 'core' . DIRECTORY_SEPARATOR . 'CodeIgniter.php';
116
117 3
            if (strpos($item->getPathname(), $core) !== false) {
118 3
                $path = str_replace($core, '', $item->getPathname());
119
120 3
                define('BASEPATH', $path);
121
122 3
                break;
123
            }
124 3
        }
125 3
    }
126
127
    /**
128
     * Sets up important charset-related stuff.
129
     *
130
     * @return void
131
     */
132 12
    protected function setCharacterSets()
133
    {
134 12
        $charset = strtoupper(config_item('charset'));
135
136 12
        ini_set('default_charset', $charset);
137
138 12
        defined('MB_ENABLED') || define('MB_ENABLED', extension_loaded('mbstring'));
139
140
        // mbstring.internal_encoding is deprecated starting with PHP 5.6
141
        // and it's usage triggers E_DEPRECATED messages.
142 12
        if (! is_php('5.6') && ! ini_get('mbstring.internal_encoding')) {
143 2
            ini_set('mbstring.internal_encoding', $charset);
144 2
        }
145
146 12
        $this->setIconv();
147 12
    }
148
149
    /**
150
     * Sets up the current environment.
151
     *
152
     * @return void
153
     */
154 12
    protected function prepareEnvironment()
155
    {
156 12
        $environment = 'development';
157
158 12
        if (isset($this->server['CI_ENV'])) {
159 12
            $environment = $this->server['CI_ENV'];
160 12
        }
161
162 12
        defined('ENVIRONMENT') || define('ENVIRONMENT', $environment);
163 12
    }
164
165
    /**
166
     * Sets global configurations.
167
     *
168
     * @return void
169
     */
170 12
    protected function setConfigurations()
171
    {
172 12
        $this->globals['CFG'] =& load_class('Config', 'core');
173 12
        $this->globals['UNI'] =& load_class('Utf8', 'core');
174 12
        $this->globals['SEC'] =& load_class('Security', 'core');
175 12
    }
176
177
    /**
178
     * Sets up ICONV.
179
     *
180
     * @return void
181
     */
182 12
    protected function setIconv()
183
    {
184
        // This is required for mb_convert_encoding() to strip invalid
185
        // characters. That's utilized by CI_Utf8, but it's also done for
186
        // consistency with iconv.
187 12
        mb_substitute_character('none');
188
189
        // There's an ICONV_IMPL constant, but the PHP manual says that using
190
        // iconv's predefined constants is "strongly discouraged".
191 12
        defined('ICONV_ENABLED') || define('ICONV_ENABLED', extension_loaded('iconv'));
192 12
    }
193
194
    /**
195
     * Sets up the APPPATH, VENDOR, and BASEPATH constants.
196
     *
197
     * @return void
198
     */
199 12
    protected function setPathConstants()
200
    {
201
        $paths = [
202 12
            'APPPATH'  => $this->path . '/application/',
203 12
            'VENDOR'   => $this->path . '/vendor/',
204 12
            'VIEWPATH' => $this->path . '/application/views/',
205 12
        ];
206
207 12
        foreach ($paths as $key => $value) {
208 12
            defined($key) || define($key, $value);
209 12
        }
210
211 12
        defined('BASEPATH') || $this->setBasePath();
212 12
    }
213
}
214