From f11d5394205cb5b1301b79f7a896254531e552c2 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Thu, 20 May 2010 17:15:36 +0200 Subject: [PATCH] [Templating] added a isFresh() method to Loader classes --- .../Templating/Loader/CacheLoader.php | 12 ++++++++++++ .../Templating/Loader/ChainLoader.php | 18 ++++++++++++++++++ .../Templating/Loader/FilesystemLoader.php | 17 +++++++++++++++++ .../Templating/Loader/LoaderInterface.php | 9 +++++++++ 4 files changed, 56 insertions(+) diff --git a/src/Symfony/Components/Templating/Loader/CacheLoader.php b/src/Symfony/Components/Templating/Loader/CacheLoader.php index 50d3ddab8559..1599aa53931b 100644 --- a/src/Symfony/Components/Templating/Loader/CacheLoader.php +++ b/src/Symfony/Components/Templating/Loader/CacheLoader.php @@ -95,4 +95,16 @@ public function load($template, array $options = array()) return new FileStorage($path, $options['renderer']); } + + /** + * Returns true if the template is still fresh. + * + * @param string $template The template name + * @param array $options An array of options + * @param timestamp $time The last modification time of the cached template + */ + public function isFresh($template, array $options = array(), $time) + { + return $this->loader->isFresh($template, $options); + } } diff --git a/src/Symfony/Components/Templating/Loader/ChainLoader.php b/src/Symfony/Components/Templating/Loader/ChainLoader.php index 7da1290326a2..12f1480434ce 100644 --- a/src/Symfony/Components/Templating/Loader/ChainLoader.php +++ b/src/Symfony/Components/Templating/Loader/ChainLoader.php @@ -67,4 +67,22 @@ public function load($template, array $options = array()) return false; } + + /** + * Returns true if the template is still fresh. + * + * @param string $template The template name + * @param array $options An array of options + * @param timestamp $time The last modification time of the cached template + */ + public function isFresh($template, array $options = array(), $time) + { + foreach ($this->loaders as $loader) { + if (false !== $ret = $loader->load($template, $options)) { + return $loader->isFresh($template, $options); + } + } + + return false; + } } diff --git a/src/Symfony/Components/Templating/Loader/FilesystemLoader.php b/src/Symfony/Components/Templating/Loader/FilesystemLoader.php index 45b021e2eb30..f4f12ba17cce 100644 --- a/src/Symfony/Components/Templating/Loader/FilesystemLoader.php +++ b/src/Symfony/Components/Templating/Loader/FilesystemLoader.php @@ -87,6 +87,23 @@ public function load($template, array $options = array()) return false; } + /** + * Returns true if the template is still fresh. + * + * @param string $template The template name + * @param array $options An array of options + * @param timestamp $time The last modification time of the cached template + */ + public function isFresh($template, array $options = array(), $time) + { + if (false === $template = $this->load($template, $options)) + { + return false; + } + + return filemtime((string) $template) < $time; + } + /** * Returns true if the file is an existing absolute path. * diff --git a/src/Symfony/Components/Templating/Loader/LoaderInterface.php b/src/Symfony/Components/Templating/Loader/LoaderInterface.php index 24268dde99eb..cd5d2f8b2419 100644 --- a/src/Symfony/Components/Templating/Loader/LoaderInterface.php +++ b/src/Symfony/Components/Templating/Loader/LoaderInterface.php @@ -29,4 +29,13 @@ interface LoaderInterface * @return Storage|Boolean false if the template cannot be loaded, a Storage instance otherwise */ function load($template, array $options = array()); + + /** + * Returns true if the template is still fresh. + * + * @param string $template The template name + * @param array $options An array of options + * @param timestamp $time The last modification time of the cached template + */ + function isFresh($template, array $options = array(), $time); }