This repository was archived by the owner on Aug 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlookup-path.php
126 lines (119 loc) · 3.91 KB
/
lookup-path.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
#!/usr/bin/env hhvm
<?hh // strict
/**
* This file is partially generated. Only make modifications between BEGIN
* MANUAL SECTION and END MANUAL SECTION designators.
*
* To re-generate this file run vendor/hhvm/hacktest/bin/hacktest
*
*
* @partially-generated SignedSource<<61e1a0bcff805162c34d005bb526d587>>
*/
namespace Facebook\HackRouter\CodeGen\Tests\Generated;
/**
* A quick way to validate that a path or url routes to the controller you
* expect.
*
* Usage: path/to/this/utility <%path or url%>.
* The output will look something like this:
*
* ```
* $ tests/examples/codegen/lookup-path.php /a-string/123/derp
* HEAD: Facebook\HackRouter\CodeGen\Tests\GetRequestExampleController
* GET: Facebook\HackRouter\CodeGen\Tests\GetRequestExampleController
* ```
*
* You may also copy paste the whole url:
* ```
* $ tests/examples/codegen/lookup-path.php
* http://localhost:8080/a-string/123/derp?query=abcd#fragment
* HEAD: Facebook\HackRouter\CodeGen\Tests\GetRequestExampleController
* GET: Facebook\HackRouter\CodeGen\Tests\GetRequestExampleController
* ```
*
* If the given path does not match any controllers, you'll get the following
* result:
* ```
* $ tests/examples/codegen/lookup-path.php /a/b/c
* No controller found for '/a/b/c'.
* ```
*
* You can edit the manual sections to change the router and formatter used.
*/
<<__EntryPoint>>
function hack_router_cli_lookup_generated_main(): void {
/* BEGIN MANUAL SECTION init */
$autoloader = null;
$autoloader_candidates = vec[
__DIR__.'/vendor/autoload.hack',
__DIR__.'/../vendor/autoload.hack',
__DIR__.'/../../vendor/autoload.hack',
__DIR__.'/../../../vendor/autoload.hack',
];
foreach ($autoloader_candidates as $candidate) {
if (\file_exists($candidate)) {
$autoloader = $candidate;
break;
}
}
if ($autoloader === null) {
\fwrite(\STDERR, "Can't find autoloader.\n");
exit(1);
}
require_once($autoloader);
\Facebook\AutoloadMap\initialize();
/* END MANUAL SECTION */
$argv = \Facebook\TypeAssert\matches<vec<string>>(\HH\global_get('argv'));
(new MySiteRouterCLILookup())->main($argv);
}
final class MySiteRouterCLILookup {
private function getRouter(
): \Facebook\HackRouter\CodeGen\Tests\Generated\MySiteRouter {
/* BEGIN MANUAL SECTION MySiteRouterCLILookup::getRouter */
return new \Facebook\HackRouter\CodeGen\Tests\Generated\MySiteRouter();
/* END MANUAL SECTION */
}
private function prettifyControllerName(string $controller): string {
/* BEGIN MANUAL SECTION MySiteRouterCLILookup::prettifyControllerName */
return $controller;
/* END MANUAL SECTION */
}
private function getControllersForPath(
string $path,
): ImmMap<\Facebook\HackRouter\HttpMethod, string> {
$router = $this->getRouter();
try {
$controllers = Map { };
foreach (\Facebook\HackRouter\HttpMethod::getValues() as $method) {
try {
list($controller, $_params) =
$router->routeMethodAndPath($method, $path);
$controllers[$method] = $controller;
} catch (\Facebook\HackRouter\MethodNotAllowedException $_) {
// Ignore
}
}
return $controllers->immutable();
} catch (\Facebook\HackRouter\NotFoundException $_) {
return ImmMap { };
}
}
public function main(vec<string> $argv): void {
$path = $argv[1] ?? null;
if ($path === null) {
\fprintf(\STDERR, "Usage: %s PATH\n", $argv[0]);
exit(1);
}
// The parser is very lenient, `?: $path` is almost never needed.
$path = \parse_url($path, \PHP_URL_PATH) ?: $path;
$controllers = $this->getControllersForPath($path);
if ($controllers->isEmpty()) {
\printf("No controller found for '%s'.\n", $path);
exit(1);
}
foreach ($controllers as $method => $controller) {
$pretty = $this->prettifyControllerName($controller);
\printf("%-8s %s\n", $method.':', $pretty);
}
}
}