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 |
|
require BASEPATH . 'core/Common.php'; |
84
|
|
|
|
85
|
24 |
|
load_class('Loader', 'core'); |
86
|
|
|
$CFG =& load_class('Config', 'core'); |
87
|
24 |
|
$UNI =& load_class('Utf8', 'core'); |
88
|
|
|
$SEC =& load_class('Security', 'core'); |
89
|
24 |
|
$RTR =& load_class('Router', 'core'); |
90
|
|
|
$IN =& load_class('Input', 'core'); |
91
|
24 |
|
$LANG =& load_class('Lang', 'core'); |
92
|
|
|
$OUT =& load_class('Output', 'core'); |
93
|
24 |
|
|
94
|
|
|
require BASEPATH . 'core/Controller.php'; |
95
|
|
|
|
96
|
|
|
if (file_exists(APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php')) |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
require_once APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php'; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$this->charset(); |
102
|
12 |
|
|
103
|
|
|
require 'helpers.php'; |
104
|
12 |
|
|
105
|
|
|
$class = ucfirst($RTR->class); |
106
|
12 |
|
$ctlPath = APPPATH.'controllers/'.$RTR->directory.$class.'.php'; |
107
|
|
|
|
108
|
12 |
|
if (file_exists($ctlPath)) { |
109
|
|
|
require_once($ctlPath); |
110
|
12 |
|
$CI = new $class(); |
111
|
|
|
} |
112
|
12 |
|
else { |
113
|
|
|
$CI = new \CI_Controller; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $CI; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
3 |
|
* Sets the constant with a value. |
121
|
|
|
* |
122
|
3 |
|
* @param string $key |
123
|
|
|
* @param string $value |
124
|
3 |
|
*/ |
125
|
|
|
public function set($key, $value) |
126
|
3 |
|
{ |
127
|
3 |
|
$this->constants[$key] = $value; |
128
|
|
|
|
129
|
3 |
|
$same = $key === 'APPPATH'; |
130
|
|
|
|
131
|
3 |
|
$path = $this->constants[$key] . '/views/'; |
132
|
|
|
|
133
|
3 |
|
$same && $this->constants['VIEWPATH'] = $path; |
134
|
1 |
|
|
135
|
3 |
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Sets the base path. |
140
|
|
|
* |
141
|
|
|
* @return void |
142
|
24 |
|
*/ |
143
|
|
|
protected function basepath() |
144
|
24 |
|
{ |
145
|
|
|
$directory = new \RecursiveDirectoryIterator(getcwd()); |
146
|
24 |
|
|
147
|
|
|
$iterator = new \RecursiveIteratorIterator($directory); |
148
|
24 |
|
|
149
|
|
|
foreach ($iterator as $item) { |
150
|
24 |
|
$core = 'core' . DIRECTORY_SEPARATOR . 'CodeIgniter.php'; |
151
|
|
|
|
152
|
24 |
|
$exists = strpos($item->getPathname(), $core) !== false; |
153
|
24 |
|
|
154
|
|
|
$path = str_replace($core, '', $item->getPathname()); |
155
|
|
|
|
156
|
|
|
$exists && ! defined('BASEPATH') && define('BASEPATH', $path); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
24 |
|
/** |
161
|
|
|
* Sets up important charset-related stuff. |
162
|
24 |
|
* |
163
|
|
|
* @return void |
164
|
24 |
|
*/ |
165
|
|
|
protected function charset() |
166
|
24 |
|
{ |
167
|
|
|
ini_set('default_charset', $charset = strtoupper(config_item('charset'))); |
168
|
24 |
|
|
169
|
24 |
|
defined('MB_ENABLED') || define('MB_ENABLED', extension_loaded('mbstring')); |
170
|
|
|
|
171
|
|
|
$encoding = 'mbstring.internal_encoding'; |
172
|
|
|
|
173
|
|
|
! is_php('5.6') && ! ini_get($encoding) && ini_set($encoding, $charset); |
174
|
|
|
|
175
|
|
|
$this->iconv(); |
176
|
24 |
|
} |
177
|
|
|
|
178
|
24 |
|
/** |
179
|
|
|
* Loads the framework constants. |
180
|
24 |
|
* |
181
|
|
|
* @return void |
182
|
24 |
|
*/ |
183
|
|
|
protected function constants() |
184
|
24 |
|
{ |
185
|
24 |
|
$config = APPPATH . 'config/'; |
|
|
|
|
186
|
|
|
|
187
|
|
|
$constants = $config . ENVIRONMENT . '/constants.php'; |
188
|
|
|
|
189
|
|
|
$filename = $config . 'constants.php'; |
190
|
|
|
|
191
|
|
|
file_exists($constants) && $filename = $constants; |
192
|
24 |
|
|
193
|
|
|
defined('FILE_READ_MODE') || require $filename; |
194
|
24 |
|
} |
195
|
|
|
|
196
|
24 |
|
/** |
197
|
|
|
* Sets up the current environment. |
198
|
24 |
|
* |
199
|
|
|
* @return void |
200
|
24 |
|
*/ |
201
|
|
|
protected function environment($value = 'development') |
202
|
24 |
|
{ |
203
|
24 |
|
isset($this->server['CI_ENV']) && $value = $this->server['CI_ENV']; |
204
|
|
|
|
205
|
|
|
defined('ENVIRONMENT') || define('ENVIRONMENT', $value); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Sets the ICONV constants. |
210
|
24 |
|
* |
211
|
|
|
* @param boolean $enabled |
212
|
24 |
|
* @return void |
213
|
|
|
*/ |
214
|
24 |
|
protected function iconv($enabled = false) |
215
|
|
|
{ |
216
|
24 |
|
mb_substitute_character('none') && $enabled = defined('ICONV_ENABLED'); |
217
|
|
|
|
218
|
24 |
|
$enabled || define('ICONV_ENABLED', extension_loaded('iconv')); |
219
|
|
|
} |
220
|
24 |
|
|
221
|
24 |
|
/** |
222
|
|
|
* Sets up the APPPATH, VENDOR, and BASEPATH constants. |
223
|
|
|
* |
224
|
|
|
* @return void |
225
|
|
|
*/ |
226
|
|
|
protected function paths() |
227
|
|
|
{ |
228
|
24 |
|
$paths = array('APPPATH' => $this->constants['APPPATH']); |
229
|
|
|
|
230
|
24 |
|
$paths['VENDOR'] = $this->constants['VENDOR']; |
231
|
|
|
|
232
|
24 |
|
$paths['VIEWPATH'] = $this->constants['VIEWPATH']; |
233
|
24 |
|
|
234
|
|
|
foreach ((array) $paths as $key => $value) { |
235
|
|
|
$defined = defined($key); |
236
|
|
|
|
237
|
|
|
$defined || define($key, $value); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
defined('BASEPATH') || $this->basepath(); |
241
|
24 |
|
} |
242
|
|
|
} |
243
|
|
|
|