-
Notifications
You must be signed in to change notification settings - Fork 8
/
App.php
260 lines (222 loc) · 7.81 KB
/
App.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<?php
/**
* This is the PPI Appliations Configuration class which is used in the Bootstrap
*/
/**
*
* @package Core
* @author Paul Dragoonis <dragoonis@php.net>
* @license http://opensource.org/licenses/mit-license.php MIT
* @link http://www.ppiframework.com
*/
class PPI_App {
/**
* The Environment Options for the PPI Application
*
* @var array
*/
protected $_envOptions = array(
'siteMode' => 'development', // This determines how PPI handles things like exceptions
'configBlock' => 'development', // The block in the config file to get the config data from
'configFile' => 'general.ini', // The default filename for the config file
'configCachePath' => '', // The path to the config cache
'cacheConfig' => false, // Config object caching
'errorLevel' => E_ALL, // The error level to throw via error_reporting()
'showErrors' => 'On', // Whether to display errors or not. This gets fired into ini_set('display_errors')
'router' => null,
'session' => null,
'config' => null,
'dispatcher' => null,
'request' => null
);
/**
* @param array $p_aParams
*/
function __construct($p_aParams = array()) {
if(!empty($p_aParams)) {
foreach ($p_aParams as $key => $value) {
if (method_exists($this, ($sMethod = 'set' . ucfirst($key)))) {
$this->$sMethod($value);
}
}
}
}
/**
* Setter for the environment, passing in options determining how the app will behave
*
* @param array $p_aOptions The options
* @return void
*/
function setEnv(array $p_aOptions) {
// If we pass in a bad sitemode, lets just default to 'development' gracefully.
if(isset($p_aOptions['siteMode'])) {
if(!in_array($p_aOptions['siteMode'], array('development', 'production'))) {
unset($p_aOptions['siteMode']);
}
}
// Any further options passed, eg: it maps; 'errorLevel' to $this->_errorLevel
foreach($p_aOptions as $optionName => $option) {
$this->_envOptions[$optionName] = $option;
}
}
/**
* Magic setter function, this is an alias of setEnv()
*
* @param string $option The Option
* @param string $value The Value
* @return void
*/
function __set($option, $value) {
$this->setEnv(array($option => $value));
}
/**
* Obtain the value of an environment option
*
* @param string $key The Environment Option
* @param mixed $default The default value to return if the key is not found
* @return mixed If your key is not found, then NULL is returned
*/
function getEnv($key, $default = null) {
return isset($this->_envOptions[$key]) ? $this->_envOptions[$key] : $default;
}
/**
* Magic getter function, this is an alias of getEnv()
*
* @param string $option The Option
* @return mixed
*/
function __get($option) {
return $this->getEnv($option);
}
/**
* Set the router object for the app bootup
*
* @param PPI_Router_Interface $p_oRouter The router object
* @return void
*/
function setRouter(PPI_Router_Interface $p_oRouter) {
$this->_envOptions['router'] = $p_oRouter;
}
/**
* Set the dispatch object for the app bootup
*
* @param PPI_Dispatch_Interface $p_oDispatch The dispatch object
* @return void
*/
function setDispatcher(PPI_Dispatch_Interface $p_oDispatch) {
$this->_envOptions['dispatcher'] = $p_oDispatch;
}
/**
* Set the request object for the app bootup
*
* @param object $p_oRequest
* @return void
*/
function setRequest($p_oRequest) {
$this->_envOptions['request'] = $p_oRequest;
}
/**
* Set the session object for the app bootup
*
* @param PPI_Session_Interface $p_oSession The session object
* @return void
*/
function setSession(PPI_Session_Interface $p_oSession) {
$this->_envOptions['session'] = $p_oSession;
}
/**
* Run the boot process, boot up our app. Call the relevant classes such as:
* config, registry, session, dispatch, router.
*
* @return $this Fluent interface
*/
function boot() {
error_reporting($this->_envOptions['errorLevel']);
ini_set('display_errors', $this->getEnv('showErrors', 'On'));
// Fire up the default config handler
if($this->_envOptions['config'] === null) {
$this->_config = new PPI_Config(array(
'configBlock' => $this->_envOptions['configBlock'],
'configFile' => $this->_envOptions['configFile'],
'cacheConfig' => $this->_envOptions['cacheConfig'],
'configCachePath' => $this->_envOptions['configCachePath']
));
}
$this->_config = $this->_config->getConfig();
// -- Set the config into the registry for quick read/write --
PPI_Registry::set('PPI_Config', $this->_config);
// ------------- Initialise the session -----------------
if(!headers_sent()) {
// Fire up the default session handler
if($this->_envOptions['session'] === null) {
$this->_envOptions['session'] = new PPI_Session();
}
PPI_Registry::set('PPI_Session', $this->_envOptions['session']);
}
// Fire up the default dispatcher
if($this->_envOptions['dispatcher'] === null) {
$this->_envOptions['dispatcher'] = new PPI_Dispatch_Standard(array(
'router' => $this->_envOptions['router']
));
}
$dispatch = new PPI_Dispatch($this->_envOptions['dispatcher']);
PPI_Registry::set('PPI_Dispatch', $dispatch);
// -- Set the PPI_Request object --
if($this->_envOptions['request'] === null) {
$this->_envOptions['request'] = new PPI_Request();
}
PPI_Registry::set('PPI_Request', $this->_envOptions['request']);
// -------------- Library Autoloading Process --------------
if(!empty($this->_config->system->autoloadLibs)) {
foreach(explode(',', $this->_config->system->autoloadLibs) as $sLib) {
switch(strtolower(trim($sLib))) {
case 'zf':
PPI_Autoload::add('Zend', array(
'path' => SYSTEMPATH . 'Vendor/Zend/',
'prefix' => 'Zend_'
));
break;
case 'github':
$githubAutoloader = SYSTEMPATH . 'Vendor/Github/Autoloader.php';
if(!file_exists($githubAutoloader)) {
throw new PPI_Exception('Unable to autoload github, the github autoloader was no found.');
}
include_once(SYSTEMPATH . 'Vendor/Github/Autoloader.php');
Github_Autoloader::register();
break;
// @todo - test this.
case 'swift':
include_once(SYSTEMPATH . 'Vendor/Swift/swift_required.php');
break;
case 'solar':
include_once(SYSTEMPATH . 'Vendor/Solar.php');
PPI_Autoload::add('Solar', array(
'path' => SYSTEMPATH . 'Vendor/Solar/',
'prefix' => 'Solar_'
));
break;
}
}
}
PPI_Registry::set('PPI_App', $this);
return $this; // Fluent Interface
}
/**
* Call the dispatch process. Running the dispatcher and dispatching
*
* @return $this Fluent interface
*/
function dispatch() {
$dispatch = PPI_Registry::get('PPI_Dispatch');
$dispatch->dispatch();
return $this;
}
/**
* Get the current site mode set, such as 'development' or 'production'
*
* @return string
*/
function getSiteMode() {
return $this->_envOptions['siteMode'];
}
}