From f4892c7bda280ee0f087704cf0bc81fa61a7db0b Mon Sep 17 00:00:00 2001 From: Franck DAKIA Date: Mon, 19 Oct 2026 07:24:18 +0000 Subject: [PATCH] Recompile a template edited after, or in the same second as, its cache Filesystem::isExpired() compared the template's mtime with the cache file's ATIME, strictly. Two faults: the atime moves on every read of the cache, so a render could make the cache look newer than a template edited afterwards; and mtimes are whole seconds, so a template saved again in the second it was compiled was never compiled again. Compare with the cache's mtime and treat a tie as expired. A tie costs one extra compile, never a stale page. --- src/Filesystem.php | 7 +++- tests/FilesystemTest.php | 81 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 2 deletions(-) diff --git a/src/Filesystem.php b/src/Filesystem.php index aaae682..033561c 100644 --- a/src/Filesystem.php +++ b/src/Filesystem.php @@ -114,11 +114,14 @@ public function isExpired(string $filename): bool $this->getFileResolvedPath($filename) ); - $cache_fileatime = fileatime( + // mtime, not atime: every render reads the cache and moves its atime. + // And >= because mtimes are whole seconds: a template saved again in + // the same second it was compiled was otherwise never compiled again. + $cache_filemtime = filemtime( $this->getCacheFileResolvedPath($filename) ); - return $fileatime > $cache_fileatime; + return $fileatime >= $cache_filemtime; } /** diff --git a/tests/FilesystemTest.php b/tests/FilesystemTest.php index a91748d..85acca0 100644 --- a/tests/FilesystemTest.php +++ b/tests/FilesystemTest.php @@ -88,4 +88,85 @@ public function testGetFileResolvedPathRejectsTraversal() $this->filesystem->exists('../../../../../../etc/passwd') ); } + + /** + * Build a filesystem over a scratch directory holding one template, so + * the fixtures' timestamps are never touched. + * + * @return array{0: Filesystem, 1: string, 2: string} filesystem, template path, cache path + */ + private function makeScratchFilesystem(): array + { + $root = sys_get_temp_dir() . '/tintin_fs_' . uniqid(); + mkdir($root . '/view', 0777, true); + mkdir($root . '/cache', 0777, true); + + $template = $root . '/view/probe.tintin.php'; + file_put_contents($template, 'Hello {{ $name }}'); + + $filesystem = new Filesystem([ + 'path' => $root . '/view', + 'extension' => 'tintin.php', + 'cache' => $root . '/cache', + ]); + + $filesystem->cache('probe', ''); + + return [$filesystem, $template, $filesystem->getCacheFileResolvedPath('probe')]; + } + + /** + * A template saved again in the same second it was compiled (mtimes are + * whole seconds) must be compiled again, not served stale forever. + */ + public function testIsExpiredWhenTemplateEditedInTheSameSecondAsItsCache() + { + [$filesystem, $template, $cache] = $this->makeScratchFilesystem(); + + $now = time(); + touch($template, $now); + touch($cache, $now); + clearstatcache(); + + $this->assertTrue($filesystem->isExpired('probe')); + } + + public function testIsExpiredFollowsTheTemplateMtime() + { + [$filesystem, $template, $cache] = $this->makeScratchFilesystem(); + + touch($template, time() - 100); + touch($cache, time() - 50); + clearstatcache(); + $this->assertFalse($filesystem->isExpired('probe'), 'cache newer than the template is fresh'); + + touch($template, time() - 10); + clearstatcache(); + $this->assertTrue($filesystem->isExpired('probe'), 'template saved after the cache is stale'); + } + + /** + * Rendering reads the cache and moves its atime; that must not count as + * the cache being "newer" than a template edited afterwards. + */ + public function testIsExpiredIgnoresTheCacheAccessTime() + { + [$filesystem, $template, $cache] = $this->makeScratchFilesystem(); + + touch($cache, time() - 100, time() + 3600); // old mtime, atime in the future + touch($template, time() - 50); + clearstatcache(); + + $this->assertTrue($filesystem->isExpired('probe')); + } + + public function testIsExpiredWhenNothingIsCached() + { + [$filesystem, , $cache] = $this->makeScratchFilesystem(); + + unlink($cache); + clearstatcache(); + + $this->assertTrue($filesystem->isExpired('probe')); + } }