/
index.php
439 lines (377 loc) · 10.8 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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
<?php
use Microsite\App;
use Microsite\Regex;
use Microsite\Response;
use Microsite\Request;
use Microsite\DB\PDO\DB;
use Microsite\Handler;
use Microsite\Template;
use Microsite\Tinycode;
use Microsite\Renderers\JSONRenderer;
use Microsite\Renderers\MarkdownRenderer;
use Microsite\DB\Mongo\DB as MongoDB;
//include 'src/microsite.phar';
include 'src/stub2.php';
$app = new App();
// Assign a directory in which templates can be found for rendering
$app->template_dirs = [
__DIR__ . '/views',
];
/**
* Basic home page.
* Set the view to a home.php view provided in the view directory
*/
$app->route('home', '/', function(App $app) {
return $app->response()->render('home.php');
});
/**
* Simple string output.
*/
$app->route('string', '/string', function() {
return "A response can be a simple string from inside a function";
});
/**
* Weird routing issue
*/
$app->route('inbound', '/inbound', function() { echo 'ok?'; });
/**
* Simple echo output.
*/
$app->route('echo', '/echo', function() {
echo "A response can even be echoed from inside a function";
});
/**
* Route with a parameter
*/
$app->route('hello', '/hello/:name', function(Request $request) {
echo "Hello {$request['name']}!";
});
/**
* Route with a validated parameter
*/
$app->route('count', '/count/:number', function(Request $request) {
echo "This is a number: {$request['number']}";
})->validate_fields(['number' => '[0-9]+']);
/**
* Route with a validated parameter function, only /valid/ok correctly routes here
*/
$app->route('valid', '/valid/:valid', function(Request $request) {
echo "This is a valid route: {$request['valid']}";
})->validate_fields(['valid' => function($value) {return ($value == 'ok');}]);
/**
* Convert an incoming url parameter into a useful value
*/
$app
->route(
'author',
'/author/:user',
function(Request $request, Response $response) {
$response['output'] = '<pre>' . print_r($request['user'], 1) . '</pre>';
return $response->render('debug.php');
}
)
->validate_fields([':user' => '\d+'])
->convert('user', function($user_id){
$user = new stdClass(); // Simulate getting a database record.
$user->id = $user_id;
$user->name = 'Test User #' . $user_id;
return $user;
});
/**
* Two handlers
*/
$app->route(
'evenodd',
'/evenodd/:number',
function(Request $request) {
if($request['number'] % 2 == 0) {
echo "This is an even number";
}
},
function(Request $request) {
if($request['number'] % 2 == 1) {
echo "This is an odd number";
}
}
)->validate_fields([':number' => '[0-9]+']);
/**
* Use the route system to produce the url to the named route "hello"
*/
$app->route('interior', '/interior', function(Response $response, $app) {
$response['output'] = $app->get_route('hello')->build(['name' => 'User']);
return $response->render('debug.php');
});
/**
* A simple custom Handler class
*/
class MyHandler extends Handler {
public $prerequisite;
public function handler_one() {
echo 'This is a custom handler function.';
}
public function prerequisite() {
$this->prerequisite = 'set';
}
public function handler_two() {
echo 'The prerequisite value is: ' . $this->prerequisite;
}
}
/**
* Have this route respond with a method from the custom handler
*/
$app->route('dohandler', '/dohandler1', Handler::handle('MyHandler', 'handler_one'));
/**
* Demonstrate the shared handler instance between handler methods
* This allows a handler class object to maintain a state between handler method execution
*/
$app->route(
'prerequisite',
'/prerequisite',
Handler::handle('MyHandler', 'prerequisite'),
Handler::handle('MyHandler', 'handler_two')
);
/**
* GET method only
* Demonstrates two routes with the same URL, on different HTTP methods
*/
$app->route('form', '/form', function() {
echo <<< FORM_HTML
<form action="" method="POST">
<label>Name: <input type="text" name="name" /></label>
<input type="submit" value="Submit">
</form>
FORM_HTML;
})->get();
/**
* POST method only
* Demonstrates two routes with the same URL, on different HTTP methods
*/
$app->route(
'form_post',
'/form',
function(Response $response) {
if(trim($_POST['name']) == '') {
$response->redirect('/form');
}
},
function() {
echo 'The entered name is: ' . $_POST['name'];
}
)->post();
/**
* Response for either the GET or POST method
*/
$app->route('getorpost', '/method', function() {echo 'Worked.';})->via('GET,POST');
/**
* Use a regular expression to test the route URL
* Pass the name back into the response for output.
* Use the internal debug.php view again for output.
*/
$app->route('hiya', new Regex('#/hiya/(?P<name>.+)/?$#'), function(Response $response, Request $request) {
$response['output'] = "Hiya {$request['name']}";
return $response->render('debug.php');
});
/**
* Only accept even arguments in the URL
*/
$app->route('even', new Regex('#^/number/(?P<number>[0-9]+)/?$#'), function(Request $request) {
echo "The number {$request['number']} was even.";
})->validate(function($request) { return $request['number'] % 2 == 0;});
/**
* Only accept odd arguments in the URL
*/
$app->route('odd', new Regex('#^/number/(?P<number>[0-9]+)/?$#'), function(Request $request) {
echo "The number {$request['number']} was odd.";
})->validate(function($request) { return $request['number'] % 2 == 1;});
class AdminApp extends App {
public function add_response(Response $response) {
$response['added'] = 'true';
}
}
/**
* Create a new app to layer into the /admin URL space.
*/
$admin = new AdminApp();
/**
* Within the admin app, create a /plugins URL
* Output a message using the internal debug.php template
*/
$admin->route('plugins', '/plugins', function() {
echo "This is the Plugins page";
});
/**
* Display the value of "added" as added in the app middleware
* Note the use of a string as the middleware method name, and that it resolves to the app method
*/
$admin->route('add_response', '/add_response', 'add_response', function(Response $response){
echo $response['added'];
});
/**
* Build an index for the admin route
*/
$admin->route('admin_index', '/', function(){
echo "This is the admin index";
});
/**
* Add the admin app as a handler within the /admin route on the main app
*/
$app->route('admin', '/admin', $admin);
/**
* Register an on-demand object with the app
*/
$app->demand('mockdb', function($param) {
$obj = new stdClass();
$obj->foo = 'bar';
$obj->baz = range($param, $param + 15, 3);
$obj->microtime = microtime(true);
$obj->param = $param;
return $obj;
});
/**
* Return the view data as a json object
* Fetch the registsered on-demand "mockdb" object from the app
* Note that the mockdb objects are the different because it was registered with ->demand()
* If it was registered with ->share() it would be created only once
*/
$app->route('json', '/json', function(App $app) {
$response = $app->response();
$response['user'] = 'Owen';
$response['user_id'] = 1;
$response['registered'] = true;
$response['mockdb_obj'] = $app->mockdb(1);
$response['mockdb_obj2'] = $app->mockdb(16);
$response->set_renderer(JSONRenderer::create('', $app));
return $response->render();
});
/**
* Create a basic test page for different accept header requests
*/
$app->route('accept', '/accept', function(Response $response) {
$response['message'] = 'The result of the demo appears here. This is from HTML.';
return $response->render('accept.php');
});
$app->route('accecpt_json', '/accept', function(Response $response, App $app) {
$response->set_renderer(JSONRenderer::create('', $app));
$response['message'] = 'The result of the demo appears here. This is from JSON.';
return $response;
})->type('application/json');
/**
* Render some markdown
*/
$app->route('markdown', '/markdown', function(Response $response, App $app) {
$response->set_renderer(MarkdownRenderer::create($app->template_dirs(), $app));
return $response->render('markdown.md');
});
/**
* Register an on-demand object with the app for a real database
*/
$app->share('db', function() {
$db = new DB('sqlite:' . __DIR__ . '/db.db');
return $db;
});
$app->route('database', '/database', function(App $app) {
$response = $app->response();
$samples = $app->db()->results('SELECT * FROM sample ORDER BY age ASC;');
$response['output'] = $response->partial('table.php', array('results' => $samples));
$response['count'] = $app->db()->val('SELECT count(*) as ct FROM sample');
$response['sample'] = $samples[0]['name'];
return $response->render('debug.php');
});
/**
* On-demand mongo
*/
$app->share('mongo', function() {
return new MongoDB('samplez');
});
$app->route('mongo', '/mongo', function(App $app) {
$response = $app->response();
/** @var \Microsite\DB\Mongo\DB $mongo */
$mongo = $app->mongo();
$samples = iterator_to_array($mongo->find('test'));
$response['output'] = $response->partial('table.php', array('results' => $samples));
return $response->render('debug.php');
});
/**
* Display a phpinfo
*/
$app->route('phpinfo', '/phpinfo', function(){ phpinfo(); });
/**
* Show a sequence of Tinycodes
*/
$app->route('tinycode', '/tiny', function(){
Tinycode::init();
header('content-type: text/plain');
echo 'Total codes: ' . Tinycode::max_int() . "\n";
for($z = 1; $z <=300; $z++) {
$s = Tinycode::to_code($z);
$n = Tinycode::to_int($s);
echo $z . ' encoded => ' . $s . ' decoded => ' . $n . "\n";
}
});
$app->route('first', '/one/two', function() {
echo 'one/two';
});
$app->route('second', '/one/two/three', function() {
echo 'one/two/three';
});
$app->route('first2', '/one/:two', function() {
echo 'one/:two';
});
$app->route('second2', '/one/:two/three', function() {
echo 'one/:two/three';
});
$app->route('template', '/template', function(Response $response) {
Template::register('tpl');
$response['name'] = '<b>Owen Winkler</b>';
$user = new stdClass();
$user->city = 'Chester Springs';
$user->age = 39;
$user->gender = 'male';
$response['user'] = $user;
$response['rows'] = [['cell' => 4],['cell' => '<b>3</b>'],['cell' => 12],['cell' => 8],];
$response['values'] = [1,3,5,8,15];
return $response->render('template.tpl');
});
class UserHandler extends \Microsite\Handler {
public function load(App $app) {
parent::load($app);
$app->route('do_routed', '/routed/:number', [$this, 'do_routed'])
->validate_fields([':number' => '[0-9]+']);
}
/**
* @url /
*/
public function do_index(App $app) {
echo 'do index';
var_dump($app);
}
/**
* @url /get
* @method GET
*/
public function do_get() {
echo 'do get';
}
/**
* @url /post
* @method POST
*/
public function do_post() {
echo 'do post';
}
/**
* @param Request $request
* @url /value/:value
*/
public function do_value(Request $request) {
echo $request['value'];
}
public function do_routed(Request $request) {
echo 'routed. The number was: ' . $request['number'];
}
}
$app->route('user', '/user', \Microsite\Handler::mount('UserHandler'));
/**
* Run the app to match and dispatch routes
*/
$app();