1C-Bitrix
25.700.0
Загрузка...
Поиск...
Не найдено
asset.php
См. документацию.
1
<?php
2
3
namespace
Bitrix\Main\Web\WebPacker\Resource
;
4
5
use
Bitrix\Main\Application
;
6
use
Bitrix\Main\ArgumentException
;
7
use
Bitrix\Main\IO
;
8
use
Bitrix\Main\Web\WebPacker
;
9
15
abstract
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
{
40
$type
=
self::detectType
(
$path
);
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
{
63
return
[self::JS, self::CSS, self::LAYOUT, self::LANG];
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
(!str_starts_with(array_pop($parts), $mapUri))
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
}
Bitrix\Main\Application
Определения
application.php:30
Bitrix\Main\Application\getDocumentRoot
static getDocumentRoot()
Определения
application.php:736
Bitrix\Main\ArgumentException
Определения
ArgumentException.php:9
Bitrix\Main\IO\File\isFileExists
static isFileExists($path)
Определения
file.php:249
Bitrix\Main\IO\File\getFileContents
static getFileContents($path)
Определения
file.php:255
Bitrix\Main\Web\WebPacker\Builder\getDefaultSiteUri
static getDefaultSiteUri()
Определения
builder.php:288
Bitrix\Main\Web\WebPacker\Resource\Asset
Определения
asset.php:16
Bitrix\Main\Web\WebPacker\Resource\Asset\setContent
setContent($content)
Определения
asset.php:186
Bitrix\Main\Web\WebPacker\Resource\Asset\$path
$path
Определения
asset.php:25
Bitrix\Main\Web\WebPacker\Resource\Asset\getTypeList
static getTypeList()
Определения
asset.php:61
Bitrix\Main\Web\WebPacker\Resource\Asset\__construct
__construct($path=null)
Определения
asset.php:96
Bitrix\Main\Web\WebPacker\Resource\Asset\getUri
getUri()
Определения
asset.php:152
Bitrix\Main\Web\WebPacker\Resource\Asset\getPath
getPath()
Определения
asset.php:119
Bitrix\Main\Web\WebPacker\Resource\Asset\getName
getName()
Определения
asset.php:197
Bitrix\Main\Web\WebPacker\Resource\Asset\isExists
static isExists($path)
Определения
asset.php:268
Bitrix\Main\Web\WebPacker\Resource\Asset\$content
$content
Определения
asset.php:29
Bitrix\Main\Web\WebPacker\Resource\Asset\getContent
getContent()
Определения
asset.php:207
Bitrix\Main\Web\WebPacker\Resource\Asset\setPath
setPath($path)
Определения
asset.php:130
Bitrix\Main\Web\WebPacker\Resource\Asset\LANG
const LANG
Определения
asset.php:20
Bitrix\Main\Web\WebPacker\Resource\Asset\$uri
$uri
Определения
asset.php:27
Bitrix\Main\Web\WebPacker\Resource\Asset\JS
const JS
Определения
asset.php:17
Bitrix\Main\Web\WebPacker\Resource\Asset\getAbsolutePath
static getAbsolutePath($path)
Определения
asset.php:279
Bitrix\Main\Web\WebPacker\Resource\Asset\getType
getType()
Определения
asset.php:109
Bitrix\Main\Web\WebPacker\Resource\Asset\$type
$type
Определения
asset.php:23
Bitrix\Main\Web\WebPacker\Resource\Asset\create
static create($path)
Определения
asset.php:38
Bitrix\Main\Web\WebPacker\Resource\Asset\LAYOUT
const LAYOUT
Определения
asset.php:19
Bitrix\Main\Web\WebPacker\Resource\Asset\detectType
static detectType($path)
Определения
asset.php:72
Bitrix\Main\Web\WebPacker\Resource\Asset\setUri
setUri($uri)
Определения
asset.php:174
Bitrix\Main\Web\WebPacker\Resource\Asset\CSS
const CSS
Определения
asset.php:18
Bitrix\Main\Web\WebPacker\Resource\CssAsset
Определения
cssasset.php:11
Bitrix\Main\Web\WebPacker\Resource\JsAsset
Определения
jsasset.php:11
Bitrix\Main\Web\WebPacker\Resource\LangAsset
Определения
langasset.php:14
Bitrix\Main\Web\WebPacker\Resource\LayoutAsset
Определения
layoutasset.php:11
Bitrix\Main\IO
Определения
directory.php:3
Bitrix\Main\Web\WebPacker\Resource
Определения
asset.php:3
Bitrix\Main\Web\WebPacker
Определения
builder.php:3
path
path
Определения
template_copy.php:201
bitrix
modules
main
lib
web
webpacker
resource
asset.php
Создано системой
1.14.0