44
55use Psr \Http \Message \ServerRequestInterface ;
66
7+ use function is_array ;
8+
79/**
810 * Reproduces API Gateway routing for local development.
911 *
1012 * @internal
1113 */
1214class Router
1315{
16+ public static function fromServerlessConfig (array $ serverlessConfig ): self
17+ {
18+ $ routes = [];
19+ foreach ($ serverlessConfig ['functions ' ] as $ function ) {
20+ $ pattern = $ function ['events ' ][0 ]['httpApi ' ] ?? null ;
21+
22+ if (! $ pattern ) {
23+ continue ;
24+ }
25+
26+ if (is_array ($ pattern )) {
27+ $ pattern = self ::patternToString ($ pattern );
28+ }
29+
30+ $ routes [$ pattern ] = $ function ['handler ' ];
31+ }
32+
33+ return new self ($ routes );
34+ }
35+
36+ private static function patternToString (array $ pattern ): string
37+ {
38+ $ method = $ pattern ['method ' ] ?? '* ' ;
39+ $ path = $ pattern ['path ' ] ?? '* ' ;
40+
41+ // Special "any" method MUST be converted to star.
42+ if ($ method === 'any ' ) {
43+ $ method = '* ' ;
44+ }
45+
46+ // Alternative catch-all MUST be converted to standard catch-all.
47+ if ($ method === '* ' && $ path === '* ' ) {
48+ return '* ' ;
49+ }
50+
51+ return $ method . ' ' . $ path ;
52+ }
53+
1454 /** @var array<string,string> */
1555 private array $ routes ;
1656
@@ -22,17 +62,6 @@ public function __construct(array $routes)
2262 $ this ->routes = $ routes ;
2363 }
2464
25- public static function fromServerlessConfig (array $ serverlessConfig ): self
26- {
27- $ routes = [];
28- foreach ($ serverlessConfig ['functions ' ] as $ function ) {
29- $ pattern = $ function ['events ' ][0 ]['httpApi ' ] ?? null ;
30- if (! $ pattern ) continue ;
31- $ routes [$ pattern ] = $ function ['handler ' ];
32- }
33- return new self ($ routes );
34- }
35-
3665 /**
3766 * @return array{0: ?string, 1: ServerRequestInterface}
3867 */
@@ -58,7 +87,7 @@ private function matchesMethod(ServerRequestInterface $request, string $method):
5887 {
5988 $ method = strtolower ($ method );
6089
61- return ($ method === 'any ' ) || ($ method === strtolower ($ request ->getMethod ()));
90+ return ($ method === '* ' ) || ($ method === strtolower ($ request ->getMethod ()));
6291 }
6392
6493 private function matchesPath (ServerRequestInterface $ request , string $ pathPattern ): bool
0 commit comments