-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Copy pathFileSystem.php
252 lines (235 loc) · 7.91 KB
/
FileSystem.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\View;
/**
* Model that finds file paths by their fileId
*
* @api
* @since 100.0.2
*/
class FileSystem
{
/**
* @var \Magento\Framework\View\Design\FileResolution\Fallback\File
*/
protected $_fileResolution;
/**
* @var \Magento\Framework\View\Design\FileResolution\Fallback\TemplateFile
*/
protected $_templateFileResolution;
/**
* @var \Magento\Framework\View\Design\FileResolution\Fallback\LocaleFile
*/
protected $_localeFileResolution;
/**
* @var \Magento\Framework\View\Design\FileResolution\Fallback\StaticFile
*/
protected $_staticFileResolution;
/**
* @var \Magento\Framework\View\Design\FileResolution\Fallback\EmailTemplateFile
*/
protected $_emailTemplateFileResolution;
/**
* View service
*
* @var \Magento\Framework\View\Asset\Repository
*/
protected $_assetRepo;
/**
* Constructor
*
* @param \Magento\Framework\View\Design\FileResolution\Fallback\File $fallbackFile
* @param \Magento\Framework\View\Design\FileResolution\Fallback\TemplateFile $fallbackTemplateFile
* @param \Magento\Framework\View\Design\FileResolution\Fallback\LocaleFile $fallbackLocaleFile
* @param \Magento\Framework\View\Design\FileResolution\Fallback\StaticFile $fallbackStaticFile
* @param \Magento\Framework\View\Design\FileResolution\Fallback\EmailTemplateFile $fallbackEmailTemplateFile
* @param \Magento\Framework\View\Asset\Repository $assetRepo
*/
public function __construct(
\Magento\Framework\View\Design\FileResolution\Fallback\File $fallbackFile,
\Magento\Framework\View\Design\FileResolution\Fallback\TemplateFile $fallbackTemplateFile,
\Magento\Framework\View\Design\FileResolution\Fallback\LocaleFile $fallbackLocaleFile,
\Magento\Framework\View\Design\FileResolution\Fallback\StaticFile $fallbackStaticFile,
\Magento\Framework\View\Design\FileResolution\Fallback\EmailTemplateFile $fallbackEmailTemplateFile,
\Magento\Framework\View\Asset\Repository $assetRepo
) {
$this->_fileResolution = $fallbackFile;
$this->_templateFileResolution = $fallbackTemplateFile;
$this->_localeFileResolution = $fallbackLocaleFile;
$this->_staticFileResolution = $fallbackStaticFile;
$this->_emailTemplateFileResolution = $fallbackEmailTemplateFile;
$this->_assetRepo = $assetRepo;
}
/**
* Get existing file name with fallback to default
*
* @param string $fileId
* @param array $params
* @return string
*/
public function getFilename($fileId, array $params = [])
{
list($module, $filePath) = \Magento\Framework\View\Asset\Repository::extractModule(
$this->normalizePath($fileId)
);
if ($module) {
$params['module'] = $module;
}
$this->_assetRepo->updateDesignParams($params);
$file = $this->_fileResolution
->getFile($params['area'], $params['themeModel'], $filePath, $params['module']);
return $file;
}
/**
* Get a locale file
*
* @param string $file
* @param array $params
* @return string
*/
public function getLocaleFileName($file, array $params = [])
{
$this->_assetRepo->updateDesignParams($params);
return $this->_localeFileResolution
->getFile($params['area'], $params['themeModel'], $params['locale'], $file);
}
/**
* Get a template file
*
* @param string $fileId
* @param array $params
* @return string|bool
*/
public function getTemplateFileName($fileId, array $params = [])
{
list($module, $filePath) = \Magento\Framework\View\Asset\Repository::extractModule(
$this->normalizePath($fileId)
);
if ($module) {
$params['module'] = $module;
}
$this->_assetRepo->updateDesignParams($params);
return $this->_templateFileResolution
->getFile($params['area'], $params['themeModel'], $filePath, $params['module']);
}
/**
* Find a static view file using fallback mechanism
*
* @param string $fileId
* @param array $params
* @return string
*/
public function getStaticFileName($fileId, array $params = [])
{
list($module, $filePath) = \Magento\Framework\View\Asset\Repository::extractModule(
$this->normalizePath($fileId)
);
if ($module) {
$params['module'] = $module;
}
$this->_assetRepo->updateDesignParams($params);
return $this->_staticFileResolution
->getFile($params['area'], $params['themeModel'], $params['locale'], $filePath, $params['module']);
}
/**
* Get an email template file
*
* @param string $fileId
* @param array $params
* @param string $module
* @return string|bool
*/
public function getEmailTemplateFileName($fileId, array $params, $module)
{
$this->_assetRepo->updateDesignParams($params);
return $this->_emailTemplateFileResolution
->getFile($params['area'], $params['themeModel'], $params['locale'], $fileId, $module);
}
/**
* Remove excessive "." and ".." parts from a path
*
* For example foo/bar/../file.ext -> foo/file.ext
*
* @param string $path
* @return string
*/
public static function normalizePath($path)
{
$parts = explode('/', $path);
$result = [];
foreach ($parts as $part) {
if ('..' === $part) {
if (!count($result) || ($result[count($result) - 1] == '..')) {
$result[] = $part;
} else {
array_pop($result);
}
} elseif ('.' !== $part) {
$result[] = $part;
}
}
return implode('/', $result);
}
/**
* Get a relative path between $relatedPath and $path paths as if $path was to refer to $relatedPath
* relatively of itself
*
* Returns new calculated relative path.
* Examples:
* $path: /some/directory/one/file.ext
* $relatedPath: /some/directory/two/another/file.ext
* Result: ../two/another
*
* $path: http://example.com/themes/demo/css/styles.css
* $relatedPath: http://example.com/images/logo.gif
* Result: ../../../images
*
* @param string $relatedPath
* @param string $path
* @return string
*/
public static function offsetPath($relatedPath, $path)
{
$relatedPath = self::normalizePath($relatedPath);
$path = self::normalizePath($path);
list($relatedPath, $path) = self::ltrimSamePart($relatedPath, $path);
$toDir = ltrim(dirname($path), '/');
if ($toDir == '.') {
$offset = '';
} else {
$offset = str_repeat('../', count(explode('/', $toDir)));
}
return rtrim($offset . dirname($relatedPath), '/');
}
/**
* Concatenate/normalize a path to another path as a relative, assuming it will be relative to its directory
*
* @param string $relativeTo
* @param string $path
* @return string
*/
public static function getRelatedPath($relativeTo, $path)
{
return self::normalizePath(dirname($relativeTo) . '/' . $path);
}
/**
* Left-trim same part of two paths
*
* @param string $pathOne
* @param string $pathTwo
* @return array
*/
private static function ltrimSamePart($pathOne, $pathTwo)
{
$one = explode('/', $pathOne);
$two = explode('/', $pathTwo);
while (isset($one[0]) && isset($two[0]) && $one[0] == $two[0]) {
array_shift($one);
array_shift($two);
}
return [implode('/', $one), implode('/', $two)];
}
}