From: fabien Date: Sun, 11 Oct 2009 07:37:41 +0000 (+0000) Subject: added support for multiple directories in the filesystem loader X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=561dea5f8a62480d40a0b5313b137ce94efa51e8;p=konrad%2Ftwig.git added support for multiple directories in the filesystem loader git-svn-id: http://svn.twig-project.org/trunk@20 93ef8e89-cb99-4229-a87c-7fa0fa45744b --- diff --git a/lib/Twig/Loader.php b/lib/Twig/Loader.php index 5b30b4a..db63a58 100644 --- a/lib/Twig/Loader.php +++ b/lib/Twig/Loader.php @@ -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) { diff --git a/lib/Twig/Loader/Array.php b/lib/Twig/Loader/Array.php index c308831..ca238b8 100644 --- a/lib/Twig/Loader/Array.php +++ b/lib/Twig/Loader/Array.php @@ -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); diff --git a/lib/Twig/Loader/Filesystem.php b/lib/Twig/Loader/Filesystem.php index d1394a2..cbceb9b 100644 --- a/lib/Twig/Loader/Filesystem.php +++ b/lib/Twig/Loader/Filesystem.php @@ -18,11 +18,29 @@ */ 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)); } }