From 6c9aa9573c9413916621a08eef216b23d3835f03 Mon Sep 17 00:00:00 2001 From: Dave Stein Date: Sun, 17 May 2015 16:03:39 -0400 Subject: [PATCH] FilesystemLoader: Updates to be more extendable Directory setting can be overridden The way a directory is sanitized can be overridden The way options are handled can be overridden --- src/Handlebars/Loader/FilesystemLoader.php | 92 +++++++++++++++------- 1 file changed, 64 insertions(+), 28 deletions(-) diff --git a/src/Handlebars/Loader/FilesystemLoader.php b/src/Handlebars/Loader/FilesystemLoader.php index 1010fbf..3fcf6da 100755 --- a/src/Handlebars/Loader/FilesystemLoader.php +++ b/src/Handlebars/Loader/FilesystemLoader.php @@ -12,6 +12,7 @@ * @author Behrooz Shabani * @author Craig Bass * @author ^^ + * @author Dave Stein * @copyright 2010-2012 (c) Justin Hileman * @copyright 2012 (c) ParsPooyesh Co * @copyright 2013 (c) Behrooz Shabani @@ -40,7 +41,7 @@ use Handlebars\String; class FilesystemLoader implements Loader { - private $_baseDir; + protected $baseDir; private $_extension = '.handlebars'; private $_prefix = ''; private $_templates = array(); @@ -62,32 +63,8 @@ class FilesystemLoader implements Loader */ public function __construct($baseDirs, array $options = array()) { - if (is_string($baseDirs)) { - $baseDirs = array(rtrim(realpath($baseDirs), '/')); - } else { - foreach ($baseDirs as &$dir) { - $dir = rtrim(realpath($dir), '/'); - } - unset($dir); - } - - $this->_baseDir = $baseDirs; - - foreach ($this->_baseDir as $dir) { - if (!is_dir($dir)) { - throw new \RuntimeException( - 'FilesystemLoader baseDir must be a directory: ' . $dir - ); - } - } - - if (isset($options['extension'])) { - $this->_extension = '.' . ltrim($options['extension'], '.'); - } - - if (isset($options['prefix'])) { - $this->_prefix = $options['prefix']; - } + $this->setBaseDir($baseDirs); + $this->handleOptions($options); } /** @@ -110,6 +87,65 @@ class FilesystemLoader implements Loader return new String($this->_templates[$name]); } + /** + * Sets directories to load templates from + * + * @param string|array $baseDirs A path contain template files or array of paths + * + * @return void + */ + protected function setBaseDir($baseDirs) + { + if (is_string($baseDirs)) { + $baseDirs = array($this->sanitizeDirectory($baseDirs)); + } else { + foreach ($baseDirs as &$dir) { + $dir = $this->sanitizeDirectory($dir); + } + unset($dir); + } + + foreach ($baseDirs as $dir) { + if (!is_dir($dir)) { + throw new \RuntimeException( + 'FilesystemLoader baseDir must be a directory: ' . $dir + ); + } + } + + $this->baseDir = $baseDirs; + } + + /** + * Puts directory into standardized format + * + * @param String $dir The directory to sanitize + * + * @return String + */ + protected function sanitizeDirectory($dir) + { + return rtrim(realpath($dir), '/'); + } + + /** + * Sets properties based on options + * + * @param array $options Array of Loader options (default: array()) + * + * @return void + */ + protected function handleOptions(array $options = array()) + { + if (isset($options['extension'])) { + $this->_extension = '.' . ltrim($options['extension'], '.'); + } + + if (isset($options['prefix'])) { + $this->_prefix = $options['prefix']; + } + } + /** * Helper function for loading a Handlebars file by name. * @@ -138,7 +174,7 @@ class FilesystemLoader implements Loader */ protected function getFileName($name) { - foreach ($this->_baseDir as $baseDir) { + foreach ($this->baseDir as $baseDir) { $fileName = $baseDir . '/'; $fileParts = explode('/', $name); $file = array_pop($fileParts);