-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
166 lines (136 loc) · 5.68 KB
/
index.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
<?php
ob_start();
define("DEBUG", TRUE);
// 1. define the default path for includes
//define("APP_PATH", dirname(dirname(__FILE__)));
define("APP_PATH", str_replace(DIRECTORY_SEPARATOR, "/", dirname(__FILE__)));
define("URL", "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");
define("CDN", "http://localhost/LawFirm/public/assets/");
try {
// library's class autoloader
spl_autoload_register(function($class) {
$path = str_replace("\\", DIRECTORY_SEPARATOR, $class);
$file = APP_PATH . "/application/libraries/{$path}.php";
if (file_exists($file)) {
require_once $file;
return true;
}
});
// 2. load the Core class that includes an autoloader
require("framework/core.php");
Framework\Core::initialize();
// plugins
$path = APP_PATH . "/application/plugins";
$iterator = new DirectoryIterator($path);
foreach ($iterator as $item) {
if (!$item->isDot() && $item->isDir()) {
include($path . "/" . $item->getFilename() . "/initialize.php");
}
}
// 3. load and initialize the Configuration class
$configuration = new Framework\Configuration(array(
"type" => "ini"
));
Framework\Registry::set("configuration", $configuration->initialize());
// 4. load and initialize the Database class – does not connect
$database = new Framework\Database();
Framework\Registry::set("database", $database->initialize());
// 5. load and initialize the Cache class – does not connect
$cache = new Framework\Cache();
Framework\Registry::set("cache", $cache->initialize());
// 6. load and initialize the Session class
$session = new Framework\Session();
Framework\Registry::set("session", $session->initialize());
// 7. load the Router class and provide the url + extension
$router = new Framework\Router(array(
"url" => isset($_GET["url"]) ? $_GET["url"] : "home/index",
"extension" => !empty($_GET["extension"]) ? $_GET["extension"] : "html"
));
Framework\Registry::set("router", $router);
// include custom routes
include("public/routes.php");
// 8. dispatch the current request
$router->dispatch();
// 9. unset global variables
unset($configuration);
unset($database);
unset($cache);
unset($session);
unset($router);
} catch (Exception $e) {
// list exceptions
$exceptions = array(
"500" => array(
"Framework\Cache\Exception",
"Framework\Cache\Exception\Argument",
"Framework\Cache\Exception\Implementation",
"Framework\Cache\Exception\Service",
"Framework\Configuration\Exception",
"Framework\Configuration\Exception\Argument",
"Framework\Configuration\Exception\Implementation",
"Framework\Configuration\Exception\Syntax",
"Framework\Controller\Exception",
"Framework\Controller\Exception\Argument",
"Framework\Controller\Exception\Implementation",
"Framework\Core\Exception",
"Framework\Core\Exception\Argument",
"Framework\Core\Exception\Implementation",
"Framework\Core\Exception\Property",
"Framework\Core\Exception\ReadOnly",
"Framework\Core\Exception\WriteOnly",
"Framework\Database\Exception",
"Framework\Database\Exception\Argument",
"Framework\Database\Exception\Implementation",
"Framework\Database\Exception\Service",
"Framework\Database\Exception\Sql",
"Framework\Model\Exception",
"Framework\Model\Exception\Argument",
"Framework\Model\Exception\Connector",
"Framework\Model\Exception\Implementation",
"Framework\Model\Exception\Primary",
"Framework\Model\Exception\Type",
"Framework\Model\Exception\Validation",
"Framework\Request\Exception",
"Framework\Request\Exception\Argument",
"Framework\Request\Exception\Implementation",
"Framework\Request\Exception\Response",
"Framework\Router\Exception",
"Framework\Router\Exception\Argument",
"Framework\Router\Exception\Implementation",
"Framework\Session\Exception",
"Framework\Session\Exception\Argument",
"Framework\Session\Exception\Implementation",
"Framework\Template\Exception",
"Framework\Template\Exception\Argument",
"Framework\Template\Exception\Implementation",
"Framework\Template\Exception\Parser",
"Framework\View\Exception",
"Framework\View\Exception\Argument",
"Framework\View\Exception\Data",
"Framework\View\Exception\Implementation",
"Framework\View\Exception\Renderer",
"Framework\View\Exception\Syntax"
),
"404" => array(
"Framework\Router\Exception\Action",
"Framework\Router\Exception\Controller"
)
);
$exception = get_class($e);
// attempt to find the approapriate template, and render
foreach ($exceptions as $template => $classes) {
foreach ($classes as $class) {
if ($class == $exception) {
header("Content-type: text/html");
include(APP_PATH . "/application/views/errors/{$template}.php");
exit;
}
}
}
// log or email any error
// render fallback template
header("Content-type: text/html");
echo "An error occurred.". $e;
exit;
}
?>