|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Enjoys\AssetsCollector; |
|
6
|
|
|
|
|
7
|
|
|
use function getenv; |
|
8
|
|
|
use function str_starts_with; |
|
9
|
|
|
|
|
10
|
|
|
class Asset |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @deprecated use AssetOption::MINIFY |
|
15
|
|
|
*/ |
|
16
|
|
|
public const MINIFY = 'minify'; |
|
17
|
|
|
/** |
|
18
|
|
|
* @deprecated use AssetOption::REPLACE_RELATIVE_URLS |
|
19
|
|
|
*/ |
|
20
|
|
|
public const REPLACE_RELATIVE_URLS = 'replaceRelativeUrls'; |
|
21
|
|
|
/** |
|
22
|
|
|
* @deprecated use AssetOption::SYMLINKS |
|
23
|
|
|
*/ |
|
24
|
|
|
public const CREATE_SYMLINK = 'symlinks'; |
|
25
|
|
|
/** |
|
26
|
|
|
* @deprecated use AssetOption::NOT_COLLECT |
|
27
|
|
|
*/ |
|
28
|
|
|
public const NOT_COLLECT = 'notCollect'; |
|
29
|
|
|
/** |
|
30
|
|
|
* @deprecated use AssetOption::ATTRIBUTES |
|
31
|
|
|
*/ |
|
32
|
|
|
public const ATTRIBUTES = 'attributes'; |
|
33
|
|
|
|
|
34
|
|
|
private ?string $id = null; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var string|false |
|
38
|
|
|
*/ |
|
39
|
|
|
private $path; |
|
40
|
|
|
private string $type; |
|
41
|
|
|
private bool $isUrl; |
|
42
|
|
|
private string $origPath; |
|
43
|
|
|
private string $url = ''; |
|
44
|
|
|
|
|
45
|
|
|
private AssetOption $options; |
|
46
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param string $type |
|
50
|
|
|
* @param string $path |
|
51
|
|
|
* @param array<string, string|bool|array|null> $options |
|
52
|
|
|
*/ |
|
53
|
54 |
|
public function __construct(string $type, string $path, array $options = []) |
|
54
|
|
|
{ |
|
55
|
54 |
|
$this->type = $type; |
|
56
|
54 |
|
$this->origPath = $path; |
|
57
|
54 |
|
$this->options = new AssetOption($options); |
|
58
|
|
|
|
|
59
|
54 |
|
$this->isUrl = $this->checkIsUrl($path); |
|
60
|
54 |
|
$this->path = $this->getNormalizedPath($path); |
|
61
|
|
|
|
|
62
|
54 |
|
$this->setId($this->path); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param string $path |
|
67
|
|
|
* @return false|string |
|
68
|
|
|
*/ |
|
69
|
54 |
|
private function getNormalizedPath(string $path) |
|
70
|
|
|
{ |
|
71
|
54 |
|
if ($this->isUrl()) { |
|
72
|
31 |
|
return $this->url; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
28 |
|
if (false === $projectDir = getenv('ASSETS_PROJECT_DIRECTORY')) { |
|
76
|
|
|
$projectDir = ''; |
|
77
|
|
|
} |
|
78
|
28 |
|
$paths = [ |
|
79
|
28 |
|
$path, |
|
80
|
28 |
|
$projectDir . $path |
|
81
|
28 |
|
]; |
|
82
|
|
|
|
|
83
|
28 |
|
foreach ($paths as $path) { |
|
84
|
28 |
|
if (false !== $normalizedPath = realpath($path)) { |
|
85
|
25 |
|
return $normalizedPath; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
6 |
|
return false; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
54 |
|
private function checkIsUrl(string $path): bool |
|
92
|
|
|
{ |
|
93
|
54 |
|
if (str_starts_with($path, '//')) { |
|
94
|
18 |
|
$this->url = Helpers::getHttpScheme() . ':' . $path; |
|
95
|
18 |
|
return true; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
40 |
|
if (in_array(strpos($path, '://'), [4, 5])) { |
|
99
|
11 |
|
$this->url = $path; |
|
100
|
11 |
|
return true; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
31 |
|
if (str_starts_with($path, 'url:') || str_starts_with($path, 'local:')) { |
|
104
|
3 |
|
$this->url = str_replace(['url:', 'local:'], '', $path); |
|
105
|
3 |
|
return true; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
28 |
|
return false; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @return false|string |
|
113
|
|
|
*/ |
|
114
|
44 |
|
public function getPath() |
|
115
|
|
|
{ |
|
116
|
44 |
|
return $this->path; |
|
|
|
|
|
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
27 |
|
public function getType(): string |
|
120
|
|
|
{ |
|
121
|
27 |
|
return $this->type; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
54 |
|
public function isUrl(): bool |
|
125
|
|
|
{ |
|
126
|
54 |
|
return $this->isUrl; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
24 |
|
public function getId(): ?string |
|
130
|
|
|
{ |
|
131
|
24 |
|
return $this->id; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
7 |
|
public function getOrigPath(): string |
|
135
|
|
|
{ |
|
136
|
7 |
|
return $this->origPath; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @param string|false $path |
|
141
|
|
|
*/ |
|
142
|
54 |
|
private function setId($path): void |
|
143
|
|
|
{ |
|
144
|
54 |
|
if ($path === false) { |
|
145
|
6 |
|
return; |
|
146
|
|
|
} |
|
147
|
51 |
|
$this->id = md5($path . serialize($this->getOptions())); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
53 |
|
public function getOptions(): AssetOption |
|
151
|
|
|
{ |
|
152
|
53 |
|
return $this->options; |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|