-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEngine.php
More file actions
249 lines (212 loc) · 9.2 KB
/
Engine.php
File metadata and controls
249 lines (212 loc) · 9.2 KB
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
<?php
/**
*****************************************************************************
** Copyright (c) 2007-2010 Jerome Poichet <jerome@frencaze.com>
**
** This software is supplied to you by Jerome Poichet in consideration of
** your agreement to the following terms, and your use, installation,
** modification or redistribution of this software constitutes acceptance of
** these terms. If you do not agree with these terms, please do not use,
** install, modify or redistribute this software.
**
** In consideration of your agreement to abide by the following terms, and
** subject to these terms, Jerome Poichet grants you a personal, non-exclusive
** license, to use, reproduce, modify and redistribute the software, with or
** without modifications, in source and/or binary forms; provided that if you
** redistribute the software in its entirety and without modifications, you
** must retain this notice and the following text and disclaimers in all such
** redistributions of the software, and that in all cases attribution of
** Jerome Poichet as the original author of the source code shall be included
** in all such resulting software products or distributions.
**
** Neither the name, trademarks, service marks or logos of Jerome Poichet may
** be used to endorse or promote products derived from the software without
** specific prior written permission from Jerome Poichet. Except as expressly
** stated in this notice, no other rights or licenses, express or implied, are
** granted by Jerome Poichet herein, including but not limited to any patent
** rights that may be infringed by your derivative works or by other works in
** which the software may be incorporated.
**
** The software is provided by Jerome Poichet on an "AS IS" basis.
** JEROME POICHET MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT
** LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND
** FITNESS FOR A PARTICULAR PURPOSE, REGARDING THE SOFTWARE OR ITS USE AND
** OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
**
** IN NO EVENT SHALL JEROME POICHET BE LIABLE FOR ANY SPECIAL, INDIRECT,
** INCIDENTAL OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
** OR BUSINESS INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
** MODIFICATION AND/OR DISTRIBUTION OF THE SOFTWARE, HOWEVER CAUSED AND
** WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT
** LIABILITY OR OTHERWISE, EVEN IF JEROME POICHET HAS BEEN ADVISED OF THE
** POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************
**/
require_once DOKIN_DIR.'Log.php';
class Engine
{
public static $aWatches = array();
static private $instance = null;
private function __construct()
{
}
public static function get_instance()
{
if (!self::$instance) {
self::$instance = new Engine();
}
return self::$instance;
}
public function run()
{
global $aAppConfig;
$iStartTime = microtime(true);
if (file_exists('app/lib/Helpers.php')) {
include_once('app/lib/Helpers.php');
}
// Retrieve the URL, remove the leading /
if (isset($_SERVER['GATEWAY_INTERFACE']) && isset($_GET['path'])) {
// FCGI MODE
$sURL = isset($_GET['path']) ? $_GET['path'] : '/';
$sURL = $sURL[0] == '/' ? substr($sURL, 1) : $sURL;
} else {
// MOD_PHP MODE
$sURL = $_SERVER['REDIRECT_URL'];
$sURL = substr($sURL, 1);
if ($sURL == 'dispatch.php') {
$sURL = $_SERVER['REQUEST_URI'];
$sURL = substr($sURL, 1);
// Remove query string
if (strstr($sURL,'?')) {
$sURL = substr($sURL, 0, strpos($sURL, '?'));
}
if (strstr($sURL, '&')) {
$sURL = substr($sURL, 0, strpos($sURL, '&'));
}
}
}
// Clean up the URL from the path where the application actually is
$sRoot = $_SERVER['DOCUMENT_ROOT'];
$sScript = $_SERVER['SCRIPT_FILENAME'];
$sIgnore = substr(str_replace($sRoot, '', $sScript), 1);
$sIgnore = dirname($sIgnore).'/';
$sURL = str_replace($sIgnore, '', $sURL);
// Ignore the extension
if (strpos($sURL,'.')) {
$sExt = substr($sURL, strrpos($sURL, '.') + 1);
$sURL = substr($sURL, 0, strrpos($sURL, '.'));
}
$sExt = isset($sExt) ? $sExt : 'html';
$sExt = strtolower($sExt);
// Explode the URL to figure out the controller and method being called
$aURLComponents = explode('/', $sURL);
// Underscores and dashes are replaced by spaces, then camel case
$sController = $aURLComponents[0] ? $aURLComponents[0] : 'Default';
$sController = str_replace(array('_', '-'), ' ', $sController);
$sController = ucwords($sController);
$sController = str_replace(' ', '', $sController);
// Underscores and dashes are replaced by spaces, periods are ignored
// numbers will be preceded with n
$sMethod = isset($aURLComponents[1]) ? $aURLComponents[1] : 'index';
$sMethod = str_replace(array('_', '-'), ' ', $sMethod);
$sMethod = ucwords($sMethod);
$sMethod = str_replace(' ', '', $sMethod);
$sMethod = str_replace('.', '', $sMethod);
$sMethod = strtolower(substr($sMethod, 0, 1)).substr($sMethod,1);
$sMethod = is_numeric($sMethod) ? 'n'.$sMethod : $sMethod;
// Determine controller class and path
$sControllerClass = $sController.'Controller';
$sControllerPath = 'app/controllers/'.$sControllerClass.'.php';
if (!file_exists($sControllerPath)) {
// Analyze routes
// TODO improve routing
if (isset($aAppConfig['MAPPING']) && is_array($aAppConfig['MAPPING'])) {
foreach ($aAppConfig['MAPPING'] as $sPattern => $sController) {
if (preg_match($sPattern, $sURL)) {
$bFound = true;
break;
}
}
}
$sController = ucwords($sController);
$sMethod = 'index';
$sControllerClass = $sController.'Controller';
$sControllerPath = 'app/controllers/'.$sControllerClass.'.php';
if (!file_exists($sControllerPath)) {
$this->notFound('controller '.$sController.' not found');
}
}
$GLOBALS['controller'] = $sController;
$GLOBALS['method'] = $sMethod;
include_once($sControllerPath);
if ($aAppConfig['TARGET'] == 'dev') {
_DEBUG('---> '.$sControllerClass.' '.$sMethod);
}
ob_start();
$oController = new $sControllerClass();
$__debug = ob_get_contents();
ob_end_clean();
if (!$oController->__early_exit) {
if (!method_exists($oController, $sMethod)) {
$this->notFound('method '.$sMethod.' not found');
}
$oController->set('controller', $sController);
$oController->set('method', $sMethod);
$oController->set('extension', $sExt);
if (method_exists($oController, 'preExec')) {
$oController->preExec();
}
$aArgs = array_slice($aURLComponents,2);
$oController->$sMethod($args);
}
if (method_exists($oController, 'postExec')) {
$oController->postExec();
}
// TEMPLATE
$sTemplate = $oController->getTemplate();
$sTemplate = $sTemplate ? $sTemplate : strtolower($sController).'.php';
$sTemplatePath = 'app/templates/'.$sTemplate;
if (!file_exists($sTemplatePath)) {
$this->notFound('template '.$sTemplate.' not found');
}
$GLOBALS['_content_template'] = $sTemplate;
$iEndTime = microtime(true);
$iTotalTime = $iEndTime - $iStartTime;
/*
$oController->set('time_elapsed', $iTotalTime);
$oController->set('__data', $oController->get());
$oController->set('queries', DB::get_query_count());
$oController->set('aQueries', DB::get_queries());
$oController->set('__debug', $__debug);
*/
// Rendering
if (method_exists($oController, 'preRender')) {
$oController->preRender();
}
ob_start();
$oController->_render($sTemplatePath);
$sContent = ob_get_contents();
ob_end_clean();
if (method_exists($oController, 'postRender')) {
$sContent = $oController->postRender($sContent);
}
print $sContent;
}
public function notFound($msg)
{
// XXX configure 404 page
header('HTTP/1.1 404 Not Found', true, 404);
header('Location: /404.html');
_ERROR($msg);
exit();
}
public static function watch_start($k)
{
self::$aWatches[$k] = microtime(true);
}
public static function watch_stop($k)
{
self::$aWatches[$k] = microtime(true) - self::$aWatches[$k];
}
}