forked from hhvm/hack-router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UriPatternsExample.php
127 lines (109 loc) · 3.08 KB
/
UriPatternsExample.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
<?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\UrlPatternsExample;
require_once(__DIR__.'/../vendor/hh_autoload.php');
use type Facebook\HackRouter\{
BaseRouter,
GetFastRoutePatternFromUriPattern,
GetUriBuilderFromUriPattern,
HasUriPattern,
HttpMethod,
RequestParameters,
UriPattern
};
<<__ConsistentConstruct>>
abstract class WebController implements HasUriPattern {
use GetFastRoutePatternFromUriPattern;
use GetUriBuilderFromUriPattern;
abstract public function getResponse(): string;
private RequestParameters $uriParameters;
final protected function getRequestParameters(): RequestParameters {
return $this->uriParameters;
}
public function __construct(
ImmMap<string, string> $uri_parameter_values,
) {
$this->uriParameters = new RequestParameters(
static::getUriPattern()->getParameters(),
ImmVector { },
$uri_parameter_values,
);
}
}
final class HomePageController extends WebController {
<<__Override>>
public static function getUriPattern(): UriPattern {
return (new UriPattern())->literal('/');
}
<<__Override>>
public function getResponse(): string {
return 'Hello, world';
}
}
final class UserPageController extends WebController {
<<__Override>>
public static function getUriPattern(): UriPattern {
return (new UriPattern())
->literal('/users/')
->string('user_name');
}
<<__Override>>
public function getResponse(): string {
return 'Hello, '.$this->getRequestParameters()->getString('user_name');
}
}
type TResponder = classname<WebController>;
final class UriPatternsExample extends BaseRouter<TResponder> {
public static function getControllers(): ImmVector<TResponder> {
return ImmVector {
HomePageController::class,
UserPageController::class,
};
}
<<__Override>>
public function getRoutes(
): ImmMap<HttpMethod, ImmMap<string, TResponder>> {
$urls_to_controllers = dict[];
foreach (self::getControllers() as $controller) {
$pattern = $controller::getFastRoutePattern();
$urls_to_controllers[$pattern] = $controller;
}
return ImmMap {
HttpMethod::GET => new ImmMap($urls_to_controllers),
};
}
}
function get_example_paths(): ImmVector<string> {
return ImmVector {
HomePageController::getUriBuilder()->getPath(),
UserPageController::getUriBuilder()
->setString('user_name', 'Mr Hankey')
->getPath(),
};
}
function main(): void {
$router = new UriPatternsExample();
foreach (get_example_paths() as $path) {
list($controller, $params) = $router->routeMethodAndPath(
HttpMethod::GET,
$path,
);
\printf(
"GET %s\n\t%s\n",
$path,
(new $controller($params))->getResponse(),
);
}
}
/* HH_IGNORE_ERROR[1002] top-level statement in strict file */
main();