Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
namespace Syna;
class ViewLocator
{
/** @var array */
protected $paths = [];
/** @var array */
protected $namedPaths = [];
/** @var array */
protected $map = [];
/** @var string */
protected $extension = '.phtml';
public function __construct(string $path, string $extension = '.phtml')
{
$this->paths[] = $path;
$this->extension = $extension;
}
public function addPath(string $path): self
{
$this->paths[] = $path;
return $this;
}
public function prependPath(string $path): self
{
array_unshift($this->paths, $path);
return $this;
}
public function add($name, $path): self
{
if (file_exists($path)) {
throw new \LogicException('File ' . $path . ' does not exist');
}
$this->map[$name] = $path;
return $this;
}
public function has($name): bool
{
if (isset($this->map[$name])) {
return true;
}
foreach (array_reverse($this->paths) as $path) {
$viewPath = $path . DIRECTORY_SEPARATOR . $name . $this->extension;
if (file_exists($viewPath)) {
$this->map[$name] = $viewPath;
return true;
}
}
return false;
}
public function getPath($name): string
{
if (!$this->has($name)) {
throw new \Exception('View ' . $name . ' not found');
}
return $this->map[$name];
}
}