1 | <?php |
||
8 | class FileViewFinder implements ViewFinderInterface |
||
9 | { |
||
10 | /** |
||
11 | * The filesystem instance. |
||
12 | * |
||
13 | * @var \Illuminate\Filesystem\Filesystem |
||
14 | */ |
||
15 | protected $files; |
||
16 | |||
17 | /** |
||
18 | * The array of active view paths. |
||
19 | * |
||
20 | * @var array |
||
21 | */ |
||
22 | protected $paths; |
||
23 | |||
24 | /** |
||
25 | * The array of views that have been located. |
||
26 | * |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $views = []; |
||
30 | |||
31 | /** |
||
32 | * The namespace to file path hints. |
||
33 | * |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $hints = []; |
||
37 | |||
38 | /** |
||
39 | * Register a view extension with the finder. |
||
40 | * |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $extensions = ['blade.php', 'php', 'css']; |
||
44 | |||
45 | /** |
||
46 | * Create a new file view loader instance. |
||
47 | * |
||
48 | * @param \Illuminate\Filesystem\Filesystem $files |
||
49 | * @param array $paths |
||
50 | * @param array $extensions |
||
51 | */ |
||
52 | public function __construct(Filesystem $files, array $paths, array $extensions = null) |
||
61 | |||
62 | /** |
||
63 | * Get the fully qualified location of the view. |
||
64 | * |
||
65 | * @param string $name |
||
66 | * @return string |
||
67 | */ |
||
68 | public function find($name) |
||
80 | |||
81 | /** |
||
82 | * Get the path to a template with a named path. |
||
83 | * |
||
84 | * @param string $name |
||
85 | * @return string |
||
86 | */ |
||
87 | protected function findNamespacedView($name) |
||
88 | { |
||
89 | list($namespace, $view) = $this->parseNamespaceSegments($name); |
||
90 | |||
91 | return $this->findInPaths($view, $this->hints[$namespace]); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Get the segments of a template with a named path. |
||
96 | * |
||
97 | * @param string $name |
||
98 | * @return array |
||
99 | * |
||
100 | * @throws \InvalidArgumentException |
||
101 | */ |
||
102 | protected function parseNamespaceSegments($name) |
||
103 | { |
||
104 | $segments = explode(static::HINT_PATH_DELIMITER, $name); |
||
105 | |||
106 | if (count($segments) != 2) { |
||
107 | throw new \InvalidArgumentException("View [$name] has an invalid name."); |
||
108 | } |
||
109 | |||
110 | if (! isset($this->hints[$segments[0]])) { |
||
111 | throw new \InvalidArgumentException("No hint path defined for [{$segments[0]}]."); |
||
112 | } |
||
113 | |||
114 | return $segments; |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Find the given view in the list of paths. |
||
119 | * |
||
120 | * @param string $name |
||
121 | * @param array $paths |
||
122 | * @return string |
||
123 | * |
||
124 | * @throws \InvalidArgumentException |
||
125 | */ |
||
126 | protected function findInPaths($name, $paths) |
||
142 | |||
143 | /** |
||
144 | * Get an array of possible view files. |
||
145 | * |
||
146 | * @param string $name |
||
147 | * @return array |
||
148 | */ |
||
149 | protected function getPossibleViewFiles($name) |
||
155 | |||
156 | /** |
||
157 | * Add a location to the finder. |
||
158 | * |
||
159 | * @param string $location |
||
160 | * @return void |
||
161 | */ |
||
162 | public function addLocation($location) |
||
166 | |||
167 | /** |
||
168 | * Prepend a location to the finder. |
||
169 | * |
||
170 | * @param string $location |
||
171 | * @return void |
||
172 | */ |
||
173 | public function prependLocation($location) |
||
177 | |||
178 | /** |
||
179 | * Add a namespace hint to the finder. |
||
180 | * |
||
181 | * @param string $namespace |
||
182 | * @param string|array $hints |
||
183 | * @return void |
||
184 | */ |
||
185 | public function addNamespace($namespace, $hints) |
||
195 | |||
196 | /** |
||
197 | * Prepend a namespace hint to the finder. |
||
198 | * |
||
199 | * @param string $namespace |
||
200 | * @param string|array $hints |
||
201 | * @return void |
||
202 | */ |
||
203 | public function prependNamespace($namespace, $hints) |
||
213 | |||
214 | /** |
||
215 | * Replace the namespace hints for the given namespace. |
||
216 | * |
||
217 | * @param string $namespace |
||
218 | * @param string|array $hints |
||
219 | * @return void |
||
220 | */ |
||
221 | public function replaceNamespace($namespace, $hints) |
||
225 | |||
226 | /** |
||
227 | * Register an extension with the view finder. |
||
228 | * |
||
229 | * @param string $extension |
||
230 | * @return void |
||
231 | */ |
||
232 | public function addExtension($extension) |
||
240 | |||
241 | /** |
||
242 | * Returns whether or not the view name has any hint information. |
||
243 | * |
||
244 | * @param string $name |
||
245 | * @return bool |
||
246 | */ |
||
247 | public function hasHintInformation($name) |
||
251 | |||
252 | /** |
||
253 | * Flush the cache of located views. |
||
254 | * |
||
255 | * @return void |
||
256 | */ |
||
257 | public function flush() |
||
261 | |||
262 | /** |
||
263 | * Get the filesystem instance. |
||
264 | * |
||
265 | * @return \Illuminate\Filesystem\Filesystem |
||
266 | */ |
||
267 | public function getFilesystem() |
||
271 | |||
272 | /** |
||
273 | * Get the active view paths. |
||
274 | * |
||
275 | * @return array |
||
276 | */ |
||
277 | public function getPaths() |
||
281 | |||
282 | /** |
||
283 | * Get the namespace to file path hints. |
||
284 | * |
||
285 | * @return array |
||
286 | */ |
||
287 | public function getHints() |
||
291 | |||
292 | /** |
||
293 | * Get registered extensions. |
||
294 | * |
||
295 | * @return array |
||
296 | */ |
||
297 | public function getExtensions() |
||
301 | |||
302 | /** |
||
303 | * @param string $name |
||
304 | * @param array $paths |
||
305 | * @param string $path |
||
306 | * @return string |
||
307 | */ |
||
308 | protected function createMissingView($name, $paths, $path): string |
||
322 | } |
||
323 |