@@ -100,10 +100,12 @@ public static function of(OperatingSystem $os): self
100100 */
101101 public function mapEnvironment (callable $ map ): self
102102 {
103+ $ previous = $ this ->map ;
104+
103105 return new self (
104106 $ this ->os ,
105- function (OperatingSystem $ os , Environment $ env ) use ($ map ): array {
106- [$ os , $ env ] = ( $ this -> map ) ($ os , $ env );
107+ static function (OperatingSystem $ os , Environment $ env ) use ($ previous , $ map ): array {
108+ [$ os , $ env ] = $ previous ($ os , $ env );
107109 $ env = $ map ($ env , $ os );
108110
109111 return [$ os , $ env ];
@@ -120,10 +122,12 @@ function(OperatingSystem $os, Environment $env) use ($map): array {
120122 */
121123 public function mapOperatingSystem (callable $ map ): self
122124 {
125+ $ previous = $ this ->map ;
126+
123127 return new self (
124128 $ this ->os ,
125- function (OperatingSystem $ os , Environment $ env ) use ($ map ): array {
126- [$ os , $ env ] = ( $ this -> map ) ($ os , $ env );
129+ static function (OperatingSystem $ os , Environment $ env ) use ($ previous , $ map ): array {
130+ [$ os , $ env ] = $ previous ($ os , $ env );
127131 $ os = $ map ($ os , $ env );
128132
129133 return [$ os , $ env ];
@@ -140,10 +144,12 @@ function(OperatingSystem $os, Environment $env) use ($map): array {
140144 */
141145 public function service (string |Service $ name , callable $ definition ): self
142146 {
147+ $ container = $ this ->container ;
148+
143149 return new self (
144150 $ this ->os ,
145151 $ this ->map ,
146- fn (OperatingSystem $ os , Environment $ env ) => ( $ this -> container ) ($ os , $ env )->add (
152+ static fn (OperatingSystem $ os , Environment $ env ) => $ container ($ os , $ env )->add (
147153 $ name ,
148154 static fn ($ service ) => $ definition ($ service , $ os , $ env ),
149155 ),
@@ -207,18 +213,20 @@ public function appendRoutes(callable $append): self
207213 */
208214 public function mapRequestHandler (callable $ map ): self
209215 {
216+ $ previous = $ this ->mapRequestHandler ;
217+
210218 return new self (
211219 $ this ->os ,
212220 $ this ->map ,
213221 $ this ->container ,
214222 $ this ->routes ,
215- fn (
223+ static fn (
216224 RequestHandler $ handler ,
217225 Container $ container ,
218226 OperatingSystem $ os ,
219227 Environment $ env ,
220228 ) => $ map (
221- ( $ this -> mapRequestHandler ) ($ handler , $ container , $ os , $ env ),
229+ $ previous ($ handler , $ container , $ os , $ env ),
222230 $ container ,
223231 $ os ,
224232 $ env ,
@@ -244,13 +252,25 @@ public function notFoundRequestHandler(callable $handle): self
244252
245253 public function run ($ input )
246254 {
255+ $ map = $ this ->map ;
256+ $ container = $ this ->container ;
257+ $ routes = $ this ->routes ;
258+ $ notFound = $ this ->notFound ;
259+ $ mapRequestHandler = $ this ->mapRequestHandler ;
260+
247261 $ run = Commands::of (Serve::of (
248262 $ this ->os ,
249- function (ServerRequest $ request , OperatingSystem $ os ): Response {
263+ static function (ServerRequest $ request , OperatingSystem $ os ) use (
264+ $ map ,
265+ $ container ,
266+ $ routes ,
267+ $ notFound ,
268+ $ mapRequestHandler ,
269+ ): Response {
250270 $ env = Environment::http ($ request ->environment ());
251- [$ os , $ env ] = ( $ this -> map ) ($ os , $ env );
252- $ container = ( $ this -> container ) ($ os , $ env )->build ();
253- $ routes = Sequence::lazyStartingWith ($ this -> routes )
271+ [$ os , $ env ] = $ map ($ os , $ env );
272+ $ container = $ container ($ os , $ env )->build ();
273+ $ routes = Sequence::lazyStartingWith ($ routes )
254274 ->flatMap (static fn ($ routes ) => $ routes )
255275 ->map (static fn ($ provide ) => $ provide (
256276 Routes::lazy (),
@@ -261,7 +281,7 @@ function(ServerRequest $request, OperatingSystem $os): Response {
261281 ->flatMap (static fn ($ routes ) => $ routes ->toSequence ());
262282 $ router = new Router (
263283 $ routes ,
264- $ this -> notFound ->map (
284+ $ notFound ->map (
265285 static fn ($ handle ) => static fn (ServerRequest $ request ) => $ handle (
266286 $ request ,
267287 $ container ,
@@ -270,7 +290,7 @@ function(ServerRequest $request, OperatingSystem $os): Response {
270290 ),
271291 ),
272292 );
273- $ handle = ( $ this -> mapRequestHandler ) ($ router , $ container , $ os , $ env );
293+ $ handle = $ mapRequestHandler ($ router , $ container , $ os , $ env );
274294
275295 return $ handle ($ request );
276296 },
0 commit comments