1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AssetManager\Core\Resolver; |
4
|
|
|
|
5
|
|
|
use Assetic\Factory\Resource\DirectoryResource; |
6
|
|
|
use AssetManager\Core\Exception; |
7
|
|
|
use SplFileInfo; |
8
|
|
|
use Zend\Stdlib\SplStack; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* This resolver allows you to resolve from a stack of aliases to a path. |
12
|
|
|
*/ |
13
|
|
|
class AliasPathStackResolver extends FileResolverAbstract |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
protected $aliases = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Flag indicating whether or not LFI protection for rendering view scripts is enabled |
22
|
|
|
* |
23
|
|
|
* @var bool |
24
|
|
|
*/ |
25
|
|
|
protected $lfiProtectionOn = true; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Constructor |
29
|
|
|
* |
30
|
|
|
* Populate the array stack with a list of aliases and their corresponding paths |
31
|
|
|
* |
32
|
|
|
* @param array $aliases |
33
|
|
|
* |
34
|
|
|
* @throws Exception\InvalidArgumentException |
35
|
|
|
*/ |
36
|
|
|
public function __construct(array $aliases) |
37
|
|
|
{ |
38
|
|
|
foreach ($aliases as $alias => $path) { |
39
|
|
|
$this->addAlias($alias, $path); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Add a single alias to the stack |
45
|
|
|
* |
46
|
|
|
* @param string $alias |
47
|
|
|
* @param string $path |
48
|
|
|
* |
49
|
|
|
* @throws Exception\InvalidArgumentException |
50
|
|
|
*/ |
51
|
|
|
private function addAlias($alias, $path) |
52
|
|
|
{ |
53
|
|
|
if (!is_string($path)) { |
54
|
|
|
throw new Exception\InvalidArgumentException(sprintf( |
55
|
|
|
'Invalid path provided; must be a string, received %s', |
56
|
|
|
gettype($path) |
57
|
|
|
)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (!is_string($alias)) { |
61
|
|
|
throw new Exception\InvalidArgumentException(sprintf( |
62
|
|
|
'Invalid alias provided; must be a string, received %s', |
63
|
|
|
gettype($alias) |
64
|
|
|
)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$this->aliases[$alias] = $this->normalizePath($path); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Normalize a path for insertion in the stack |
72
|
|
|
* |
73
|
|
|
* @param string $path |
74
|
|
|
* |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
private function normalizePath($path) |
78
|
|
|
{ |
79
|
|
|
return rtrim($path, '/\\') . DIRECTORY_SEPARATOR; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Set LFI protection flag |
84
|
|
|
* |
85
|
|
|
* @param bool $flag |
86
|
|
|
* @return void |
87
|
|
|
*/ |
88
|
|
|
public function setLfiProtection($flag) |
89
|
|
|
{ |
90
|
|
|
$this->lfiProtectionOn = (bool) $flag; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Return status of LFI protection flag |
95
|
|
|
* |
96
|
|
|
* @return bool |
97
|
|
|
*/ |
98
|
|
|
public function isLfiProtectionOn() |
99
|
|
|
{ |
100
|
|
|
return $this->lfiProtectionOn; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritDoc} |
105
|
|
|
*/ |
106
|
|
|
public function resolve($name) |
107
|
|
|
{ |
108
|
|
|
if ($this->isLfiProtectionOn() && preg_match('#\.\.[\\\/]#', $name)) { |
109
|
|
|
return null; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
foreach ($this->aliases as $alias => $path) { |
113
|
|
|
if (strpos($name, $alias) !== 0) { |
114
|
|
|
continue; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$filename = substr_replace($name, '', 0, strlen($alias)); |
118
|
|
|
|
119
|
|
|
$asset = $this->resolveFile($path . $filename); |
|
|
|
|
120
|
|
|
|
121
|
|
|
if (!$asset) { |
122
|
|
|
continue; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$asset->mimetype = $this->getMimeResolver()->getMimeType($name); |
126
|
|
|
|
127
|
|
|
return $asset; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return null; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* {@inheritDoc} |
135
|
|
|
*/ |
136
|
|
|
public function collect() |
137
|
|
|
{ |
138
|
|
|
$collection = []; |
139
|
|
|
|
140
|
|
|
foreach ($this->aliases as $alias => $path) { |
141
|
|
|
$locations = new SplStack(); |
142
|
|
|
$pathInfo = new SplFileInfo($path); |
143
|
|
|
$locations->push($pathInfo); |
144
|
|
|
$basePath = $this->normalizePath($pathInfo->getRealPath()); |
145
|
|
|
|
146
|
|
|
while (!$locations->isEmpty()) { |
147
|
|
|
/** @var SplFileInfo $pathInfo */ |
148
|
|
|
$pathInfo = $locations->pop(); |
149
|
|
|
if (!$pathInfo->isReadable()) { |
150
|
|
|
throw new Exception\RuntimeException(sprintf('%s is not readable.', $pathInfo->getPath())); |
151
|
|
|
} |
152
|
|
|
if ($pathInfo->isDir()) { |
153
|
|
|
foreach (new DirectoryResource($pathInfo->getRealPath()) as $resource) { |
154
|
|
|
$locations->push(new SplFileInfo($resource)); |
155
|
|
|
} |
156
|
|
|
} else { |
157
|
|
|
$collection[] = $alias . substr($pathInfo->getRealPath(), strlen($basePath)); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return array_unique($collection); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.