added support for multiple directories in the filesystem loader
authorfabien <fabien@93ef8e89-cb99-4229-a87c-7fa0fa45744b>
Sun, 11 Oct 2009 07:37:41 +0000 (07:37 +0000)
committerfabien <fabien@93ef8e89-cb99-4229-a87c-7fa0fa45744b>
Sun, 11 Oct 2009 07:37:41 +0000 (07:37 +0000)
git-svn-id: http://svn.twig-project.org/trunk@20 93ef8e89-cb99-4229-a87c-7fa0fa45744b

lib/Twig/Loader.php
lib/Twig/Loader/Array.php
lib/Twig/Loader/Filesystem.php

index 5b30b4a..db63a58 100644 (file)
@@ -35,7 +35,8 @@ abstract class Twig_Loader implements Twig_LoaderInterface
    *
    *  * An absolute path where to store the compiled templates
    *
-   * @param
+   * @param string  $cache      The compiler cache directory
+   * @param Boolean $autoReload Whether to reload the template is the original source changed
    */
   public function __construct($cache = null, $autoReload = true)
   {
index c308831..ca238b8 100644 (file)
@@ -20,6 +20,15 @@ class Twig_Loader_Array extends Twig_Loader
 {
   protected $templates;
 
+  /**
+   * Constructor.
+   *
+   * @param array   $templates  An array of templates (keys are the names, and values are the source code)
+   * @param string  $cache      The compiler cache directory
+   * @param Boolean $autoReload Whether to reload the template is the original source changed
+   *
+   * @see Twig_Loader
+   */
   public function __construct(array $templates, $cache = null)
   {
     parent::__construct($cache);
index d1394a2..cbceb9b 100644 (file)
  */
 class Twig_Loader_Filesystem extends Twig_Loader
 {
-  protected $folder;
+  protected $folders;
 
-  public function __construct($folder, $cache = null, $autoReload = true)
+  /**
+   * Constructor.
+   *
+   * @param string|array $folders    A folder or an array of folders where to look for templates
+   * @param string       $cache      The compiler cache directory
+   * @param Boolean      $autoReload Whether to reload the template is the original source changed
+   *
+   * @see Twig_Loader
+   */
+  public function __construct($folders, $cache = null, $autoReload = true)
   {
-    $this->folder = realpath($folder);
+    if (!is_array($folders))
+    {
+      $folders = array($folders);
+    }
+
+    $this->folders = array();
+    foreach ($folders as $folder)
+    {
+      $this->folders[] = realpath($folder);
+    }
 
     parent::__construct($cache, $autoReload);
   }
@@ -38,19 +56,24 @@ class Twig_Loader_Filesystem extends Twig_Loader
    */
   public function getSource($name)
   {
-    $file = realpath($this->folder.DIRECTORY_SEPARATOR.$name);
-
-    if (0 !== strpos($file, $this->folder))
+    foreach ($this->folders as $folder)
     {
-      throw new RuntimeException(sprintf('Unable to find template "%s".', $name));
-    }
+      $file = realpath($folder.DIRECTORY_SEPARATOR.$name);
 
-    // simple security check
-    if (0 !== strpos($file, $this->folder))
-    {
-      throw new RuntimeException(sprintf('You cannot load a template outside the "%s" directory.', $this->folder));
+      if (0 !== strpos($file, $folder))
+      {
+        continue;
+      }
+
+      // simple security check
+      if (0 !== strpos($file, $folder))
+      {
+        throw new RuntimeException('Looks like you try to load a template outside configured directories.');
+      }
+
+      return array(file_get_contents($file), filemtime($file));
     }
 
-    return array(file_get_contents($file), filemtime($file));
+    throw new RuntimeException(sprintf('Unable to find template "%s".', $name));
   }
 }