1 | <?php |
||
17 | class FileSystemLocator implements LocatorInterface |
||
18 | { |
||
19 | /** |
||
20 | * @var string[] |
||
21 | */ |
||
22 | private $roots = []; |
||
23 | |||
24 | /** |
||
25 | * @param string[] $roots |
||
26 | */ |
||
27 | public function __construct(array $roots = []) |
||
28 | { |
||
29 | $this->roots = array_map([$this, 'sanitizeRootPath'], $roots); |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * @param string $path |
||
34 | * |
||
35 | * @throws NotLoadableException |
||
36 | * |
||
37 | * @return string |
||
38 | */ |
||
39 | public function locate($path) |
||
40 | { |
||
41 | if (false !== $absolute = $this->locateUsingRootPlaceholder($path)) { |
||
42 | return $this->sanitizeAbsolutePath($absolute); |
||
43 | } |
||
44 | |||
45 | if (false !== $absolute = $this->locateUsingRootPathsSearch($path)) { |
||
46 | return $this->sanitizeAbsolutePath($absolute); |
||
47 | } |
||
48 | |||
49 | throw new NotLoadableException(sprintf('Source image not resolvable "%s" in root path(s) "%s"', |
||
50 | $path, implode(':', $this->roots))); |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * @param string $root |
||
55 | * @param string $path |
||
56 | * |
||
57 | * @return string|false |
||
58 | */ |
||
59 | protected function generateAbsolutePath($root, $path) |
||
60 | { |
||
61 | return realpath($root.DIRECTORY_SEPARATOR.$path); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * @param string $path |
||
66 | * |
||
67 | * @return bool|string |
||
68 | */ |
||
69 | private function locateUsingRootPathsSearch($path) |
||
79 | |||
80 | /** |
||
81 | * @param string $path |
||
82 | * |
||
83 | * @return bool|string |
||
84 | */ |
||
85 | private function locateUsingRootPlaceholder($path) |
||
98 | |||
99 | /** |
||
100 | * @param string $root |
||
101 | * |
||
102 | * @throws InvalidArgumentException |
||
103 | * |
||
104 | * @return string |
||
105 | */ |
||
106 | private function sanitizeRootPath($root) |
||
114 | |||
115 | /** |
||
116 | * @param string $path |
||
117 | * |
||
118 | * @throws NotLoadableException |
||
119 | * |
||
120 | * @return string |
||
121 | */ |
||
122 | private function sanitizeAbsolutePath($path) |
||
133 | } |
||
134 |