@@ -49,6 +49,7 @@ final class Http implements Implementation
4949 * @param \Closure(OperatingSystem, Environment): Builder $container
5050 * @param Sequence<callable(Pipe, Container): Component<SideEffect, Response>> $routes
5151 * @param \Closure(Component<SideEffect, Response>, Container): Component<SideEffect, Response> $mapRoute
52+ * @param \Closure(Component<SideEffect, Response>, Container): Component<SideEffect, Response> $mapRoutes
5253 * @param Maybe<callable(ServerRequest, Container): Attempt<Response>> $notFound
5354 * @param \Closure(ServerRequest, \Throwable, Container): Attempt<Response> $recover
5455 */
@@ -58,6 +59,7 @@ private function __construct(
5859 private \Closure $ container ,
5960 private Sequence $ routes ,
6061 private \Closure $ mapRoute ,
62+ private \Closure $ mapRoutes ,
6163 private Maybe $ notFound ,
6264 private \Closure $ recover ,
6365 ) {
@@ -75,7 +77,8 @@ public static function of(OperatingSystem $os): self
7577 $ os ,
7678 static fn (OperatingSystem $ os , Environment $ env ) => [$ os , $ env ],
7779 static fn () => Builder::new (),
78- Sequence::lazyStartingWith (),
80+ Sequence::of (),
81+ static fn (Component $ component ) => $ component ,
7982 static fn (Component $ component ) => $ component ,
8083 $ notFound ,
8184 static fn (ServerRequest $ request , \Throwable $ e ) => Attempt::error ($ e ),
@@ -101,6 +104,7 @@ static function(OperatingSystem $os, Environment $env) use ($previous, $map): ar
101104 $ this ->container ,
102105 $ this ->routes ,
103106 $ this ->mapRoute ,
107+ $ this ->mapRoutes ,
104108 $ this ->notFound ,
105109 $ this ->recover ,
106110 );
@@ -125,6 +129,7 @@ static function(OperatingSystem $os, Environment $env) use ($previous, $map): ar
125129 $ this ->container ,
126130 $ this ->routes ,
127131 $ this ->mapRoute ,
132+ $ this ->mapRoutes ,
128133 $ this ->notFound ,
129134 $ this ->recover ,
130135 );
@@ -147,6 +152,7 @@ public function service(Service $name, callable $definition): self
147152 ),
148153 $ this ->routes ,
149154 $ this ->mapRoute ,
155+ $ this ->mapRoutes ,
150156 $ this ->notFound ,
151157 $ this ->recover ,
152158 );
@@ -182,6 +188,7 @@ public function route(callable $handle): self
182188 $ this ->container ,
183189 ($ this ->routes )($ handle ),
184190 $ this ->mapRoute ,
191+ $ this ->mapRoutes ,
185192 $ this ->notFound ,
186193 $ this ->recover ,
187194 );
@@ -204,6 +211,30 @@ public function mapRoute(callable $map): self
204211 $ previous ($ component , $ get ),
205212 $ get ,
206213 ),
214+ $ this ->mapRoutes ,
215+ $ this ->notFound ,
216+ $ this ->recover ,
217+ );
218+ }
219+
220+ /**
221+ * @psalm-mutation-free
222+ */
223+ #[\Override]
224+ public function mapRoutes (callable $ map ): self
225+ {
226+ $ previous = $ this ->mapRoutes ;
227+
228+ return new self (
229+ $ this ->os ,
230+ $ this ->map ,
231+ $ this ->container ,
232+ $ this ->routes ,
233+ $ this ->mapRoute ,
234+ static fn ($ component , $ get ) => $ map (
235+ $ previous ($ component , $ get ),
236+ $ get ,
237+ ),
207238 $ this ->notFound ,
208239 $ this ->recover ,
209240 );
@@ -221,6 +252,7 @@ public function routeNotFound(callable $handle): self
221252 $ this ->container ,
222253 $ this ->routes ,
223254 $ this ->mapRoute ,
255+ $ this ->mapRoutes ,
224256 Maybe::just ($ handle ),
225257 $ this ->recover ,
226258 );
@@ -240,6 +272,7 @@ public function recoverRouteError(callable $recover): self
240272 $ this ->container ,
241273 $ this ->routes ,
242274 $ this ->mapRoute ,
275+ $ this ->mapRoutes ,
243276 $ this ->notFound ,
244277 static fn ($ request , $ e , $ container ) => $ previous ($ request , $ e , $ container )->recover (
245278 static fn ($ e ) => $ recover ($ request , $ e , $ container ),
@@ -252,9 +285,12 @@ public function run($input): Attempt
252285 {
253286 $ map = $ this ->map ;
254287 $ container = $ this ->container ;
255- $ routes = $ this ->routes ;
288+ $ routes = Sequence::lazyStartingWith ($ this ->routes )->flatMap (
289+ static fn ($ routes ) => $ routes ,
290+ );
256291 $ notFound = $ this ->notFound ;
257292 $ mapRoute = $ this ->mapRoute ;
293+ $ mapRoutes = $ this ->mapRoutes ;
258294 $ recover = $ this ->recover ;
259295
260296 $ run = Commands::of (Serve::of (
@@ -265,6 +301,7 @@ static function(ServerRequest $request, OperatingSystem $os, Map $env) use (
265301 $ routes ,
266302 $ notFound ,
267303 $ mapRoute ,
304+ $ mapRoutes ,
268305 $ recover ,
269306 ): Response {
270307 $ env = Environment::of ($ env );
@@ -275,6 +312,7 @@ static function(ServerRequest $request, OperatingSystem $os, Map $env) use (
275312 ->map (static fn ($ handle ) => $ handle ($ pipe , $ container ))
276313 ->map (static fn ($ component ) => $ mapRoute ($ component , $ container ));
277314 $ router = new Router (
315+ static fn ($ component ) => $ mapRoutes ($ component , $ container ),
278316 $ routes ,
279317 $ notFound ->map (
280318 static fn ($ handle ) => static fn (ServerRequest $ request ) => $ handle (
0 commit comments