1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Enjoys\AssetsCollector; |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
use Enjoys\Traits\Options; |
9
|
|
|
|
10
|
|
|
class Asset |
11
|
|
|
{ |
12
|
|
|
use Options; |
13
|
|
|
|
14
|
|
|
public const TYPE_CSS = 'css'; |
15
|
|
|
public const TYPE_JS = 'js'; |
16
|
|
|
|
17
|
|
|
public const PARAM_MINIFY = 'minify'; |
18
|
|
|
private ?string $id = null; |
19
|
|
|
/** |
20
|
|
|
* @var false|string |
21
|
|
|
*/ |
22
|
|
|
private $path; |
23
|
|
|
private string $type; |
24
|
|
|
private bool $isUrl; |
25
|
|
|
private string $origPath; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Asset constructor. |
29
|
|
|
* @param string $type |
30
|
|
|
* @param string $path |
31
|
|
|
* @param array<mixed> $params |
32
|
|
|
*/ |
33
|
27 |
|
public function __construct(string $type, string $path, array $params = []) |
34
|
|
|
{ |
35
|
27 |
|
$this->setOptions($params); |
36
|
27 |
|
$this->type = $type; |
37
|
27 |
|
$this->origPath = $path; |
38
|
27 |
|
$this->path = $path; |
39
|
27 |
|
$this->isUrl = $this->checkIsUrl(); |
40
|
|
|
|
41
|
27 |
|
$this->normalizePath(); |
42
|
27 |
|
} |
43
|
|
|
|
44
|
27 |
|
private function normalizePath(): void |
45
|
|
|
{ |
46
|
27 |
|
if ($this->isUrl() && $this->path !== false) { |
47
|
18 |
|
$this->setId(); |
48
|
18 |
|
return; |
49
|
|
|
} |
50
|
|
|
|
51
|
12 |
|
$projectDir = ''; |
52
|
12 |
|
if (isset($_ENV['ASSETS_PROJECT_DIRECTORY'])) { |
53
|
|
|
$projectDir = $_ENV['ASSETS_PROJECT_DIRECTORY'] . '/'; |
54
|
|
|
} |
55
|
|
|
$paths = [ |
56
|
12 |
|
$this->path, |
57
|
12 |
|
$projectDir . $this->path |
58
|
|
|
]; |
59
|
|
|
|
60
|
12 |
|
foreach ($paths as $path) { |
61
|
12 |
|
if (false !== $this->path = realpath($path)) { |
|
|
|
|
62
|
9 |
|
$this->setId(); |
63
|
9 |
|
break; |
64
|
|
|
} |
65
|
|
|
} |
66
|
12 |
|
} |
67
|
|
|
|
68
|
27 |
|
private function checkIsUrl(): bool |
69
|
|
|
{ |
70
|
27 |
|
if (\str_starts_with($this->path, '//')) { |
|
|
|
|
71
|
15 |
|
$this->path = $this->defineHttpScheme() . ':' . $this->path; |
72
|
15 |
|
return true; |
73
|
|
|
} |
74
|
|
|
|
75
|
14 |
|
if (in_array(strpos($this->path, '://'), [4, 5])) { |
|
|
|
|
76
|
4 |
|
return true; |
77
|
|
|
} |
78
|
12 |
|
return false; |
79
|
|
|
} |
80
|
|
|
|
81
|
15 |
|
private function defineHttpScheme(): string |
82
|
|
|
{ |
83
|
15 |
|
$scheme = 'http'; |
84
|
15 |
|
if (isset($_SERVER['HTTP_SCHEME'])) { |
85
|
1 |
|
return $_SERVER['HTTP_SCHEME']; |
86
|
|
|
} |
87
|
|
|
|
88
|
14 |
|
if (isset($_SERVER['HTTPS']) && \strtolower($_SERVER['HTTPS']) != 'off') { |
89
|
1 |
|
return 'https'; |
90
|
|
|
} |
91
|
|
|
|
92
|
13 |
|
if (isset($_SERVER['SERVER_PORT']) && 443 == $_SERVER['SERVER_PORT']) { |
93
|
1 |
|
return 'https'; |
94
|
|
|
} |
95
|
12 |
|
return $scheme; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
|
99
|
10 |
|
public function isMinify(): bool |
100
|
|
|
{ |
101
|
10 |
|
return $this->getOption(self::PARAM_MINIFY, true); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return false|string |
106
|
|
|
*/ |
107
|
25 |
|
public function getPath() |
108
|
|
|
{ |
109
|
25 |
|
return $this->path; |
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
112
|
16 |
|
public function getType(): string |
113
|
|
|
{ |
114
|
16 |
|
return $this->type; |
115
|
|
|
} |
116
|
|
|
|
117
|
27 |
|
public function isUrl(): bool |
118
|
|
|
{ |
119
|
27 |
|
return $this->isUrl; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return string|null |
125
|
|
|
*/ |
126
|
14 |
|
public function getId(): ?string |
127
|
|
|
{ |
128
|
14 |
|
return $this->id; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
2 |
|
public function getOrigPath(): string |
135
|
|
|
{ |
136
|
2 |
|
return $this->origPath; |
137
|
|
|
} |
138
|
|
|
|
139
|
24 |
|
private function setId(): void |
140
|
|
|
{ |
141
|
24 |
|
$this->id = md5($this->path); |
|
|
|
|
142
|
24 |
|
} |
143
|
|
|
|
144
|
|
|
|
145
|
|
|
} |