Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
asset.php
1<?php
2
4
9
15abstract class Asset
16{
17 const JS = 'js';
18 const CSS = 'css';
19 const LAYOUT = 'layout';
20 const LANG = 'lang';
21
23 protected $type;
25 protected $path;
27 protected $uri;
29 protected $content;
30
38 public static function create($path)
39 {
41 switch ($type)
42 {
43 case self::LANG:
44 return new LangAsset($path);
45 case self::JS:
46 return new JsAsset($path);
47 case self::CSS:
48 return new CssAsset($path);
49 case self::LAYOUT:
50 return new LayoutAsset($path);
51 default:
52 throw new ArgumentException("Unknown type `$type`.");
53 }
54 }
55
61 public static function getTypeList()
62 {
64 }
65
72 public static function detectType($path)
73 {
74 $type = mb_strtolower(mb_substr(strrchr($path, '.'), 1));
75 switch ($type)
76 {
77 case 'js':
78 return self::JS;
79 case 'css':
80 return self::CSS;
81 case 'htm':
82 case 'html':
83 return self::LAYOUT;
84 case 'php':
85 return self::LANG;
86 default:
87 return null;
88 }
89 }
90
96 public function __construct($path = null)
97 {
98 if ($path)
99 {
100 $this->setPath($path);
101 }
102 }
103
109 public function getType()
110 {
111 return $this->type;
112 }
113
119 public function getPath()
120 {
121 return $this->path;
122 }
123
130 public function setPath($path)
131 {
132 if (!static::isExists($path))
133 {
134 throw new ArgumentException("Can not locate file by path `{$path}`.");
135 }
136
137 if (self::detectType($path) != $this->getType())
138 {
139 throw new ArgumentException("Path has wrong ext of `$path` for Asset with type `{$this->getType()}`.");
140 }
141
142 $this->path = $path;
143
144 return $this;
145 }
146
152 public function getUri()
153 {
154 if ($this->uri)
155 {
156 return $this->uri;
157 }
158
159 return $this->path ?
160 WebPacker\Builder::getDefaultSiteUri()
161 . $this->path
162 . '?' . filemtime(self::getAbsolutePath($this->path))
163 . '.' . filesize(self::getAbsolutePath($this->path))
164 :
165 null;
166 }
167
174 public function setUri($uri)
175 {
176 $this->uri = $uri;
177 return $this;
178 }
179
186 public function setContent($content)
187 {
188 $this->content = $content;
189 return $this;
190 }
191
197 public function getName()
198 {
199 return $this->path ? basename($this->path) : null;
200 }
201
207 public function getContent()
208 {
209 if ($this->content)
210 {
211 return $this->content;
212 }
213
214 if (!static::isExists($this->path))
215 {
216 return null;
217 }
218
219 $path = self::getAbsolutePath($this->path);
220
221 $mapPath = null;
222 $useMinimized = false;
223 $minPathPos = mb_strrpos($path, '.');
224 if ($minPathPos !== false)
225 {
226 $minPath = mb_substr($path, 0, $minPathPos)
227 .'.min'
228 .mb_substr($path, $minPathPos);
229
230 if (IO\File::isFileExists($minPath))
231 {
232 $path = $minPath;
233 $useMinimized = true;
234
235 $minPathPos = mb_strrpos($this->path, '.');
236 $mapPath = mb_substr($this->path, 0, $minPathPos)
237 .'.map'
238 .mb_substr($this->path, $minPathPos);
239 }
240 }
241
242 $content = IO\File::getFileContents($path) ?: '';
243 if (!$content || !$useMinimized || !$mapPath)
244 {
245 return $content;
246 }
247
248 $parts = explode("\n", $content);
249 $mapUri = '//# sourceMappingURL=';
250 if (mb_strpos(array_pop($parts), $mapUri) !== 0)
251 {
252 return $content;
253 }
254
255 $mapUri .= WebPacker\Builder::getDefaultSiteUri() . $mapPath;
256 array_push($parts, $mapUri);
257
258 return implode("\n", $parts);
259
260 }
261
268 public static function isExists($path)
269 {
270 return $path ? IO\File::isFileExists(self::getAbsolutePath($path)) : false;
271 }
272
279 protected static function getAbsolutePath($path)
280 {
281 return $path ? Application::getDocumentRoot() . $path : null;
282 }
283}
static isFileExists($path)
Definition file.php:256