PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Illuminate/Blade/Loader.php

https://github.com/clauddiu/Laravel3-Blade
PHP | 244 lines | 97 code | 32 blank | 115 comment | 6 complexity | 47a29cd179a42b577e2d2794a6284b17 MD5 | raw file
  1. <?php namespace Illuminate\Blade;
  2. use Illuminate\Filesystem;
  3. class Loader {
  4. /**
  5. * The paths to the template files.
  6. *
  7. * @var array
  8. */
  9. protected $paths = array();
  10. /**
  11. * The path where cached templates are stored.
  12. *
  13. * @var string
  14. */
  15. protected $cache;
  16. /**
  17. * The Illuminate filesystem instance.
  18. *
  19. * @var Illuminate\Filesystem
  20. */
  21. protected $files;
  22. /**
  23. * The Blade compiler implementation.
  24. *
  25. * @var Illuminate\Blade\Compiler
  26. */
  27. protected $compiler;
  28. /**
  29. * Create a new Blade template loader.
  30. *
  31. * @param Illuminate\Blade\CompilerInterface $compiler
  32. * @param Illuminate\Filesystem $files
  33. * @param string $path
  34. * @param string $cache
  35. * @return void
  36. */
  37. public function __construct(Compiler $compiler, Filesystem $files, $path, $cache)
  38. {
  39. $this->cache = $cache;
  40. $this->files = $files;
  41. $this->compiler = $compiler;
  42. $this->addTemplatePath('*', $path);
  43. }
  44. /**
  45. * Create a new Blade template loader.
  46. *
  47. * @param string $path
  48. * @param string $cache
  49. * @return void
  50. */
  51. public static function make($path, $cache)
  52. {
  53. return new static(new Compiler, new Filesystem, $path, $cache);
  54. }
  55. /**
  56. * Determine if a given template exists.
  57. *
  58. * @param string $view
  59. * @return bool
  60. */
  61. public function exists($view)
  62. {
  63. return $this->files->exists($this->getFullPath($view));
  64. }
  65. /**
  66. * Get the path to the given template.
  67. *
  68. * @param string $view
  69. * @return string
  70. */
  71. public function getPath($view)
  72. {
  73. // We'll store all compiled templates using the MD5 hash of their full path
  74. // since that gives us a convenient, valid file name to use for all the
  75. // templates that will be used and should be a quick view identifier.
  76. if ( ! $this->exists($view))
  77. {
  78. throw new InvalidViewException;
  79. }
  80. $hash = md5($path = $this->getFullPath($view));
  81. $cachePath = $this->getFullCachePath($hash);
  82. // If the template source has been changed since it was last compiled we'll
  83. // re-compile the template and write a new version of the compiled view
  84. // contents to the cache directory on disk then return its full path.
  85. if ($this->isExpired($path, $cachePath))
  86. {
  87. $this->recompile($path, $cachePath);
  88. }
  89. return $cachePath;
  90. }
  91. /**
  92. * Determine if the given template is expired.
  93. *
  94. * @param string $path
  95. * @param string $cachePath
  96. * @return bool
  97. */
  98. protected function isExpired($path, $cachePath)
  99. {
  100. if ( ! $this->files->exists($cachePath))
  101. {
  102. return true;
  103. }
  104. $lastModified = $this->files->lastModified($path);
  105. return $lastModified >= $this->files->lastModified($cachePath);
  106. }
  107. /**
  108. * Recompile the view at a given path.
  109. *
  110. * @param string $path
  111. * @param string $cachePath
  112. * @return void
  113. */
  114. protected function recompile($path, $cachePath)
  115. {
  116. $compiled = $this->compiler->compile($this->files->get($path));
  117. $this->files->put($cachePath, $compiled);
  118. }
  119. /**
  120. * Get the fully qualified path to a view.
  121. *
  122. * @param string $view
  123. * @return string
  124. */
  125. public function getFullPath($view)
  126. {
  127. if (strpos($view, 'path: ') === 0)
  128. {
  129. return substr($view, 6);
  130. }
  131. // If the view begins with a double colon, it means we are loading the view
  132. // from a named view path, so we will grab the path for the named views
  133. // and return it to the caller so the view can be loaded from there.
  134. if (strpos($view, '::') !== false)
  135. {
  136. return $this->getNamedViewPath($view);
  137. }
  138. else
  139. {
  140. $view = $this->formatViewName($view);
  141. return rtrim($this->paths['*'], '/').'/'.$view;
  142. }
  143. }
  144. /**
  145. * Get the fully qualified path to a view.
  146. *
  147. * @param string $view
  148. * @return string
  149. */
  150. protected function getNamedViewPath($view)
  151. {
  152. list($name, $view) = explode('::', $view);
  153. $view = $this->formatViewName($view);
  154. // If the given name hasn't been registered with a path, we'll throw an
  155. // exception to alert the developer that the given view is not valid.
  156. // Probably this is a simple misspelling or something of the like.
  157. if ( ! isset($this->paths[$name]))
  158. {
  159. throw new InvalidViewException;
  160. }
  161. return rtrim($this->paths[$name], '/').'/'.$view;
  162. }
  163. /**
  164. * Format the view name to use directory slashes.
  165. *
  166. * @param string $view
  167. * @return string
  168. */
  169. protected function formatViewName($view)
  170. {
  171. return str_replace('.', '/', $view).'.blade.php';
  172. }
  173. /**
  174. * Get the fully qualified path to a cached template.
  175. *
  176. * @param string $hash
  177. * @return string
  178. */
  179. protected function getFullCachePath($hash)
  180. {
  181. return rtrim($this->cache, '/').'/'.$hash;
  182. }
  183. /**
  184. * Add a path to the template loader.
  185. *
  186. * @param string $name
  187. * @param string $path
  188. * @return void
  189. */
  190. public function addTemplatePath($name, $path)
  191. {
  192. $this->paths[$name] = $path;
  193. }
  194. /**
  195. * Get the Blade compiler used by the loader.
  196. *
  197. * @return Illuminate\Blade\Compiler
  198. */
  199. public function getCompiler()
  200. {
  201. return $this->compiler;
  202. }
  203. /**
  204. * Get the Illuminate filesystem used by the loader.
  205. *
  206. * @return Illuminate\Filesystem
  207. */
  208. public function getFilesystem()
  209. {
  210. return $this->files;
  211. }
  212. }