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
|
|
|
private string $id = ''; |
14
|
|
|
private string $path = ''; |
15
|
|
|
private bool $valid = false; |
16
|
|
|
|
17
|
|
|
private bool $isUrl; |
18
|
|
|
private string $origPath; |
19
|
|
|
private string $url = ''; |
20
|
|
|
|
21
|
|
|
private AssetOption $options; |
22
|
|
|
private AttributeCollection $attributeCollection; |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param AssetType $type |
27
|
|
|
* @param string $path |
28
|
|
|
* @param array<string, bool|array> $options |
29
|
|
|
*/ |
30
|
|
|
public function __construct(private readonly AssetType $type, string $path, array $options = []) |
31
|
|
|
{ |
32
|
|
|
$this->origPath = $path; |
33
|
|
|
|
34
|
|
|
/** @psalm-suppress MixedArgumentTypeCoercion */ |
35
|
|
|
$this->attributeCollection = new AttributeCollection($options[AssetOption::ATTRIBUTES] ?? []); |
36
|
|
|
|
37
|
|
|
$this->options = new AssetOption($options); |
38
|
|
|
|
39
|
|
|
$this->isUrl = $this->checkIsUrl($path); |
40
|
|
|
$path = $this->getNormalizedPath($path); |
41
|
|
|
|
42
|
|
|
if ($path !== false) { |
43
|
|
|
$this->valid = true; |
44
|
|
|
$this->path = $path; |
45
|
|
|
$this->setId($this->path); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
private function getNormalizedPath(string $path): false|string |
50
|
|
|
{ |
51
|
|
|
if ($this->isUrl()) { |
52
|
|
|
return $this->url; |
53
|
54 |
|
} |
54
|
|
|
|
55
|
54 |
|
if (false === $projectDir = getenv('ASSETS_PROJECT_DIRECTORY')) { |
56
|
54 |
|
$projectDir = ''; |
57
|
54 |
|
} |
58
|
|
|
|
59
|
54 |
|
$paths = [ |
60
|
54 |
|
$path, |
61
|
|
|
$projectDir . $path |
62
|
54 |
|
]; |
63
|
|
|
|
64
|
|
|
foreach ($paths as $path) { |
65
|
|
|
if (false !== $normalizedPath = realpath($path)) { |
66
|
|
|
return $normalizedPath; |
67
|
|
|
} |
68
|
|
|
} |
69
|
54 |
|
return false; |
70
|
|
|
} |
71
|
54 |
|
|
72
|
31 |
|
private function checkIsUrl(string $path): bool |
73
|
|
|
{ |
74
|
|
|
if (str_starts_with($path, '//')) { |
75
|
28 |
|
$this->url = Helpers::getHttpScheme() . ':' . $path; |
76
|
|
|
return true; |
77
|
|
|
} |
78
|
28 |
|
|
79
|
28 |
|
if (in_array(strpos($path, '://'), [4, 5])) { |
80
|
28 |
|
$this->url = $path; |
81
|
28 |
|
return true; |
82
|
|
|
} |
83
|
28 |
|
|
84
|
28 |
|
if (str_starts_with($path, 'url:') || str_starts_with($path, 'local:')) { |
85
|
25 |
|
$this->url = str_replace(['url:', 'local:'], '', $path); |
86
|
|
|
return true; |
87
|
|
|
} |
88
|
6 |
|
|
89
|
|
|
return false; |
90
|
|
|
} |
91
|
54 |
|
|
92
|
|
|
public function getPath(): string |
93
|
54 |
|
{ |
94
|
18 |
|
return $this->path; |
95
|
18 |
|
} |
96
|
|
|
|
97
|
|
|
public function getType(): AssetType |
98
|
40 |
|
{ |
99
|
11 |
|
return $this->type; |
100
|
11 |
|
} |
101
|
|
|
|
102
|
|
|
public function isUrl(): bool |
103
|
31 |
|
{ |
104
|
3 |
|
return $this->isUrl; |
105
|
3 |
|
} |
106
|
|
|
|
107
|
|
|
public function getId(): string |
108
|
28 |
|
{ |
109
|
|
|
return $this->id; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function getOrigPath(): string |
113
|
|
|
{ |
114
|
44 |
|
return $this->origPath; |
115
|
|
|
} |
116
|
44 |
|
|
117
|
|
|
private function setId(string $path): void |
118
|
|
|
{ |
119
|
27 |
|
$this->id = md5($path . serialize($this->getOptions())); |
120
|
|
|
} |
121
|
27 |
|
|
122
|
|
|
public function getOptions(): AssetOption |
123
|
|
|
{ |
124
|
54 |
|
return $this->options; |
125
|
|
|
} |
126
|
54 |
|
|
127
|
|
|
public function getAttributeCollection(): AttributeCollection |
128
|
|
|
{ |
129
|
25 |
|
return $this->attributeCollection; |
130
|
|
|
} |
131
|
25 |
|
|
132
|
|
|
public function isValid(): bool |
133
|
|
|
{ |
134
|
7 |
|
return $this->valid; |
135
|
|
|
} |
136
|
7 |
|
|
137
|
|
|
} |
138
|
|
|
|