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