SparkPlug   A
last analyzed

Complexity

Total Complexity 32

Size/Duplication

Total Lines 255
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 9
Bugs 0 Features 1
Metric Value
wmc 32
eloc 69
c 9
b 0
f 1
dl 0
loc 255
ccs 88
cts 88
cp 1
rs 9.84

13 Methods

Rating   Name   Duplication   Size   Complexity  
A config() 0 9 1
A instance() 0 19 2
A charset() 0 11 4
A common() 0 9 2
A environment() 0 5 3
A basepath() 0 14 4
A constants() 0 11 3
A __construct() 0 17 2
A iconv() 0 5 3
A core() 0 11 1
A paths() 0 15 4
A getCodeIgniter() 0 3 1
A set() 0 11 2
1
<?php
2
3
namespace Rougin\SparkPlug;
4
5
/**
6
 * Spark Plug
7
 *
8
 * Returns Codeigniter applications as single variables.
9
 *
10
 * @package SparkPlug
11
 * @author  Rougin Gutib <[email protected]>
12
 */
13
class SparkPlug
14
{
15
    /**
16
     * @var array
17
     */
18
    protected $constants = array();
19
20
    /**
21
     * @var array
22
     */
23
    protected $globals = array();
24
25
    /**
26
     * @var string
27
     */
28
    protected $path = '';
29
30
    /**
31
     * @var array
32
     */
33
    protected $server = array();
34
35
    /**
36
     * @param array       $globals
37
     * @param array       $server
38
     * @param string|null $path
39
     */
40 24
    public function __construct(array &$globals, array $server, $path = null)
41
    {
42 24
        $this->globals =& $globals;
43
44 24
        $this->path = $path === null ? getcwd() : $path;
45
46 24
        $this->server = $server;
47
48 24
        $apppath = $this->path . '/application/';
49
50 24
        $this->constants['APPPATH'] = $apppath;
51
52 24
        $this->constants['ENVIRONMENT'] = 'development';
53
54 24
        $this->constants['VENDOR'] = $this->path . '/vendor/';
55
56 24
        $this->constants['VIEWPATH'] = $apppath . 'views/';
57 24
    }
58
59
    /**
60
     * Returns the Codeigniter singleton.
61
     * NOTE: To be removed in v1.0.0. Use instance() instead.
62
     *
63
     * @return \CI_Controller
64
     */
65 12
    public function getCodeIgniter()
66
    {
67 12
        return $this->instance();
68
    }
69
70
    /**
71
     * Returns the Codeigniter singleton.
72
     *
73
     * @return \CI_Controller
74
     */
75 24
    public function instance()
76
    {
77 24
        $this->paths();
78
79 24
        $this->environment($this->constants['ENVIRONMENT']);
80
81 24
        $this->constants();
82
83 24
        $this->common();
84
85 24
        $this->config();
86
87 24
        require 'helpers.php';
88
89 24
        $instance = \CI_Controller::get_instance();
90
91 24
        empty($instance) && $instance = new \CI_Controller;
92
93 24
        return $instance;
94
    }
95
96
    /**
97
     * Sets the constant with a value.
98
     *
99
     * @param string $key
100
     * @param string $value
101
     */
102 12
    public function set($key, $value)
103
    {
104 12
        $this->constants[$key] = $value;
105
106 12
        $same = $key === 'APPPATH';
107
108 12
        $path = $this->constants[$key] . '/views/';
109
110 12
        $same && $this->constants['VIEWPATH'] = $path;
111
112 12
        return $this;
113
    }
114
115
    /**
116
     * Sets the base path.
117
     *
118
     * @return void
119
     */
120 3
    protected function basepath()
121
    {
122 3
        $directory = new \RecursiveDirectoryIterator(getcwd());
123
124 3
        $iterator = new \RecursiveIteratorIterator($directory);
125
126 3
        foreach ($iterator as $item) {
127 3
            $core = 'core' . DIRECTORY_SEPARATOR . 'CodeIgniter.php';
128
129 3
            $exists = strpos($item->getPathname(), $core) !== false;
130
131 3
            $path = str_replace($core, '', $item->getPathname());
132
133 3
            $exists && ! defined('BASEPATH') && define('BASEPATH', $path);
134 1
        }
135 3
    }
136
137
    /**
138
     * Sets up important charset-related stuff.
139
     *
140
     * @return void
141
     */
142 24
    protected function charset()
143
    {
144 24
        ini_set('default_charset', $charset = strtoupper(config_item('charset')));
145
146 24
        defined('MB_ENABLED') || define('MB_ENABLED', extension_loaded('mbstring'));
147
148 24
        $encoding = 'mbstring.internal_encoding';
149
150 24
        ! is_php('5.6') && ! ini_get($encoding) && ini_set($encoding, $charset);
151
152 24
        $this->iconv();
153 24
    }
154
155
    /**
156
     * Loads the Common and the Base Controller class.
157
     *
158
     * @return void
159
     */
160 24
    protected function common()
161
    {
162 24
        $exists = class_exists('CI_Controller');
163
164 24
        require BASEPATH . 'core/Common.php';
165
166 24
        $exists || require BASEPATH . 'core/Controller.php';
167
168 24
        $this->charset();
169 24
    }
170
171
    /**
172
     * Sets global configurations.
173
     *
174
     * @return void
175
     */
176 24
    protected function config()
177
    {
178 24
        $this->globals['CFG'] =& load_class('Config', 'core');
179
180 24
        $this->globals['UNI'] =& load_class('Utf8', 'core');
181
182 24
        $this->globals['SEC'] =& load_class('Security', 'core');
183
184 24
        $this->core();
185 24
    }
186
187
    /**
188
     * Loads the framework constants.
189
     *
190
     * @return void
191
     */
192 24
    protected function constants()
193
    {
194 24
        $config = APPPATH . 'config/';
0 ignored issues
show
Bug introduced by
The constant Rougin\SparkPlug\APPPATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
195
196 24
        $constants = $config . ENVIRONMENT . '/constants.php';
197
198 24
        $filename = $config . 'constants.php';
199
200 24
        file_exists($constants) && $filename = $constants;
201
202 24
        defined('FILE_READ_MODE') || require $filename;
203 24
    }
204
205
    /**
206
     * Loads the CodeIgniter's core classes.
207
     *
208
     * @return void
209
     */
210 24
    protected function core()
211
    {
212 24
        load_class('Loader', 'core');
213
214 24
        load_class('Router', 'core');
215
216 24
        load_class('Input', 'core');
217
218 24
        load_class('Lang', 'core');
219
        
220 24
        load_class('Output', 'core');
221 24
    }
222
223
    /**
224
     * Sets up the current environment.
225
     *
226
     * @return void
227
     */
228 24
    protected function environment($value = 'development')
229
    {
230 24
        isset($this->server['CI_ENV']) && $value = $this->server['CI_ENV'];
231
232 24
        defined('ENVIRONMENT') || define('ENVIRONMENT', $value);
233 24
    }
234
235
    /**
236
     * Sets the ICONV constants.
237
     *
238
     * @param  boolean $enabled
239
     * @return void
240
     */
241 24
    protected function iconv($enabled = false)
242
    {
243 24
        mb_substitute_character('none') && $enabled = defined('ICONV_ENABLED');
244
245 24
        $enabled || define('ICONV_ENABLED', extension_loaded('iconv'));
246 24
    }
247
248
    /**
249
     * Sets up the APPPATH, VENDOR, and BASEPATH constants.
250
     *
251
     * @return void
252
     */
253 24
    protected function paths()
254
    {
255 24
        $paths = array('APPPATH' => $this->constants['APPPATH']);
256
257 24
        $paths['VENDOR'] = $this->constants['VENDOR'];
258
259 24
        $paths['VIEWPATH'] = $this->constants['VIEWPATH'];
260
261 24
        foreach ((array) $paths as $key => $value) {
262 24
            $defined = defined($key);
263
264 24
            $defined || define($key, $value);
265 8
        }
266
267 24
        defined('BASEPATH') || $this->basepath();
268 24
    }
269
}
270