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 TYPE_CSS = 'css'; |
14
|
|
|
public const TYPE_JS = 'js'; |
15
|
|
|
|
16
|
|
|
public const PARAM_MINIFY = 'minify'; |
17
|
|
|
private ?string $id = null; |
18
|
|
|
/** |
19
|
|
|
* @var false|string |
20
|
|
|
*/ |
21
|
|
|
private $path; |
22
|
|
|
private string $type; |
23
|
|
|
private bool $isUrl; |
24
|
|
|
private string $origPath; |
25
|
|
|
private bool $minify; |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Asset constructor. |
30
|
|
|
* @param string $type |
31
|
|
|
* @param string $path |
32
|
|
|
* @param array<mixed> $params |
33
|
|
|
*/ |
34
|
27 |
|
public function __construct(string $type, string $path, array $params = []) |
35
|
|
|
{ |
36
|
27 |
|
$this->setOptions($params); |
37
|
27 |
|
$this->type = $type; |
38
|
27 |
|
$this->origPath = $path; |
39
|
27 |
|
$this->path = $path; |
40
|
27 |
|
$this->minify = $this->getOption(self::PARAM_MINIFY, true); |
41
|
27 |
|
$this->isUrl = $this->checkIsUrl(); |
42
|
|
|
|
43
|
27 |
|
$this->normalizePath(); |
44
|
27 |
|
} |
45
|
|
|
|
46
|
27 |
|
private function normalizePath(): void |
47
|
|
|
{ |
48
|
27 |
|
if ($this->isUrl() && $this->path !== false) { |
49
|
18 |
|
$this->setId(); |
50
|
18 |
|
return; |
51
|
|
|
} |
52
|
|
|
|
53
|
12 |
|
if (false === $projectDir = \getenv('ASSETS_PROJECT_DIRECTORY')) { |
54
|
|
|
$projectDir = ''; |
55
|
|
|
} |
56
|
|
|
$paths = [ |
57
|
12 |
|
$this->path, |
58
|
12 |
|
$projectDir . $this->path |
59
|
|
|
]; |
60
|
|
|
|
61
|
12 |
|
foreach ($paths as $path) { |
62
|
12 |
|
if (false !== $this->path = realpath($path)) { |
|
|
|
|
63
|
9 |
|
$this->setId(); |
64
|
9 |
|
break; |
65
|
|
|
} |
66
|
|
|
} |
67
|
12 |
|
} |
68
|
|
|
|
69
|
27 |
|
private function checkIsUrl(): bool |
70
|
|
|
{ |
71
|
27 |
|
if (\str_starts_with($this->path, '//')) { |
|
|
|
|
72
|
15 |
|
$this->path = Helpers::getHttpScheme() . ':' . $this->path; |
73
|
15 |
|
return true; |
74
|
|
|
} |
75
|
|
|
|
76
|
14 |
|
if (in_array(strpos($this->path, '://'), [4, 5])) { |
|
|
|
|
77
|
4 |
|
return true; |
78
|
|
|
} |
79
|
12 |
|
return false; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
|
84
|
10 |
|
public function isMinify(): bool |
85
|
|
|
{ |
86
|
10 |
|
return $this->minify; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return false|string |
91
|
|
|
*/ |
92
|
25 |
|
public function getPath() |
93
|
|
|
{ |
94
|
25 |
|
return $this->path; |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
16 |
|
public function getType(): string |
98
|
|
|
{ |
99
|
16 |
|
return $this->type; |
100
|
|
|
} |
101
|
|
|
|
102
|
27 |
|
public function isUrl(): bool |
103
|
|
|
{ |
104
|
27 |
|
return $this->isUrl; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return string|null |
110
|
|
|
*/ |
111
|
14 |
|
public function getId(): ?string |
112
|
|
|
{ |
113
|
14 |
|
return $this->id; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
2 |
|
public function getOrigPath(): string |
120
|
|
|
{ |
121
|
2 |
|
return $this->origPath; |
122
|
|
|
} |
123
|
|
|
|
124
|
24 |
|
private function setId(): void |
125
|
|
|
{ |
126
|
24 |
|
$this->id = md5($this->path); |
|
|
|
|
127
|
24 |
|
} |
128
|
|
|
} |
129
|
|
|
|