Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\View;
4
 
5
use Illuminate\Filesystem\Filesystem;
6
use InvalidArgumentException;
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 string[]
42
     */
43
    protected $extensions = ['blade.php', 'php', 'css', 'html'];
44
 
45
    /**
46
     * Create a new file view loader instance.
47
     *
48
     * @param  \Illuminate\Filesystem\Filesystem  $files
49
     * @param  array  $paths
50
     * @param  array|null  $extensions
51
     * @return void
52
     */
53
    public function __construct(Filesystem $files, array $paths, array $extensions = null)
54
    {
55
        $this->files = $files;
56
        $this->paths = array_map([$this, 'resolvePath'], $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->findNamespacedView($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 findNamespacedView($name)
89
    {
90
        [$namespace, $view] = $this->parseNamespaceSegments($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 parseNamespaceSegments($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(fn ($extension) => str_replace('.', '/', $name).'.'.$extension, $this->extensions);
149
    }
150
 
151
    /**
152
     * Add a location to the finder.
153
     *
154
     * @param  string  $location
155
     * @return void
156
     */
157
    public function addLocation($location)
158
    {
159
        $this->paths[] = $this->resolvePath($location);
160
    }
161
 
162
    /**
163
     * Prepend a location to the finder.
164
     *
165
     * @param  string  $location
166
     * @return void
167
     */
168
    public function prependLocation($location)
169
    {
170
        array_unshift($this->paths, $this->resolvePath($location));
171
    }
172
 
173
    /**
174
     * Resolve the path.
175
     *
176
     * @param  string  $path
177
     * @return string
178
     */
179
    protected function resolvePath($path)
180
    {
181
        return realpath($path) ?: $path;
182
    }
183
 
184
    /**
185
     * Add a namespace hint to the finder.
186
     *
187
     * @param  string  $namespace
188
     * @param  string|array  $hints
189
     * @return void
190
     */
191
    public function addNamespace($namespace, $hints)
192
    {
193
        $hints = (array) $hints;
194
 
195
        if (isset($this->hints[$namespace])) {
196
            $hints = array_merge($this->hints[$namespace], $hints);
197
        }
198
 
199
        $this->hints[$namespace] = $hints;
200
    }
201
 
202
    /**
203
     * Prepend a namespace hint to the finder.
204
     *
205
     * @param  string  $namespace
206
     * @param  string|array  $hints
207
     * @return void
208
     */
209
    public function prependNamespace($namespace, $hints)
210
    {
211
        $hints = (array) $hints;
212
 
213
        if (isset($this->hints[$namespace])) {
214
            $hints = array_merge($hints, $this->hints[$namespace]);
215
        }
216
 
217
        $this->hints[$namespace] = $hints;
218
    }
219
 
220
    /**
221
     * Replace the namespace hints for the given namespace.
222
     *
223
     * @param  string  $namespace
224
     * @param  string|array  $hints
225
     * @return void
226
     */
227
    public function replaceNamespace($namespace, $hints)
228
    {
229
        $this->hints[$namespace] = (array) $hints;
230
    }
231
 
232
    /**
233
     * Register an extension with the view finder.
234
     *
235
     * @param  string  $extension
236
     * @return void
237
     */
238
    public function addExtension($extension)
239
    {
240
        if (($index = array_search($extension, $this->extensions)) !== false) {
241
            unset($this->extensions[$index]);
242
        }
243
 
244
        array_unshift($this->extensions, $extension);
245
    }
246
 
247
    /**
248
     * Returns whether or not the view name has any hint information.
249
     *
250
     * @param  string  $name
251
     * @return bool
252
     */
253
    public function hasHintInformation($name)
254
    {
255
        return strpos($name, static::HINT_PATH_DELIMITER) > 0;
256
    }
257
 
258
    /**
259
     * Flush the cache of located views.
260
     *
261
     * @return void
262
     */
263
    public function flush()
264
    {
265
        $this->views = [];
266
    }
267
 
268
    /**
269
     * Get the filesystem instance.
270
     *
271
     * @return \Illuminate\Filesystem\Filesystem
272
     */
273
    public function getFilesystem()
274
    {
275
        return $this->files;
276
    }
277
 
278
    /**
279
     * Set the active view paths.
280
     *
281
     * @param  array  $paths
282
     * @return $this
283
     */
284
    public function setPaths($paths)
285
    {
286
        $this->paths = $paths;
287
 
288
        return $this;
289
    }
290
 
291
    /**
292
     * Get the active view paths.
293
     *
294
     * @return array
295
     */
296
    public function getPaths()
297
    {
298
        return $this->paths;
299
    }
300
 
301
    /**
302
     * Get the views that have been located.
303
     *
304
     * @return array
305
     */
306
    public function getViews()
307
    {
308
        return $this->views;
309
    }
310
 
311
    /**
312
     * Get the namespace to file path hints.
313
     *
314
     * @return array
315
     */
316
    public function getHints()
317
    {
318
        return $this->hints;
319
    }
320
 
321
    /**
322
     * Get registered extensions.
323
     *
324
     * @return array
325
     */
326
    public function getExtensions()
327
    {
328
        return $this->extensions;
329
    }
330
}