FileViewFinder.php 6.5KB

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