|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Enjoys\AssetsCollector\Strategy; |
|
4
|
|
|
|
|
5
|
|
|
use Enjoys\AssetsCollector\Asset; |
|
6
|
|
|
use Enjoys\AssetsCollector\AssetOption; |
|
7
|
|
|
use Enjoys\AssetsCollector\AssetType; |
|
8
|
|
|
use Enjoys\AssetsCollector\Content\Reader; |
|
9
|
|
|
use Enjoys\AssetsCollector\Environment; |
|
10
|
|
|
use Enjoys\AssetsCollector\Strategy; |
|
11
|
|
|
use Exception; |
|
12
|
|
|
|
|
13
|
|
|
use function Enjoys\FileSystem\createFile; |
|
14
|
|
|
use function Enjoys\FileSystem\makeSymlink; |
|
15
|
|
|
use function Enjoys\FileSystem\writeFile; |
|
16
|
|
|
|
|
17
|
|
|
class OneFileStrategy implements Strategy |
|
18
|
|
|
{ |
|
19
|
|
|
private int $cacheTime = 0; |
|
20
|
|
|
private string $filename = ''; |
|
21
|
|
|
private string $filePath = ''; |
|
22
|
|
|
private string $fileUrl = ''; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param Asset[] $assets |
|
26
|
|
|
* @return string |
|
27
|
|
|
*/ |
|
28
|
|
|
private function generateHashId(array $assets): string |
|
29
|
|
|
{ |
|
30
|
|
|
|
|
31
|
|
|
$assetsIds = array_map(fn($i) => $i->getId(), $assets); |
|
32
|
|
|
sort($assetsIds); |
|
33
|
|
|
return md5(implode('', $assetsIds)); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @psalm-suppress PossiblyFalseOperand |
|
39
|
|
|
*/ |
|
40
|
|
|
private function isCacheValid(): bool |
|
41
|
|
|
{ |
|
42
|
|
|
if (file_exists($this->filePath)){ |
|
43
|
|
|
return (filemtime($this->filePath) + $this->cacheTime) > time(); |
|
44
|
|
|
} |
|
45
|
|
|
return false; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @inheritdoc |
|
51
|
|
|
* @throws Exception |
|
52
|
|
|
*/ |
|
53
|
|
|
public function getAssets(AssetType $type, array $assetsCollection, Environment $environment): array |
|
54
|
|
|
{ |
|
55
|
|
|
$this->cacheTime = $environment->getCacheTime(); |
|
56
|
|
|
|
|
57
|
|
|
/** @infection-ignore-all */ |
|
58
|
|
|
$this->filename = '_' . $type->value . DIRECTORY_SEPARATOR . $this->generateHashId( |
|
59
|
|
|
$assetsCollection |
|
60
|
|
|
) . '.' . $type->value; |
|
61
|
|
|
|
|
62
|
|
|
/** @infection-ignore-all */ |
|
63
|
|
|
$this->filePath = $environment->getCompileDir() . DIRECTORY_SEPARATOR . $this->filename; |
|
64
|
|
|
|
|
65
|
|
|
/** @infection-ignore-all */ |
|
66
|
|
|
$this->fileUrl = $environment->getBaseUrl() . '/' . str_replace(DIRECTORY_SEPARATOR, '/', $this->filename); |
|
67
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
$logger = $environment->getLogger(); |
|
70
|
|
|
$notCollectAssets = array_filter($assetsCollection, function ($asset) { |
|
71
|
|
|
return $asset->getOptions()->isNotCollect(); |
|
72
|
|
|
}); |
|
73
|
|
|
|
|
74
|
|
|
$collectAssets = array_filter($assetsCollection, function ($asset) { |
|
75
|
|
|
return !$asset->getOptions()->isNotCollect(); |
|
76
|
|
|
}); |
|
77
|
|
|
|
|
78
|
|
|
$notCollectedResult = (new ManyFilesStrategy())->getAssets($type, $notCollectAssets, $environment); |
|
79
|
|
|
|
|
80
|
|
|
$result = array_merge([ |
|
81
|
|
|
new Asset($type, $this->fileUrl, [ |
|
82
|
|
|
AssetOption::ATTRIBUTES => [ |
|
83
|
|
|
$type->htmlAttribute() => $this->fileUrl |
|
84
|
|
|
] |
|
85
|
|
|
]) |
|
86
|
|
|
], $notCollectedResult); |
|
87
|
|
|
|
|
88
|
|
|
try { |
|
89
|
|
|
if ($this->isCacheValid()) { |
|
90
|
|
|
$logger->info(sprintf('Use cached file: %s', $this->filePath)); |
|
91
|
|
|
$logger->info(sprintf('Return url: %s', $this->fileUrl)); |
|
92
|
|
|
return $result; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
foreach ($collectAssets as $asset) { |
|
96
|
|
|
|
|
97
|
|
|
writeFile( |
|
98
|
|
|
$this->filePath, |
|
99
|
|
|
(new Reader($asset, $environment))->replaceRelativeUrls()->minify()->getContents(), |
|
100
|
|
|
'a' |
|
101
|
|
|
); |
|
102
|
|
|
|
|
103
|
|
|
foreach ($asset->getOptions()->getSymlinks() as $optLink => $optTarget) { |
|
104
|
|
|
if (makeSymlink($optLink, $optTarget)) { |
|
105
|
|
|
$logger->info(sprintf('Created symlink: %s', $optLink)); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
$logger->info(sprintf('Write to: %s', $this->filePath)); |
|
111
|
|
|
} catch (Exception $e) { |
|
112
|
|
|
$logger->notice($e->getMessage()); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$logger->info(sprintf('Return url: %s', $this->fileUrl)); |
|
116
|
|
|
|
|
117
|
|
|
return $result; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function getFilename(): string |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->filename; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function getFilePath(): string |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->filePath; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function getFileUrl(): string |
|
131
|
|
|
{ |
|
132
|
|
|
return $this->fileUrl; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|