@@ -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 ) {
@@ -77,6 +79,7 @@ public static function of(OperatingSystem $os): self
7779 static fn () => Builder ::new (),
7880 Sequence ::lazyStartingWith (),
7981 static fn (Component $ component ) => $ component ,
82+ static fn (Component $ component ) => $ component ,
8083 $ notFound ,
8184 static fn (ServerRequest $ request , \Throwable $ e ) => Attempt ::error ($ e ),
8285 );
@@ -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 ),
@@ -255,6 +288,7 @@ public function run($input): Attempt
255288 $ routes = $ this ->routes ;
256289 $ notFound = $ this ->notFound ;
257290 $ mapRoute = $ this ->mapRoute ;
291+ $ mapRoutes = $ this ->mapRoutes ;
258292 $ recover = $ this ->recover ;
259293
260294 $ run = Commands ::of (Serve ::of (
@@ -265,6 +299,7 @@ static function(ServerRequest $request, OperatingSystem $os, Map $env) use (
265299 $ routes ,
266300 $ notFound ,
267301 $ mapRoute ,
302+ $ mapRoutes ,
268303 $ recover ,
269304 ): Response {
270305 $ env = Environment ::of ($ env );
@@ -275,6 +310,7 @@ static function(ServerRequest $request, OperatingSystem $os, Map $env) use (
275310 ->map (static fn ($ handle ) => $ handle ($ pipe , $ container ))
276311 ->map (static fn ($ component ) => $ mapRoute ($ component , $ container ));
277312 $ router = new Router (
313+ static fn ($ component ) => $ mapRoutes ($ component , $ container ),
278314 $ routes ,
279315 $ notFound ->map (
280316 static fn ($ handle ) => static fn (ServerRequest $ request ) => $ handle (
0 commit comments