This repository has been archived by the owner on Jun 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
BaseRouterExample.php
69 lines (60 loc) · 1.73 KB
/
BaseRouterExample.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
<?hh // strict
/*
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
/***********
* IF YOU EDIT THIS FILE also update the snippet in README.md
***********/
namespace Facebook\HackRouter\Examples\BaseRouterExample;
require_once(__DIR__.'/../vendor/hh_autoload.php');
use type Facebook\HackRouter\{BaseRouter, HttpMethod};
/** This can be whatever you want; in this case, it's a
* callable, but classname<MyWebControllerBase> is also a
* common choice.
*/
type TResponder = (function(dict<string, string>):string);
final class BaseRouterExample extends BaseRouter<TResponder> {
<<__Override>>
protected function getRoutes(
): ImmMap<HttpMethod, ImmMap<string, TResponder>> {
return ImmMap {
HttpMethod::GET => ImmMap {
'/' =>
($_params) ==> 'Hello, world',
'/user/{user_name}' =>
($params) ==> 'Hello, '.$params['user_name'],
},
HttpMethod::POST => ImmMap {
'/' => ($_params) ==> 'Hello, POST world',
},
};
}
}
function get_example_inputs(): ImmVector<(HttpMethod, string)> {
return ImmVector {
tuple(HttpMethod::GET, '/'),
tuple(HttpMethod::GET, '/user/foo'),
tuple(HttpMethod::GET, '/user/bar'),
tuple(HttpMethod::POST, '/'),
};
}
<<__EntryPoint>>
function main(): noreturn {
$router = new BaseRouterExample();
foreach (get_example_inputs() as $input) {
list($method, $path) = $input;
list($responder, $params) = $router->routeMethodAndPath($method, $path);
\printf(
"%s %s\n\t%s\n",
$method,
$path,
$responder(dict($params)),
);
}
exit(0);
}