Skip to content

Commit f084123

Browse files
[12.x] memoize result of Application@eventsAreCached() (#57709)
* memoize result of eventsAreCached * Update FoundationApplicationTest.php * Update FoundationApplicationTest.php * Update Application.php --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent f86b948 commit f084123

File tree

2 files changed

+43
-12
lines changed

2 files changed

+43
-12
lines changed

src/Illuminate/Foundation/Application.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1350,7 +1350,13 @@ public function getCachedRoutesPath()
13501350
*/
13511351
public function eventsAreCached()
13521352
{
1353-
return $this['files']->exists($this->getCachedEventsPath());
1353+
if ($this->bound('events.cached')) {
1354+
return (bool) $this->make('events.cached');
1355+
}
1356+
1357+
return $this->instance(
1358+
'events.cached', $this['files']->exists($this->getCachedEventsPath())
1359+
);
13541360
}
13551361

13561362
/**

tests/Foundation/FoundationApplicationTest.php

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -620,23 +620,36 @@ public function test_routes_are_cached()
620620
public function test_routes_are_not_cached_by_instance_falls_back_to_file()
621621
{
622622
$app = new Application();
623-
$files = new class
624-
{
625-
public string $pathRequested;
626-
627-
public function exists(string $path): bool
628-
{
629-
$this->pathRequested = $path;
630-
631-
return false;
632-
}
633-
};
623+
$files = new FileExistsFake;
634624
$app->instance('files', $files);
635625

636626
$this->assertFalse($app->routesAreCached());
637627
$this->assertStringContainsString('routes-v7.php', $files->pathRequested);
638628
}
639629

630+
public function test_events_are_cached_uses_container_instance()
631+
{
632+
$app = new Application();
633+
$app->instance('events.cached', true);
634+
$files = new FileExistsFake;
635+
$app->instance('files', $files);
636+
637+
$this->assertTrue($app->eventsAreCached());
638+
$this->assertFalse(isset($files->pathRequested));
639+
}
640+
641+
public function test_events_are_cached_checks_filesystem_if_not_set()
642+
{
643+
$app = new Application();
644+
$files = new FileExistsFake;
645+
$app->instance('files', $files);
646+
647+
$this->assertFalse($app->eventsAreCached());
648+
$this->assertStringContainsString('events.php', $files->pathRequested);
649+
$this->assertTrue($app->bound('events.cached'));
650+
$this->assertFalse($app->make('events.cached'));
651+
}
652+
640653
public function testCoreContainerAliasesAreRegisteredByDefault(): void
641654
{
642655
$app = new Application();
@@ -782,3 +795,15 @@ public function terminate()
782795
return self::$counter++;
783796
}
784797
}
798+
799+
class FileExistsFake
800+
{
801+
public string $pathRequested;
802+
803+
public function exists(string $path): bool
804+
{
805+
$this->pathRequested = $path;
806+
807+
return false;
808+
}
809+
}

0 commit comments

Comments
 (0)