/
FileSystemLocator.php
146 lines (125 loc) · 3.83 KB
/
FileSystemLocator.php
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
/*
* This file is part of the `liip/LiipImagineBundle` project.
*
* (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/
namespace Liip\ImagineBundle\Binary\Locator;
use Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException;
use Liip\ImagineBundle\Exception\InvalidArgumentException;
class FileSystemLocator implements LocatorInterface
{
/**
* @var string[]
*/
private $roots = [];
/**
* @param string[] $roots
*/
public function __construct(array $roots = [])
{
$this->roots = array_map(function (string $root): string {
return $this->sanitizeRootPath($root);
}, $roots);
}
/**
* @param string $path
*
* @throws NotLoadableException
*
* @return string
*/
public function locate(string $path): string
{
if (null !== $absolute = $this->locateUsingRootPlaceholder($path)) {
return $this->sanitizeAbsolutePath($absolute);
}
if (null !== $absolute = $this->locateUsingRootPathsSearch($path)) {
return $this->sanitizeAbsolutePath($absolute);
}
throw new NotLoadableException(sprintf('Source image not resolvable "%s" in root path(s) "%s"',
$path, implode(':', $this->roots)));
}
/**
* @param string $root
* @param string $path
*
* @return string|null
*/
protected function generateAbsolutePath(string $root, string $path): ?string
{
if (false !== $absolute = realpath($root.\DIRECTORY_SEPARATOR.$path)) {
return $absolute;
}
return null;
}
/**
* @param string $path
*
* @return string|null
*/
private function locateUsingRootPathsSearch(string $path): ?string
{
foreach ($this->roots as $root) {
if (null !== $absolute = $this->generateAbsolutePath($root, $path)) {
return $absolute;
}
}
return null;
}
/**
* @param string $path
*
* @return string|null
*/
private function locateUsingRootPlaceholder(string $path): ?string
{
if (0 !== mb_strpos($path, '@') || 1 !== preg_match('{^@(?<name>[^:]+):(?<path>.+)$}', $path, $match)) {
return null;
}
if (isset($this->roots[$match['name']])) {
return $this->generateAbsolutePath($this->roots[$match['name']], $match['path']);
}
throw new NotLoadableException(sprintf('Invalid root placeholder "@%s" for path "%s"',
$match['name'], $match['path']));
}
/**
* @param string $path
*
* @throws InvalidArgumentException
*
* @return string
*/
private function sanitizeRootPath(string $path): string
{
if (!empty($path) && false !== $real = realpath($path)) {
return $real;
}
throw new InvalidArgumentException(sprintf('Root image path not resolvable "%s"', $path));
}
/**
* @param string $path
*
* @throws NotLoadableException
*
* @return string
*/
private function sanitizeAbsolutePath(string $path): string
{
$roots = array_filter($this->roots, function (string $root) use ($path): bool {
return 0 === mb_strpos($path, $root);
});
if (0 === count($roots)) {
throw new NotLoadableException(
sprintf('Source image invalid "%s" as it is outside of the defined root path(s) "%s"', $path, implode(':', $this->roots))
);
}
if (!is_readable($path)) {
throw new NotLoadableException(sprintf('Source image invalid "%s" as it is not readable', $path));
}
return $path;
}
}