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
|
|
|
|
46
|
12 |
|
$this->setPaths(); |
47
|
12 |
|
$this->setEnvironment(); |
48
|
12 |
|
$this->loadConstants(); |
49
|
12 |
|
$this->loadClasses(); |
50
|
12 |
|
$this->setCharSet(); |
51
|
12 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Returns a CodeIgniter instance. |
55
|
|
|
* |
56
|
|
|
* @return CodeIgniter |
57
|
|
|
*/ |
58
|
12 |
|
public function getCodeIgniter() |
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 |
|
return new CI_Controller; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Loads the Common and the Base Controller class. |
79
|
|
|
* |
80
|
|
|
* @return void |
81
|
|
|
*/ |
82
|
12 |
|
protected function loadClasses() |
83
|
|
|
{ |
84
|
12 |
|
require BASEPATH . 'core/Common.php'; |
85
|
|
|
|
86
|
12 |
|
if ( ! class_exists('CI_Controller')) { |
87
|
3 |
|
require BASEPATH . 'core/Controller.php'; |
88
|
3 |
|
} |
89
|
12 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Loads the framework constants. |
93
|
|
|
* |
94
|
|
|
* @return void |
95
|
|
|
*/ |
96
|
12 |
|
protected function loadConstants() |
97
|
|
|
{ |
98
|
12 |
|
if (defined('FILE_READ_MODE')) { |
99
|
9 |
|
return; |
100
|
|
|
} |
101
|
|
|
|
102
|
3 |
|
$envConstants = APPPATH . 'config/' . ENVIRONMENT . '/constants.php'; |
103
|
3 |
|
$constants = APPPATH . 'config/constants.php'; |
104
|
|
|
|
105
|
3 |
|
if (file_exists($envConstants)) { |
106
|
3 |
|
$constants = $envConstants; |
107
|
3 |
|
} |
108
|
|
|
|
109
|
3 |
|
require $constants; |
110
|
3 |
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Sets up important charset-related stuff. |
114
|
|
|
* |
115
|
|
|
* @return void |
116
|
|
|
*/ |
117
|
12 |
|
protected function setCharSet() |
118
|
|
|
{ |
119
|
12 |
|
$charset = strtoupper(config_item('charset')); |
120
|
|
|
|
121
|
12 |
|
ini_set('default_charset', $charset); |
122
|
|
|
|
123
|
|
|
// mbstring.internal_encoding is deprecated starting with PHP 5.6 |
124
|
|
|
// and it's usage triggers E_DEPRECATED messages. |
125
|
12 |
|
if ( ! is_php('5.6') && ! ini_get('mbstring.internal_encoding')) { |
126
|
2 |
|
ini_set('mbstring.internal_encoding', $charset); |
127
|
2 |
|
} |
128
|
|
|
|
129
|
|
|
// This is required for mb_convert_encoding() to strip invalid |
130
|
|
|
// characters. That's utilized by CI_Utf8, but it's also done for |
131
|
|
|
// consistency with iconv. |
132
|
12 |
|
mb_substitute_character('none'); |
133
|
|
|
|
134
|
|
|
// There's an ICONV_IMPL constant, but the PHP manual says that using |
135
|
|
|
// iconv's predefined constants is "strongly discouraged". |
136
|
12 |
|
if ( ! defined('ICONV_ENABLED')) { |
137
|
3 |
|
define('ICONV_ENABLED', extension_loaded('iconv')); |
138
|
3 |
|
} |
139
|
12 |
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Sets up the current environment. |
143
|
|
|
* |
144
|
|
|
* @return void |
145
|
|
|
*/ |
146
|
12 |
|
protected function setEnvironment() |
147
|
|
|
{ |
148
|
12 |
|
$environment = 'development'; |
149
|
|
|
|
150
|
12 |
|
if (isset($this->server['CI_ENV'])) { |
151
|
12 |
|
$environment = $this->server['CI_ENV']; |
152
|
12 |
|
} |
153
|
|
|
|
154
|
12 |
|
if ( ! defined('ENVIRONMENT')) { |
155
|
3 |
|
define('ENVIRONMENT', $environment); |
156
|
3 |
|
} |
157
|
12 |
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Sets up the APPPATH, VENDOR, and BASEPATH constants. |
161
|
|
|
* |
162
|
|
|
* @return void |
163
|
|
|
*/ |
164
|
12 |
|
protected function setPaths() |
165
|
|
|
{ |
166
|
12 |
|
$applicationPath = realpath('application'); |
167
|
12 |
|
$vendorPath = realpath('vendor'); |
168
|
|
|
|
169
|
12 |
|
if ($this->path) { |
170
|
12 |
|
$vendorPath = $this->path . '/vendor'; |
171
|
12 |
|
$applicationPath = $this->path . '/application'; |
172
|
12 |
|
} |
173
|
|
|
|
174
|
12 |
|
if ( ! defined('APPPATH')) { |
175
|
3 |
|
define('APPPATH', $applicationPath . '/'); |
176
|
3 |
|
} |
177
|
|
|
|
178
|
12 |
|
if ( ! defined('VENDOR')) { |
179
|
3 |
|
define('VENDOR', $vendorPath . '/'); |
180
|
3 |
|
} |
181
|
|
|
|
182
|
12 |
|
if ( ! defined('VIEWPATH')) { |
183
|
3 |
|
define('VIEWPATH', APPPATH . '/views/'); |
184
|
3 |
|
} |
185
|
|
|
|
186
|
12 |
|
if (defined('BASEPATH')) { |
187
|
9 |
|
return; |
188
|
|
|
} |
189
|
|
|
|
190
|
3 |
|
$iterator = new RecursiveIteratorIterator( |
191
|
3 |
|
new RecursiveDirectoryIterator(getcwd()) |
192
|
3 |
|
); |
193
|
|
|
|
194
|
3 |
|
foreach ($iterator as $file) { |
195
|
3 |
|
$core = 'core' . DIRECTORY_SEPARATOR . 'CodeIgniter.php'; |
196
|
|
|
|
197
|
3 |
|
if (strpos($file->getPathname(), $core) !== FALSE) { |
198
|
3 |
|
$path = str_replace($core, '', $file->getPathname()); |
199
|
|
|
|
200
|
3 |
|
define('BASEPATH', $path); |
201
|
|
|
|
202
|
3 |
|
break; |
203
|
|
|
} |
204
|
3 |
|
} |
205
|
3 |
|
} |
206
|
|
|
} |
207
|
|
|
|