Skip to content

Commit cc84146

Browse files
committed
Merge branch 'develop'
* develop: specify next release use static closures to avoid capturing $this to avoid circular references
2 parents 68ce0e5 + dbdb2aa commit cc84146

6 files changed

Lines changed: 75 additions & 33 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## 2.3.1 - 2024-10-26
4+
5+
### Changed
6+
7+
- Use `static` closures as much as possible to reduce the probability of creating circular references by capturing `$this` as it can lead to memory root buffer exhaustion.
8+
39
## 2.3.0 - 2024-08-01
410

511
### Added

src/Application/Async/Http.php

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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
},

src/Application/Cli.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,12 @@ public function mapOperatingSystem(callable $map): self
109109
*/
110110
public function service(string|Service $name, callable $definition): self
111111
{
112+
$container = $this->container;
113+
112114
return new self(
113115
$this->os,
114116
$this->env,
115-
fn(OperatingSystem $os, Environment $env) => ($this->container)($os, $env)->add(
117+
static fn(OperatingSystem $os, Environment $env) => $container($os, $env)->add(
116118
$name,
117119
static fn($service) => $definition($service, $os, $env),
118120
),
@@ -144,18 +146,20 @@ public function command(callable $command): self
144146
*/
145147
public function mapCommand(callable $map): self
146148
{
149+
$previous = $this->mapCommand;
150+
147151
return new self(
148152
$this->os,
149153
$this->env,
150154
$this->container,
151155
$this->commands,
152-
fn(
156+
static fn(
153157
Command $command,
154158
Container $service,
155159
OperatingSystem $os,
156160
Environment $env,
157161
) => $map(
158-
($this->mapCommand)($command, $service, $os, $env),
162+
$previous($command, $service, $os, $env),
159163
$service,
160164
$os,
161165
$env,
@@ -198,11 +202,14 @@ public function notFoundRequestHandler(callable $handle): self
198202
public function run($input)
199203
{
200204
$container = ($this->container)($this->os, $this->env)->build();
201-
$mapCommand = fn(Command $command): Command => ($this->mapCommand)(
205+
$mapCommand = $this->mapCommand;
206+
$os = $this->os;
207+
$env = $this->env;
208+
$mapCommand = static fn(Command $command): Command => $mapCommand(
202209
$command,
203210
$container,
204-
$this->os,
205-
$this->env,
211+
$os,
212+
$env,
206213
);
207214
$commands = $this->commands->map(static fn($service) => new Defer(
208215
$service,

src/Application/Http.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,12 @@ public function mapOperatingSystem(callable $map): self
121121
*/
122122
public function service(string|Service $name, callable $definition): self
123123
{
124+
$container = $this->container;
125+
124126
return new self(
125127
$this->os,
126128
$this->env,
127-
fn(OperatingSystem $os, Environment $env) => ($this->container)($os, $env)->add(
129+
static fn(OperatingSystem $os, Environment $env) => $container($os, $env)->add(
128130
$name,
129131
static fn($service) => $definition($service, $os, $env),
130132
),
@@ -188,18 +190,20 @@ public function appendRoutes(callable $append): self
188190
*/
189191
public function mapRequestHandler(callable $map): self
190192
{
193+
$previous = $this->mapRequestHandler;
194+
191195
return new self(
192196
$this->os,
193197
$this->env,
194198
$this->container,
195199
$this->routes,
196-
fn(
200+
static fn(
197201
RequestHandler $handler,
198202
Container $container,
199203
OperatingSystem $os,
200204
Environment $env,
201205
) => $map(
202-
($this->mapRequestHandler)($handler, $container, $os, $env),
206+
$previous($handler, $container, $os, $env),
203207
$container,
204208
$os,
205209
$env,
@@ -226,23 +230,25 @@ public function notFoundRequestHandler(callable $handle): self
226230
public function run($input)
227231
{
228232
$container = ($this->container)($this->os, $this->env)->build();
233+
$os = $this->os;
234+
$env = $this->env;
229235
$routes = Sequence::lazyStartingWith($this->routes)
230236
->flatMap(static fn($routes) => $routes)
231-
->map(fn($provide) => $provide(
237+
->map(static fn($provide) => $provide(
232238
Routes::lazy(),
233239
$container,
234-
$this->os,
235-
$this->env,
240+
$os,
241+
$env,
236242
))
237243
->flatMap(static fn($routes) => $routes->toSequence());
238244
$router = new Router(
239245
$routes,
240246
$this->notFound->map(
241-
fn($handle) => fn(ServerRequest $request) => $handle(
247+
static fn($handle) => static fn(ServerRequest $request) => $handle(
242248
$request,
243249
$container,
244-
$this->os,
245-
$this->env,
250+
$os,
251+
$env,
246252
),
247253
),
248254
);

src/Http/Router.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ public function __construct(Sequence $routes, Maybe $notFound)
4141
public function __invoke(ServerRequest $request): Response
4242
{
4343
$match = new RequestMatcher($this->routes);
44+
$notFound = $this->notFound;
4445

4546
return $match($request)
4647
->map(static fn($route) => $route->respondTo(...))
47-
->otherwise(fn() => $this->notFound)
48+
->otherwise(static fn() => $notFound)
4849
->match(
4950
static fn($handle) => $handle($request),
5051
static fn() => Response::of(

src/Middleware/LoadDotEnv.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,16 @@ private function __construct(Path $folder)
3030

3131
public function __invoke(Application $app): Application
3232
{
33+
$folder = $this->folder;
34+
3335
return $app->mapEnvironment(
34-
fn($env, $os) => $os
36+
static fn($env, $os) => $os
3537
->filesystem()
36-
->mount($this->folder)
38+
->mount($folder)
3739
->get(Name::of('.env'))
3840
->keep(Instance::of(File::class))
3941
->match(
40-
fn($file) => $this->add($env, $file),
42+
static fn($file) => self::add($env, $file),
4143
static fn() => $env,
4244
),
4345
);
@@ -48,7 +50,7 @@ public static function at(Path $folder): self
4850
return new self($folder);
4951
}
5052

51-
private function add(Environment $env, File $file): Environment
53+
private static function add(Environment $env, File $file): Environment
5254
{
5355
/** @psalm-suppress InvalidArgument Due to the empty sequence in the flatMap */
5456
return $file

0 commit comments

Comments
 (0)