-
Notifications
You must be signed in to change notification settings - Fork 381
/
FileSystemLocator.php
159 lines (137 loc) · 4.25 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
147
148
149
150
151
152
153
154
155
156
157
158
159
<?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;
use Symfony\Component\OptionsResolver\Exception\ExceptionInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class FileSystemLocator implements LocatorInterface
{
/**
* @var string[]
*/
private $roots = [];
/**
* @param string[] $roots
*/
public function __construct(array $roots = array())
{
$this->roots = array_map(array($this, 'sanitizeRootPath'), $roots);
}
/**
* @deprecated Since version 0.9.0, use __construct(array $roots) instead
*
* @param array[] $options
*/
public function setOptions(array $options = [])
{
$resolver = new OptionsResolver();
$resolver->setDefaults(['roots' => []]);
try {
$options = $resolver->resolve($options);
} catch (ExceptionInterface $e) {
throw new InvalidArgumentException(sprintf('Invalid options provided to %s()', __METHOD__), null, $e);
}
@trigger_error(
sprintf('%s() is deprecated. Pass the data roots to the constructor instead.', __METHOD__),
E_USER_DEPRECATED
);
$this->roots = array_map([$this, 'sanitizeRootPath'], (array) $options['roots']);
}
/**
* @param string $path
*
* @throws NotLoadableException
*
* @return string
*/
public function locate($path)
{
if (false !== $absolute = $this->locateUsingRootPlaceholder($path)) {
return $this->sanitizeAbsolutePath($absolute);
}
if (false !== $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 $path
*
* @return bool|string
*/
private function locateUsingRootPathsSearch($path)
{
foreach ($this->roots as $root) {
if (false !== $absolute = $this->generateAbsolutePath($root, $path)) {
return $absolute;
}
}
return false;
}
/**
* @param string $path
*
* @return bool|string
*/
private function locateUsingRootPlaceholder($path)
{
if (0 !== strpos($path, '@') || 1 !== preg_match('{@(?<name>[^:]+):(?<path>.+)}', $path, $matches)) {
return false;
}
if (isset($this->roots[$matches['name']])) {
return $this->generateAbsolutePath($this->roots[$matches['name']], $matches['path']);
}
throw new NotLoadableException(sprintf('Invalid root placeholder "%s" for path "%s"',
$matches['name'], $matches['path']));
}
/**
* @param string $root
* @param string $path
*
* @return string|false
*/
protected function generateAbsolutePath($root, $path)
{
return realpath($root.DIRECTORY_SEPARATOR.$path);
}
/**
* @param string $root
*
* @throws InvalidArgumentException
*
* @return string
*/
private function sanitizeRootPath($root)
{
if (!empty($root) && false !== $realRoot = realpath($root)) {
return $realRoot;
}
throw new InvalidArgumentException(sprintf('Root image path not resolvable "%s"', $root));
}
/**
* @param string $path
*
* @throws NotLoadableException
*
* @return string
*/
private function sanitizeAbsolutePath($path)
{
foreach ($this->roots as $root) {
if (0 === strpos($path, $root)) {
return $path;
}
}
throw new NotLoadableException(sprintf('Source image invalid "%s" as it is outside of the defined root path(s) "%s"',
$path, implode(':', $this->roots)));
}
}