1
|
<?php
|
2
|
|
3
|
namespace Illuminate\View;
|
4
|
|
5
|
use InvalidArgumentException;
|
6
|
use Illuminate\Filesystem\Filesystem;
|
7
|
|
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'];
|
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
|
* @return void
|
52
|
*/
|
53
|
public function __construct(Filesystem $files, array $paths, array $extensions = null)
|
54
|
{
|
55
|
$this->files = $files;
|
56
|
$this->paths = $paths;
|
57
|
|
58
|
if (isset($extensions)) {
|
59
|
$this->extensions = $extensions;
|
60
|
}
|
61
|
}
|
62
|
|
63
|
/**
|
64
|
* Get the fully qualified location of the view.
|
65
|
*
|
66
|
* @param string $name
|
67
|
* @return string
|
68
|
*/
|
69
|
public function find($name)
|
70
|
{
|
71
|
if (isset($this->views[$name])) {
|
72
|
return $this->views[$name];
|
73
|
}
|
74
|
|
75
|
if ($this->hasHintInformation($name = trim($name))) {
|
76
|
return $this->views[$name] = $this->findNamedPathView($name);
|
77
|
}
|
78
|
|
79
|
return $this->views[$name] = $this->findInPaths($name, $this->paths);
|
80
|
}
|
81
|
|
82
|
/**
|
83
|
* Get the path to a template with a named path.
|
84
|
*
|
85
|
* @param string $name
|
86
|
* @return string
|
87
|
*/
|
88
|
protected function findNamedPathView($name)
|
89
|
{
|
90
|
list($namespace, $view) = $this->getNamespaceSegments($name);
|
91
|
|
92
|
return $this->findInPaths($view, $this->hints[$namespace]);
|
93
|
}
|
94
|
|
95
|
/**
|
96
|
* Get the segments of a template with a named path.
|
97
|
*
|
98
|
* @param string $name
|
99
|
* @return array
|
100
|
*
|
101
|
* @throws \InvalidArgumentException
|
102
|
*/
|
103
|
protected function getNamespaceSegments($name)
|
104
|
{
|
105
|
$segments = explode(static::HINT_PATH_DELIMITER, $name);
|
106
|
|
107
|
if (count($segments) != 2) {
|
108
|
throw new InvalidArgumentException("View [$name] has an invalid name.");
|
109
|
}
|
110
|
|
111
|
if (! isset($this->hints[$segments[0]])) {
|
112
|
throw new InvalidArgumentException("No hint path defined for [{$segments[0]}].");
|
113
|
}
|
114
|
|
115
|
return $segments;
|
116
|
}
|
117
|
|
118
|
/**
|
119
|
* Find the given view in the list of paths.
|
120
|
*
|
121
|
* @param string $name
|
122
|
* @param array $paths
|
123
|
* @return string
|
124
|
*
|
125
|
* @throws \InvalidArgumentException
|
126
|
*/
|
127
|
protected function findInPaths($name, $paths)
|
128
|
{
|
129
|
foreach ((array) $paths as $path) {
|
130
|
foreach ($this->getPossibleViewFiles($name) as $file) {
|
131
|
if ($this->files->exists($viewPath = $path.'/'.$file)) {
|
132
|
return $viewPath;
|
133
|
}
|
134
|
}
|
135
|
}
|
136
|
|
137
|
throw new InvalidArgumentException("View [$name] not found.");
|
138
|
}
|
139
|
|
140
|
/**
|
141
|
* Get an array of possible view files.
|
142
|
*
|
143
|
* @param string $name
|
144
|
* @return array
|
145
|
*/
|
146
|
protected function getPossibleViewFiles($name)
|
147
|
{
|
148
|
return array_map(function ($extension) use ($name) {
|
149
|
return str_replace('.', '/', $name).'.'.$extension;
|
150
|
}, $this->extensions);
|
151
|
}
|
152
|
|
153
|
/**
|
154
|
* Add a location to the finder.
|
155
|
*
|
156
|
* @param string $location
|
157
|
* @return void
|
158
|
*/
|
159
|
public function addLocation($location)
|
160
|
{
|
161
|
$this->paths[] = $location;
|
162
|
}
|
163
|
|
164
|
/**
|
165
|
* Add a namespace hint to the finder.
|
166
|
*
|
167
|
* @param string $namespace
|
168
|
* @param string|array $hints
|
169
|
* @return void
|
170
|
*/
|
171
|
public function addNamespace($namespace, $hints)
|
172
|
{
|
173
|
$hints = (array) $hints;
|
174
|
|
175
|
if (isset($this->hints[$namespace])) {
|
176
|
$hints = array_merge($this->hints[$namespace], $hints);
|
177
|
}
|
178
|
|
179
|
$this->hints[$namespace] = $hints;
|
180
|
}
|
181
|
|
182
|
/**
|
183
|
* Prepend a namespace hint to the finder.
|
184
|
*
|
185
|
* @param string $namespace
|
186
|
* @param string|array $hints
|
187
|
* @return void
|
188
|
*/
|
189
|
public function prependNamespace($namespace, $hints)
|
190
|
{
|
191
|
$hints = (array) $hints;
|
192
|
|
193
|
if (isset($this->hints[$namespace])) {
|
194
|
$hints = array_merge($hints, $this->hints[$namespace]);
|
195
|
}
|
196
|
|
197
|
$this->hints[$namespace] = $hints;
|
198
|
}
|
199
|
|
200
|
/**
|
201
|
* Register an extension with the view finder.
|
202
|
*
|
203
|
* @param string $extension
|
204
|
* @return void
|
205
|
*/
|
206
|
public function addExtension($extension)
|
207
|
{
|
208
|
if (($index = array_search($extension, $this->extensions)) !== false) {
|
209
|
unset($this->extensions[$index]);
|
210
|
}
|
211
|
|
212
|
array_unshift($this->extensions, $extension);
|
213
|
}
|
214
|
|
215
|
/**
|
216
|
* Returns whether or not the view specify a hint information.
|
217
|
*
|
218
|
* @param string $name
|
219
|
* @return bool
|
220
|
*/
|
221
|
public function hasHintInformation($name)
|
222
|
{
|
223
|
return strpos($name, static::HINT_PATH_DELIMITER) > 0;
|
224
|
}
|
225
|
|
226
|
/**
|
227
|
* Get the filesystem instance.
|
228
|
*
|
229
|
* @return \Illuminate\Filesystem\Filesystem
|
230
|
*/
|
231
|
public function getFilesystem()
|
232
|
{
|
233
|
return $this->files;
|
234
|
}
|
235
|
|
236
|
/**
|
237
|
* Get the active view paths.
|
238
|
*
|
239
|
* @return array
|
240
|
*/
|
241
|
public function getPaths()
|
242
|
{
|
243
|
return $this->paths;
|
244
|
}
|
245
|
|
246
|
/**
|
247
|
* Get the namespace to file path hints.
|
248
|
*
|
249
|
* @return array
|
250
|
*/
|
251
|
public function getHints()
|
252
|
{
|
253
|
return $this->hints;
|
254
|
}
|
255
|
|
256
|
/**
|
257
|
* Get registered extensions.
|
258
|
*
|
259
|
* @return array
|
260
|
*/
|
261
|
public function getExtensions()
|
262
|
{
|
263
|
return $this->extensions;
|
264
|
}
|
265
|
}
|