Browse Source

[Templating] added a isFresh() method to Loader classes

Fabien Potencier 15 years ago
parent
commit
f11d539420

+ 12 - 0
src/Symfony/Components/Templating/Loader/CacheLoader.php

@@ -95,4 +95,16 @@ class CacheLoader extends Loader
 
         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);
+    }
 }

+ 18 - 0
src/Symfony/Components/Templating/Loader/ChainLoader.php

@@ -67,4 +67,22 @@ class ChainLoader extends Loader
 
         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;
+    }
 }

+ 17 - 0
src/Symfony/Components/Templating/Loader/FilesystemLoader.php

@@ -87,6 +87,23 @@ class FilesystemLoader extends Loader
         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.
      *

+ 9 - 0
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);
 }