Add array of path support to file system loader.

fix #9
This commit is contained in:
fzerorubigd 2013-03-16 10:53:16 +03:30
parent 65fe447c36
commit 89f0c00790
No known key found for this signature in database
GPG Key ID: D6EE858AF9D2999A

View File

@ -40,21 +40,31 @@ class Handlebars_Loader_FilesystemLoader implements Handlebars_Loader
* Passing an $options array allows overriding certain Loader options during instantiation: * Passing an $options array allows overriding certain Loader options during instantiation:
* *
* $options = array( * $options = array(
* // The filename extension used for Mustache templates. Defaults to '.mustache' * // The filename extension used for Handlebars templates. Defaults to '.handlebars'
* 'extension' => '.ms', * 'extension' => '.other',
* ); * );
* *
* @param string $baseDir Base directory containing Mustache template files. * @param string|array $baseDirs A path contain template files or array of paths
* @param array $options Array of Loader options (default: array()) * @param array $options Array of Loader options (default: array())
* *
* @throws RuntimeException if $baseDir does not exist. * @throws RuntimeException if $baseDir does not exist.
*/ */
public function __construct($baseDir, array $options = array()) public function __construct($baseDirs, array $options = array())
{ {
$this->_baseDir = rtrim(realpath($baseDir), '/'); if (is_string($baseDir)) {
$baseDir = array(rtrim(realpath($baseDir), '/'));
} else {
foreach ($baseDir as &$dir) {
$dir = array(rtrim(realpath($dir), '/'));
}
}
if (!is_dir($this->_baseDir)) { $this->_baseDir = $baseDir;
throw new RuntimeException('FilesystemLoader baseDir must be a directory: '.$baseDir);
foreach ($this->_baseDir as $dir) {
if (!is_dir($dir)) {
throw new RuntimeException('FilesystemLoader baseDir must be a directory: ' . $dir);
}
} }
if (isset($options['extension'])) { if (isset($options['extension'])) {
@ -70,11 +80,11 @@ class Handlebars_Loader_FilesystemLoader implements Handlebars_Loader
* Load a Template by name. * Load a Template by name.
* *
* $loader = new FilesystemLoader(dirname(__FILE__).'/views'); * $loader = new FilesystemLoader(dirname(__FILE__).'/views');
* $loader->load('admin/dashboard'); // loads "./views/admin/dashboard.mustache"; * $loader->load('admin/dashboard'); // loads "./views/admin/dashboard.handlebars";
* *
* @param string $name template name * @param string $name template name
* *
* @return string Handkebars Template source * @return string Handlebars Template source
*/ */
public function load($name) public function load($name)
{ {
@ -86,18 +96,18 @@ class Handlebars_Loader_FilesystemLoader implements Handlebars_Loader
} }
/** /**
* Helper function for loading a Mustache file by name. * Helper function for loading a Handlebars file by name.
* *
* @param string $name template name * @param string $name template name
* *
* @return string Mustache Template source * @return string Handlebars Template source
* @throws InvalidArgumentException if a template file is not found. * @throws InvalidArgumentException if a template file is not found.
*/ */
protected function loadFile($name) protected function loadFile($name)
{ {
$fileName = $this->getFileName($name); $fileName = $this->getFileName($name);
if (!file_exists($fileName)) { if ($fileName === false) {
throw new InvalidArgumentException('Template ' . $name . ' not found.'); throw new InvalidArgumentException('Template ' . $name . ' not found.');
} }
@ -105,7 +115,7 @@ class Handlebars_Loader_FilesystemLoader implements Handlebars_Loader
} }
/** /**
* Helper function for getting a Mustache template file name. * Helper function for getting a Handlebars template file name.
* *
* @param string $name template name * @param string $name template name
* *
@ -113,7 +123,8 @@ class Handlebars_Loader_FilesystemLoader implements Handlebars_Loader
*/ */
protected function getFileName($name) protected function getFileName($name)
{ {
$fileName = $this->_baseDir . '/'; foreach ($this->_baseDir as $baseDir) {
$fileName = $baseDir . '/';
$fileParts = explode('/', $name); $fileParts = explode('/', $name);
$file = array_pop($fileParts); $file = array_pop($fileParts);
@ -127,7 +138,11 @@ class Handlebars_Loader_FilesystemLoader implements Handlebars_Loader
if (substr($fileName, 0 - strlen($this->_extension)) !== $this->_extension) { if (substr($fileName, 0 - strlen($this->_extension)) !== $this->_extension) {
$fileName .= $this->_extension; $fileName .= $this->_extension;
} }
if (file_exists($fileName)) {
break;
}
$fileName = false;
}
return $fileName; return $fileName;
} }
} }