HandlebarsLoaderFilesystemLoader

Handlebars Template filesystem Loader implementation.

Defined (2)

The class is defined in the following location(s).

/vendor/calderawp/metaplate-core/vendor/xamin/handlebars.php/src/Handlebars/Loader/FilesystemLoader.php  
  1. class FilesystemLoader implements Loader 
  2. private $_baseDir; 
  3. private $_extension = '.handlebars'; 
  4. private $_prefix = ''; 
  5. private $_templates = array(); 
  6.  
  7. /** 
  8. * Handlebars filesystem Loader constructor. 
  9. * $options array allows overriding certain Loader options during instantiation: 
  10. * $options = array( 
  11. * // extension used for Handlebars templates. Defaults to '.handlebars' 
  12. * 'extension' => '.other',  
  13. * ); 
  14. * @param string|array $baseDirs A path contain template files or array of paths 
  15. * @param array $options Array of Loader options (default: array()) 
  16. * @throws \RuntimeException if $baseDir does not exist. 
  17. */ 
  18. public function __construct($baseDirs, array $options = array()) 
  19. if (is_string($baseDirs)) { 
  20. $baseDirs = array(rtrim(realpath($baseDirs), '/')); 
  21. } else { 
  22. foreach ($baseDirs as &$dir) { 
  23. $dir = rtrim(realpath($dir), '/'); 
  24. unset($dir); 
  25.  
  26. $this->_baseDir = $baseDirs; 
  27.  
  28. foreach ($this->_baseDir as $dir) { 
  29. if (!is_dir($dir)) { 
  30. throw new \RuntimeException( 
  31. 'FilesystemLoader baseDir must be a directory: ' . $dir 
  32. ); 
  33.  
  34. if (isset($options['extension'])) { 
  35. $this->_extension = '.' . ltrim($options['extension'], '.'); 
  36.  
  37. if (isset($options['prefix'])) { 
  38. $this->_prefix = $options['prefix']; 
  39.  
  40. /** 
  41. * Load a Template by name. 
  42. * $loader = new FilesystemLoader(dirname(__FILE__).'/views'); 
  43. * // loads "./views/admin/dashboard.handlebars"; 
  44. * $loader->load('admin/dashboard'); 
  45. * @param string $name template name 
  46. * @return String Handlebars Template source 
  47. */ 
  48. public function load($name) 
  49. if (!isset($this->_templates[$name])) { 
  50. $this->_templates[$name] = $this->loadFile($name); 
  51.  
  52. return new String($this->_templates[$name]); 
  53.  
  54. /** 
  55. * Helper function for loading a Handlebars file by name. 
  56. * @param string $name template name 
  57. * @throws \InvalidArgumentException if a template file is not found. 
  58. * @return string Handlebars Template source 
  59. */ 
  60. protected function loadFile($name) 
  61. $fileName = $this->getFileName($name); 
  62.  
  63. if ($fileName === false) { 
  64. throw new \InvalidArgumentException('Template ' . $name . ' not found.'); 
  65.  
  66. return file_get_contents($fileName); 
  67.  
  68. /** 
  69. * Helper function for getting a Handlebars template file name. 
  70. * @param string $name template name 
  71. * @return string Template file name 
  72. */ 
  73. protected function getFileName($name) 
  74. foreach ($this->_baseDir as $baseDir) { 
  75. $fileName = $baseDir . '/'; 
  76. $fileParts = explode('/', $name); 
  77. $file = array_pop($fileParts); 
  78.  
  79. if (substr($file, strlen($this->_prefix)) !== $this->_prefix) { 
  80. $file = $this->_prefix . $file; 
  81.  
  82. $fileParts[] = $file; 
  83. $fileName .= implode('/', $fileParts); 
  84. $lastCharacters = substr($fileName, 0 - strlen($this->_extension)); 
  85.  
  86. if ($lastCharacters !== $this->_extension) { 
  87. $fileName .= $this->_extension; 
  88. if (file_exists($fileName)) { 
  89. return $fileName; 
  90.  
  91. return false; 
  92.  
/vendor/xamin/handlebars.php/src/Handlebars/Loader/FilesystemLoader.php  
  1. class FilesystemLoader implements Loader 
  2. private $_baseDir; 
  3. private $_extension = '.handlebars'; 
  4. private $_prefix = ''; 
  5. private $_templates = array(); 
  6.  
  7. /** 
  8. * Handlebars filesystem Loader constructor. 
  9. * $options array allows overriding certain Loader options during instantiation: 
  10. * $options = array( 
  11. * // extension used for Handlebars templates. Defaults to '.handlebars' 
  12. * 'extension' => '.other',  
  13. * ); 
  14. * @param string|array $baseDirs A path contain template files or array of paths 
  15. * @param array $options Array of Loader options (default: array()) 
  16. * @throws \RuntimeException if $baseDir does not exist. 
  17. */ 
  18. public function __construct($baseDirs, array $options = array()) 
  19. if (is_string($baseDirs)) { 
  20. $baseDirs = array(rtrim(realpath($baseDirs), '/')); 
  21. } else { 
  22. foreach ($baseDirs as &$dir) { 
  23. $dir = rtrim(realpath($dir), '/'); 
  24. unset($dir); 
  25.  
  26. $this->_baseDir = $baseDirs; 
  27.  
  28. foreach ($this->_baseDir as $dir) { 
  29. if (!is_dir($dir)) { 
  30. throw new \RuntimeException( 
  31. 'FilesystemLoader baseDir must be a directory: ' . $dir 
  32. ); 
  33.  
  34. if (isset($options['extension'])) { 
  35. $this->_extension = '.' . ltrim($options['extension'], '.'); 
  36.  
  37. if (isset($options['prefix'])) { 
  38. $this->_prefix = $options['prefix']; 
  39.  
  40. /** 
  41. * Load a Template by name. 
  42. * $loader = new FilesystemLoader(dirname(__FILE__).'/views'); 
  43. * // loads "./views/admin/dashboard.handlebars"; 
  44. * $loader->load('admin/dashboard'); 
  45. * @param string $name template name 
  46. * @return String Handlebars Template source 
  47. */ 
  48. public function load($name) 
  49. if (!isset($this->_templates[$name])) { 
  50. $this->_templates[$name] = $this->loadFile($name); 
  51.  
  52. return new String($this->_templates[$name]); 
  53.  
  54. /** 
  55. * Helper function for loading a Handlebars file by name. 
  56. * @param string $name template name 
  57. * @throws \InvalidArgumentException if a template file is not found. 
  58. * @return string Handlebars Template source 
  59. */ 
  60. protected function loadFile($name) 
  61. $fileName = $this->getFileName($name); 
  62.  
  63. if ($fileName === false) { 
  64. throw new \InvalidArgumentException('Template ' . $name . ' not found.'); 
  65.  
  66. return file_get_contents($fileName); 
  67.  
  68. /** 
  69. * Helper function for getting a Handlebars template file name. 
  70. * @param string $name template name 
  71. * @return string Template file name 
  72. */ 
  73. protected function getFileName($name) 
  74. foreach ($this->_baseDir as $baseDir) { 
  75. $fileName = $baseDir . '/'; 
  76. $fileParts = explode('/', $name); 
  77. $file = array_pop($fileParts); 
  78.  
  79. if (substr($file, strlen($this->_prefix)) !== $this->_prefix) { 
  80. $file = $this->_prefix . $file; 
  81.  
  82. $fileParts[] = $file; 
  83. $fileName .= implode('/', $fileParts); 
  84. $lastCharacters = substr($fileName, 0 - strlen($this->_extension)); 
  85.  
  86. if ($lastCharacters !== $this->_extension) { 
  87. $fileName .= $this->_extension; 
  88. if (file_exists($fileName)) { 
  89. return $fileName; 
  90.  
  91. return false; 
  92.