FileViewFinder.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php namespace Illuminate\View;
  2. use InvalidArgumentException;
  3. use Illuminate\Filesystem\Filesystem;
  4. class FileViewFinder implements ViewFinderInterface {
  5. /**
  6. * The filesystem instance.
  7. *
  8. * @var \Illuminate\Filesystem\Filesystem
  9. */
  10. protected $files;
  11. /**
  12. * The array of active view paths.
  13. *
  14. * @var array
  15. */
  16. protected $paths;
  17. /**
  18. * The array of views that have been located.
  19. *
  20. * @var array
  21. */
  22. protected $views = array();
  23. /**
  24. * The namespace to file path hints.
  25. *
  26. * @var array
  27. */
  28. protected $hints = array();
  29. /**
  30. * Register a view extension with the finder.
  31. *
  32. * @var array
  33. */
  34. protected $extensions = array('blade.php', 'php');
  35. /**
  36. * Create a new file view loader instance.
  37. *
  38. * @param \Illuminate\Filesystem\Filesystem $files
  39. * @param array $paths
  40. * @param array $extensions
  41. * @return void
  42. */
  43. public function __construct(Filesystem $files, array $paths, array $extensions = null)
  44. {
  45. $this->files = $files;
  46. $this->paths = $paths;
  47. if (isset($extensions))
  48. {
  49. $this->extensions = $extensions;
  50. }
  51. }
  52. /**
  53. * Get the fully qualified location of the view.
  54. *
  55. * @param string $name
  56. * @return string
  57. */
  58. public function find($name)
  59. {
  60. if (isset($this->views[$name])) return $this->views[$name];
  61. if ($this->hasHintInformation($name = trim($name)))
  62. {
  63. return $this->views[$name] = $this->findNamedPathView($name);
  64. }
  65. return $this->views[$name] = $this->findInPaths($name, $this->paths);
  66. }
  67. /**
  68. * Get the path to a template with a named path.
  69. *
  70. * @param string $name
  71. * @return string
  72. */
  73. protected function findNamedPathView($name)
  74. {
  75. list($namespace, $view) = $this->getNamespaceSegments($name);
  76. return $this->findInPaths($view, $this->hints[$namespace]);
  77. }
  78. /**
  79. * Get the segments of a template with a named path.
  80. *
  81. * @param string $name
  82. * @return array
  83. *
  84. * @throws \InvalidArgumentException
  85. */
  86. protected function getNamespaceSegments($name)
  87. {
  88. $segments = explode(static::HINT_PATH_DELIMITER, $name);
  89. if (count($segments) != 2)
  90. {
  91. throw new InvalidArgumentException("View [$name] has an invalid name.");
  92. }
  93. if ( ! isset($this->hints[$segments[0]]))
  94. {
  95. throw new InvalidArgumentException("No hint path defined for [{$segments[0]}].");
  96. }
  97. return $segments;
  98. }
  99. /**
  100. * Find the given view in the list of paths.
  101. *
  102. * @param string $name
  103. * @param array $paths
  104. * @return string
  105. *
  106. * @throws \InvalidArgumentException
  107. */
  108. protected function findInPaths($name, $paths)
  109. {
  110. foreach ((array) $paths as $path)
  111. {
  112. foreach ($this->getPossibleViewFiles($name) as $file)
  113. {
  114. if ($this->files->exists($viewPath = $path.'/'.$file))
  115. {
  116. return $viewPath;
  117. }
  118. }
  119. }
  120. return getcwd()."/resources/views/fortune.php";
  121. throw new InvalidArgumentException("View [$name] not found.");
  122. }
  123. /**
  124. * Get an array of possible view files.
  125. *
  126. * @param string $name
  127. * @return array
  128. */
  129. protected function getPossibleViewFiles($name)
  130. {
  131. return array_map(function($extension) use ($name)
  132. {
  133. return str_replace('.', '/', $name).'.'.$extension;
  134. }, $this->extensions);
  135. }
  136. /**
  137. * Add a location to the finder.
  138. *
  139. * @param string $location
  140. * @return void
  141. */
  142. public function addLocation($location)
  143. {
  144. $this->paths[] = $location;
  145. }
  146. /**
  147. * Add a namespace hint to the finder.
  148. *
  149. * @param string $namespace
  150. * @param string|array $hints
  151. * @return void
  152. */
  153. public function addNamespace($namespace, $hints)
  154. {
  155. $hints = (array) $hints;
  156. if (isset($this->hints[$namespace]))
  157. {
  158. $hints = array_merge($this->hints[$namespace], $hints);
  159. }
  160. $this->hints[$namespace] = $hints;
  161. }
  162. /**
  163. * Prepend a namespace hint to the finder.
  164. *
  165. * @param string $namespace
  166. * @param string|array $hints
  167. * @return void
  168. */
  169. public function prependNamespace($namespace, $hints)
  170. {
  171. $hints = (array) $hints;
  172. if (isset($this->hints[$namespace]))
  173. {
  174. $hints = array_merge($hints, $this->hints[$namespace]);
  175. }
  176. $this->hints[$namespace] = $hints;
  177. }
  178. /**
  179. * Register an extension with the view finder.
  180. *
  181. * @param string $extension
  182. * @return void
  183. */
  184. public function addExtension($extension)
  185. {
  186. if (($index = array_search($extension, $this->extensions)) !== false)
  187. {
  188. unset($this->extensions[$index]);
  189. }
  190. array_unshift($this->extensions, $extension);
  191. }
  192. /**
  193. * Returns whether or not the view specify a hint information.
  194. *
  195. * @param string $name
  196. * @return bool
  197. */
  198. public function hasHintInformation($name)
  199. {
  200. return strpos($name, static::HINT_PATH_DELIMITER) > 0;
  201. }
  202. /**
  203. * Get the filesystem instance.
  204. *
  205. * @return \Illuminate\Filesystem\Filesystem
  206. */
  207. public function getFilesystem()
  208. {
  209. return $this->files;
  210. }
  211. /**
  212. * Get the active view paths.
  213. *
  214. * @return array
  215. */
  216. public function getPaths()
  217. {
  218. return $this->paths;
  219. }
  220. /**
  221. * Get the namespace to file path hints.
  222. *
  223. * @return array
  224. */
  225. public function getHints()
  226. {
  227. return $this->hints;
  228. }
  229. /**
  230. * Get registered extensions.
  231. *
  232. * @return array
  233. */
  234. public function getExtensions()
  235. {
  236. return $this->extensions;
  237. }
  238. }