-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
AssetMapperRepository.php
198 lines (163 loc) · 5.76 KB
/
AssetMapperRepository.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\AssetMapper;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Iterator\RecursiveDirectoryIterator;
/**
* Finds assets in the asset mapper.
*
* @author Ryan Weaver <[email protected]>
*
* @final
*/
class AssetMapperRepository
{
private ?array $absolutePaths = null;
/**
* @param string[] $paths Array of assets paths: key is the path, value is the namespace
* (empty string for no namespace)
*/
public function __construct(
private readonly array $paths,
private readonly string $projectRootDir,
private readonly array $excludedPathPatterns = [],
private readonly bool $excludeDotFiles = true,
private readonly bool $debug = true,
) {
}
/**
* Given the logical path - styles/app.css - returns the absolute path to the file.
*/
public function find(string $logicalPath): ?string
{
foreach ($this->getDirectories() as $path => $namespace) {
$localLogicalPath = $logicalPath;
// if this path has a namespace, only look for files in that namespace
if ('' !== $namespace) {
if (!str_starts_with($logicalPath, rtrim($namespace, '/').'/')) {
continue;
}
$localLogicalPath = substr($logicalPath, \strlen($namespace) + 1);
}
$file = rtrim($path, '/').'/'.$localLogicalPath;
if (is_file($file) && !$this->isExcluded($file)) {
return realpath($file);
}
}
return null;
}
public function findLogicalPath(string $filesystemPath): ?string
{
if (!is_file($filesystemPath)) {
return null;
}
$filesystemPath = realpath($filesystemPath);
if ($this->isExcluded($filesystemPath)) {
return null;
}
foreach ($this->getDirectories() as $path => $namespace) {
if (!str_starts_with($filesystemPath, $path.\DIRECTORY_SEPARATOR)) {
continue;
}
$logicalPath = substr($filesystemPath, \strlen($path));
if ('' !== $namespace) {
$logicalPath = $namespace.'/'.ltrim($logicalPath, '/\\');
}
return $this->normalizeLogicalPath($logicalPath);
}
return null;
}
/**
* Returns an array of all files in the asset_mapper.
*
* Key is the logical path, value is the absolute path.
*
* @return string[]
*/
public function all(): array
{
$paths = [];
foreach ($this->getDirectories() as $path => $namespace) {
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
foreach ($iterator as $file) {
/** @var \SplFileInfo $file */
if (!$file->isFile()) {
continue;
}
if ($this->isExcluded($file->getPathname())) {
continue;
}
// avoid potentially exposing PHP files
if ('php' === $file->getExtension()) {
continue;
}
/** @var RecursiveDirectoryIterator $innerIterator */
$innerIterator = $iterator->getInnerIterator();
$logicalPath = ($namespace ? rtrim($namespace, '/').'/' : '').$innerIterator->getSubPathName();
$logicalPath = $this->normalizeLogicalPath($logicalPath);
$paths[$logicalPath] = $file->getPathname();
}
}
return $paths;
}
/**
* @internal
*/
public function allDirectories(): array
{
return $this->getDirectories();
}
private function getDirectories(): array
{
$filesystem = new Filesystem();
if (null !== $this->absolutePaths) {
return $this->absolutePaths;
}
$this->absolutePaths = [];
foreach ($this->paths as $path => $namespace) {
if ($filesystem->isAbsolutePath($path)) {
if (!file_exists($path) && $this->debug) {
throw new \InvalidArgumentException(\sprintf('The asset mapper directory "%s" does not exist.', $path));
}
$this->absolutePaths[realpath($path)] = $namespace;
continue;
}
if (file_exists($this->projectRootDir.'/'.$path)) {
$this->absolutePaths[realpath($this->projectRootDir.'/'.$path)] = $namespace;
continue;
}
if ($this->debug) {
throw new \InvalidArgumentException(\sprintf('The asset mapper directory "%s" does not exist.', $path));
}
}
return $this->absolutePaths;
}
/**
* Normalize slashes to / for logical paths.
*/
private function normalizeLogicalPath(string $logicalPath): string
{
return ltrim(str_replace('\\', '/', $logicalPath), '/\\');
}
private function isExcluded(string $filesystemPath): bool
{
// normalize Windows slashes and remove trailing slashes
$filesystemPath = rtrim(str_replace('\\', '/', $filesystemPath), '/');
foreach ($this->excludedPathPatterns as $pattern) {
if (preg_match($pattern, $filesystemPath)) {
return true;
}
}
if ($this->excludeDotFiles && str_starts_with(basename($filesystemPath), '.')) {
return true;
}
return false;
}
}