Completed
Push — master ( 1ec5b5...3036e6 )
by Rougin
02:18
created

SparkPlug::getCodeIgniter()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 2

Importance

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