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