1 | <?php |
||
2 | |||
3 | namespace Rougin\SparkPlug; |
||
4 | |||
5 | /** |
||
6 | * @package SparkPlug |
||
7 | * |
||
8 | * @author Rougin Gutib <[email protected]> |
||
9 | */ |
||
10 | class SparkPlug |
||
11 | { |
||
12 | /** |
||
13 | * @var array<string, string> |
||
14 | */ |
||
15 | protected $consts = array(); |
||
16 | |||
17 | /** |
||
18 | * @var array<string, string> |
||
19 | */ |
||
20 | protected $globals = array(); |
||
21 | |||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $path = ''; |
||
26 | |||
27 | /** |
||
28 | * @var array<string, string> |
||
29 | */ |
||
30 | protected $server = array(); |
||
31 | |||
32 | /** |
||
33 | * @param array<string, string> $globals |
||
34 | * @param array<string, string> $server |
||
35 | * @param string|null $path |
||
36 | */ |
||
37 | public function __construct(array $globals, array $server, $path = null) |
||
38 | { |
||
39 | $this->globals = & $globals; |
||
40 | 24 | ||
41 | $this->path = (string) getcwd(); |
||
42 | 24 | ||
43 | if ($path) |
||
44 | 24 | { |
|
45 | $this->path = $path; |
||
46 | 24 | } |
|
47 | |||
48 | 24 | $this->server = $server; |
|
49 | |||
50 | 24 | $path = $this->path . '/application/'; |
|
51 | |||
52 | 24 | $this->consts['APPPATH'] = $path; |
|
53 | |||
54 | 24 | $this->consts['ENVIRONMENT'] = 'development'; |
|
55 | |||
56 | 24 | $this->consts['VENDOR'] = $this->path . '/vendor/'; |
|
57 | 24 | ||
58 | $this->consts['VIEWPATH'] = $path . 'views/'; |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * @deprecated since ~0.6, use "instance" instead. |
||
63 | * |
||
64 | * Returns the Codeigniter singleton. |
||
65 | 12 | * |
|
66 | * @return \Rougin\SparkPlug\Controller |
||
67 | 12 | */ |
|
68 | public function getCodeIgniter() |
||
69 | { |
||
70 | return $this->instance(); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Returns the Codeigniter singleton. |
||
75 | 24 | * |
|
76 | * @return \Rougin\SparkPlug\Controller |
||
77 | 24 | */ |
|
78 | public function instance() |
||
79 | 24 | { |
|
80 | $this->setPaths(); |
||
81 | 24 | ||
82 | $this->environment($this->consts['ENVIRONMENT']); |
||
83 | 24 | ||
84 | $this->constants(); |
||
85 | 24 | ||
86 | $this->common(); |
||
87 | 24 | ||
88 | $this->config(); |
||
89 | 24 | ||
90 | require 'helpers.php'; |
||
91 | 24 | ||
92 | /** @var \Rougin\SparkPlug\Controller|null */ |
||
93 | 24 | $instance = Controller::get_instance(); |
|
94 | |||
95 | if (empty($instance)) |
||
96 | { |
||
97 | $instance = new Controller; |
||
98 | } |
||
99 | |||
100 | return $instance; |
||
101 | } |
||
102 | 12 | ||
103 | /** |
||
104 | 12 | * Sets the constant with a value. |
|
105 | * |
||
106 | 12 | * @param string $key |
|
107 | * @param string $value |
||
108 | 12 | * |
|
109 | * @return self |
||
110 | 12 | */ |
|
111 | public function set($key, $value) |
||
112 | 12 | { |
|
113 | $this->consts[$key] = $value; |
||
114 | |||
115 | $path = $this->consts[$key] . '/views/'; |
||
116 | |||
117 | if ($key === 'APPPATH') |
||
118 | { |
||
119 | $this->consts['VIEWPATH'] = $path; |
||
120 | 3 | } |
|
121 | |||
122 | 3 | return $this; |
|
123 | } |
||
124 | 3 | ||
125 | /** |
||
126 | 3 | * Sets the base path. |
|
127 | 3 | * |
|
128 | * @return void |
||
129 | 3 | */ |
|
130 | protected function basepath() |
||
131 | 3 | { |
|
132 | $path = (string) getcwd(); |
||
133 | 3 | ||
134 | 1 | $path = new \RecursiveDirectoryIterator($path); |
|
135 | 3 | ||
136 | /** @var \SplFileInfo[] */ |
||
137 | $items = new \RecursiveIteratorIterator($path); |
||
138 | |||
139 | $slash = DIRECTORY_SEPARATOR; |
||
140 | |||
141 | foreach ($items as $item) |
||
142 | 24 | { |
|
143 | $core = 'core' . $slash . 'CodeIgniter.php'; |
||
144 | 24 | ||
145 | $path = $item->getPathname(); |
||
146 | 24 | ||
147 | $exists = strpos($path, $core) !== false; |
||
148 | 24 | ||
149 | $path = str_replace($core, '', $path); |
||
150 | 24 | ||
151 | if ($exists && ! defined('BASEPATH')) |
||
152 | 24 | { |
|
153 | 24 | define('BASEPATH', (string) $path); |
|
154 | } |
||
155 | } |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Sets up important charset-related stuff. |
||
160 | 24 | * |
|
161 | * @return void |
||
162 | 24 | */ |
|
163 | protected function charset() |
||
164 | 24 | { |
|
165 | /** @var string */ |
||
166 | 24 | $charset = config_item('charset'); |
|
167 | |||
168 | 24 | $charset = strtoupper($charset); |
|
169 | 24 | ||
170 | ini_set('default_charset', $charset); |
||
171 | |||
172 | if (! defined('MB_ENABLED')) |
||
173 | { |
||
174 | define('MB_ENABLED', extension_loaded('mbstring')); |
||
175 | } |
||
176 | 24 | ||
177 | $encoding = 'mbstring.internal_encoding'; |
||
178 | 24 | ||
179 | if (! is_php('5.6') && ! ini_get($encoding)) |
||
180 | 24 | { |
|
181 | ini_set($encoding, $charset); |
||
182 | 24 | } |
|
183 | |||
184 | 24 | $this->iconv(); |
|
185 | 24 | } |
|
186 | |||
187 | /** |
||
188 | * Loads the Common and the Base Controller class. |
||
189 | * |
||
190 | * @return void |
||
191 | */ |
||
192 | 24 | protected function common() |
|
193 | { |
||
194 | 24 | require BASEPATH . 'core/Common.php'; |
|
195 | |||
196 | 24 | if (! class_exists('CI_Controller')) |
|
197 | { |
||
198 | 24 | require BASEPATH . 'core/Controller.php'; |
|
199 | } |
||
200 | 24 | ||
201 | $this->charset(); |
||
202 | 24 | } |
|
203 | 24 | ||
204 | /** |
||
205 | * Sets global configurations. |
||
206 | * |
||
207 | * @return void |
||
208 | */ |
||
209 | protected function config() |
||
210 | 24 | { |
|
211 | /** @var string */ |
||
212 | 24 | $config = load_class('Config', 'core'); |
|
213 | |||
214 | 24 | $this->globals['CFG'] = & $config; |
|
215 | |||
216 | 24 | /** @var string */ |
|
217 | $utf8 = load_class('Utf8', 'core'); |
||
218 | 24 | ||
219 | $this->globals['UNI'] = & $utf8; |
||
220 | 24 | ||
221 | 24 | /** @var string */ |
|
222 | $security = load_class('Security', 'core'); |
||
223 | |||
224 | $this->globals['SEC'] = & $security; |
||
225 | |||
226 | $this->core(); |
||
227 | } |
||
228 | 24 | ||
229 | /** |
||
230 | 24 | * Loads the framework constants. |
|
231 | * |
||
232 | 24 | * @return void |
|
233 | 24 | */ |
|
234 | protected function constants() |
||
235 | { |
||
236 | $config = APPPATH . 'config/'; |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
237 | |||
238 | $consts = $config . ENVIRONMENT . '/constants.php'; |
||
239 | |||
240 | $filename = $config . 'constants.php'; |
||
241 | 24 | ||
242 | if (file_exists($consts)) |
||
243 | 24 | { |
|
244 | $filename = $consts; |
||
245 | 24 | } |
|
246 | 24 | ||
247 | if (! defined('FILE_READ_MODE')) |
||
248 | { |
||
249 | require $filename; |
||
250 | } |
||
251 | } |
||
252 | |||
253 | 24 | /** |
|
254 | * Loads the CodeIgniter's core classes. |
||
255 | 24 | * |
|
256 | * @return void |
||
257 | 24 | */ |
|
258 | protected function core() |
||
259 | 24 | { |
|
260 | load_class('Loader', 'core'); |
||
261 | 24 | ||
262 | 24 | load_class('Router', 'core'); |
|
263 | |||
264 | 24 | load_class('Input', 'core'); |
|
265 | 8 | ||
266 | load_class('Lang', 'core'); |
||
267 | 24 | ||
268 | 24 | load_class('Output', 'core'); |
|
269 | } |
||
270 | |||
271 | /** |
||
272 | * Sets up the current environment. |
||
273 | * |
||
274 | * @param string $value |
||
275 | * |
||
276 | * @return void |
||
277 | */ |
||
278 | protected function environment($value = 'development') |
||
279 | { |
||
280 | if (isset($this->server['CI_ENV'])) |
||
281 | { |
||
282 | $value = $this->server['CI_ENV']; |
||
283 | } |
||
284 | |||
285 | if (! defined('ENVIRONMENT')) |
||
286 | { |
||
287 | define('ENVIRONMENT', $value); |
||
288 | } |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * Sets the ICONV constants. |
||
293 | * |
||
294 | * @param boolean $enabled |
||
295 | * |
||
296 | * @return void |
||
297 | */ |
||
298 | protected function iconv($enabled = false) |
||
299 | { |
||
300 | if (mb_substitute_character('none') === true) |
||
301 | { |
||
302 | $enabled = defined('ICONV_ENABLED'); |
||
303 | } |
||
304 | |||
305 | if (! $enabled) |
||
306 | { |
||
307 | define('ICONV_ENABLED', extension_loaded('iconv')); |
||
308 | } |
||
309 | } |
||
310 | |||
311 | /** |
||
312 | * Sets up the APPPATH, VENDOR, and BASEPATH constants. |
||
313 | * |
||
314 | * @return void |
||
315 | */ |
||
316 | protected function setPaths() |
||
317 | { |
||
318 | $paths = array('APPPATH' => $this->consts['APPPATH']); |
||
319 | |||
320 | $paths['VENDOR'] = $this->consts['VENDOR']; |
||
321 | |||
322 | $paths['VIEWPATH'] = $this->consts['VIEWPATH']; |
||
323 | |||
324 | foreach ($paths as $key => $value) |
||
325 | { |
||
326 | if (! defined($key)) |
||
327 | { |
||
328 | define($key, $value); |
||
329 | } |
||
330 | } |
||
331 | |||
332 | if (! defined('BASEPATH')) |
||
333 | { |
||
334 | $this->basepath(); |
||
335 | } |
||
336 | } |
||
337 | } |
||
338 |