55namespace MicroPHP \Framework \Router ;
66
77use League \Route \RouteGroup ;
8+ use League \Route \Strategy \OptionsHandlerInterface ;
89
910class Router extends \League \Route \Router
1011{
@@ -15,4 +16,51 @@ public function getOrPost(string $path, $handler): RouteGroup
1516 $ group ->post ($ path , $ handler );
1617 });
1718 }
19+
20+ /**
21+ * @return Route[]
22+ */
23+ public function getRoutes (): array
24+ {
25+ return $ this ->routes ;
26+ }
27+
28+ public function map (string $ method , string $ path , $ handler ): Route
29+ {
30+ $ path = sprintf ('/%s ' , ltrim ($ path , '/ ' ));
31+ $ route = new Route ($ method , $ path , $ handler );
32+
33+ $ this ->routes [] = $ route ;
34+
35+ return $ route ;
36+ }
37+
38+ protected function buildOptionsRoutes (array $ options ): void
39+ {
40+ if (!($ this ->getStrategy () instanceof OptionsHandlerInterface)) {
41+ return ;
42+ }
43+
44+ /** @var OptionsHandlerInterface $strategy */
45+ $ strategy = $ this ->getStrategy ();
46+
47+ foreach ($ options as $ identifier => $ methods ) {
48+ [$ scheme , $ host , $ port , $ path ] = explode (static ::IDENTIFIER_SEPARATOR , $ identifier );
49+ $ route = new Route ('OPTIONS ' , $ path , $ strategy ->getOptionsCallable ($ methods ));
50+
51+ if (!empty ($ scheme )) {
52+ $ route ->setScheme ($ scheme );
53+ }
54+
55+ if (!empty ($ host )) {
56+ $ route ->setHost ($ host );
57+ }
58+
59+ if (!empty ($ port )) {
60+ $ route ->setPort ((int )$ port );
61+ }
62+
63+ $ this ->routeCollector ->addRoute ($ route ->getMethod (), $ this ->parseRoutePath ($ route ->getPath ()), $ route );
64+ }
65+ }
1866}
0 commit comments