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