-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathphoton.php
292 lines (254 loc) · 7.36 KB
/
photon.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<?php
/**
* Photon PHP MVC Framework 1.0
* Photon is a Lightweight PHP Framework 💡
*
* @author Clint.Network
* @link https://github.com/clintnetwork/photon
* @version 1.0
*/
class Photon
{
private $routes = [];
private $application_root;
private $base_route;
/**
* You need to have a '_layout.php' in the views folder when $use_layout_view is enabled
*/
public $use_layout_view = false;
/**
* If the development mode is enabled, you will visual helps
*/
public $development_mode = false;
/**
* To store anything
*/
public $baggy;
/**
* To store view data
*/
public static $viewbag;
/**
* To store view data
*/
public static $model;
public function __construct()
{
$this->base_route = dirname($_SERVER["SCRIPT_NAME"]);
$this->application_root = dirname($_SERVER["SCRIPT_FILENAME"]);
}
/**
* Initialize the framework
*/
public function ignite()
{
$controller_files = $this->load_folder($this->application_root . "/controllers");
$model_files = $this->load_folder($this->application_root . "/models");
foreach($controller_files as $controller)
{
Photon::inject_file($this->application_root . "/controllers/$controller");
$controller_name = pathinfo(strtolower($controller), PATHINFO_FILENAME);
$controller_class = pathinfo(ucfirst($controller), PATHINFO_FILENAME);
foreach(get_class_methods($controller_class) as $action_name)
{
$this->routes[$this->base_route . "/$controller_name/$action_name"] = array("Controller" => $controller_class, "Action" => $action_name);
if($action_name == "index")
{
$this->routes[$this->base_route . "/$controller_name"] = array("Controller" => $controller_class, "Action" => $action_name);
}
$rc = new ReflectionMethod($controller_class, $action_name);
if (preg_match_all('/@(\w+)\s+(.*)\r?\n/m', $rc->getDocComment(), $matches))
{
$result = array_combine($matches[1], $matches[2]);
if(isset($result["route"]))
{
$this->routes[$this->base_route . trim($result["route"])] = array("Controller" => $controller_class, "Action" => $action_name);
unset($this->routes[$this->base_route . "/$controller_name/$action_name"]);
}
if(isset($result["layout"]))
{
$this->routes[$this->base_route . trim($result["route"])]["Layout"] = trim($result["layout"]);
}
}
}
}
foreach($model_files as $model)
{
$this->inject_file($this->application_root . "/models/$model");
}
if($this->development_mode)
{
PhotonInject::css();
PhotonInject::debug();
}
$has_output = false;
foreach(array_keys($this->routes) as $route)
{
if($route == $_SERVER["REQUEST_URI"])
{
$controller_class = $this->routes[$route]["Controller"];
$action_name = $this->routes[$route]["Action"];
$layout_view = isset($this->routes[$route]["Layout"]) ? $this->routes[$route]["Layout"] : "_layout";
if($this->use_layout_view && $layout_view != "null")
{
// Inject the layout view
$this->baggy = array("Controller" => $controller_class, "Action" => $action_name);
Photon::inject_file($this->application_root . "/views/$layout_view.php");
}
else
{
// Execute actions from the controller
$tmp_class = new $controller_class();
$tmp_class->$action_name();
}
$has_output = true;
}
}
if(!$has_output)
{
if($this->development_mode)
{
PhotonInject::error(404);
}
else
{
header('HTTP/1.1 404 Not Found');
}
}
}
public static function render_body()
{
$baggy = Photon::get_caller(4)["object"]->baggy;
$controller_class = $baggy["Controller"];
$action_name = $baggy["Action"];
// Execute actions from the controller
$tmp_class = new $controller_class();
$tmp_class->$action_name();
}
public static function inject_file($file_location)
{
include $file_location;
}
public static function debug($what)
{
echo "<pre>";
print_r($what);
echo "</pre>";
}
public static function load_folder($folder)
{
return array_filter(scandir($folder), function($v, $k)
{
return StringManipulation::endsWith($v, ".php");
}, ARRAY_FILTER_USE_BOTH);
}
public static function get_caller($index = 1)
{
return debug_backtrace()[$index];
}
}
class PhotonInject
{
/**
* Inject the CSS
*/
public static function css()
{
echo <<<HTML
<style type="text/css">
.photon
{
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen-Sans, Ubuntu, Cantarell, 'Helvetica Neue', Helvetica, Arial, sans-serif; position: fixed; bottom: 0; right: 0; padding: 10px; color: #333333;
color: #333333;
}
.photon.error
{
position: fixed;
top: 0;
width: 100%;
height: 85%;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
}
.photon.error .code
{
font-size: 20rem;
}
.photon.error .message
{
font-size: 23pt;
color: #555555;
margin: 0;
}
.photon.powered
{
position: fixed;
bottom: 0;
right: 0;
padding: 10px;
}
.photon.powered .accent
{
color: #0e5a90;
}
</style>
HTML;
}
/**
* Add a powered message
*/
public static function debug()
{
echo <<<HTML
<div class="photon powered">
Photon Framework | <span class="accent">Development Mode</span>
</div>
HTML;
}
/**
* Create an error message
*/
public static function error($code = 500)
{
$message = "Internal Server Error";
if($code == 404)
{
$message = "This page or request does not exists.";
}
echo <<<HTML
<div class="photon error">
<span class="code">$code</span>
<p class="message">$message</p>
</div>
HTML;
}
}
class StringManipulation
{
public static function startsWith($haystack, $needle)
{
$length = strlen($needle);
return (substr($haystack, 0, $length) === $needle);
}
public static function endsWith($haystack, $needle)
{
$length = strlen($needle);
if ($length == 0) {
return true;
}
return (substr($haystack, -$length) === $needle);
}
}
function view($view_parameters = null)
{
$get_caller = Photon::get_caller(2);
Photon::$model = $view_parameters;
Photon::inject_file(dirname($_SERVER["SCRIPT_FILENAME"]) . "/views/" . strtolower($get_caller["class"]) . "/" . $get_caller["function"] . ".php");
}
function content($what)
{
echo $what;
}
?>