1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Enjoys\AssetsCollector\CollectStrategy\Strategy; |
4
|
|
|
|
5
|
|
|
use Enjoys\AssetsCollector\Asset; |
6
|
|
|
use Enjoys\AssetsCollector\CollectStrategy\StrategyAbstract; |
7
|
|
|
use Enjoys\AssetsCollector\Content\Reader; |
8
|
|
|
use Enjoys\AssetsCollector\Environment; |
9
|
|
|
use Enjoys\AssetsCollector\Helpers; |
10
|
|
|
|
11
|
|
|
class OneFileStrategy extends StrategyAbstract |
12
|
|
|
{ |
13
|
|
|
private int $cacheTime; |
14
|
|
|
private string $filePath; |
15
|
|
|
private string $fileUrl; |
16
|
|
|
private bool $fileCreated = false; |
17
|
|
|
/** |
18
|
|
|
* @var array{css: array<mixed>, js: array<mixed>} |
19
|
|
|
*/ |
20
|
|
|
private array $minifyOptions; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Build constructor. |
24
|
|
|
* @param Environment $environment |
25
|
|
|
* @param array<Asset> $assetsCollection |
26
|
|
|
* @param string $type |
27
|
|
|
* @throws \Exception |
28
|
|
|
*/ |
29
|
2 |
|
public function __construct( |
30
|
|
|
Environment $environment, |
31
|
|
|
array $assetsCollection, |
32
|
|
|
string $type |
33
|
|
|
) { |
34
|
2 |
|
parent::__construct($environment, $assetsCollection, $type); |
35
|
|
|
|
36
|
2 |
|
$this->cacheTime = $environment->getCacheTime(); |
37
|
2 |
|
$this->minifyOptions = $environment->getMinifyOptions(); |
38
|
|
|
|
39
|
2 |
|
$filename = $this->generateFilename($type); |
40
|
|
|
|
41
|
2 |
|
$this->filePath = $environment->getCompileDir() . DIRECTORY_SEPARATOR . $filename; |
42
|
2 |
|
$this->fileUrl = $environment->getBaseUrl() . '/' . str_replace(DIRECTORY_SEPARATOR, '/', $filename); |
43
|
2 |
|
$this->init(); |
44
|
2 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $type css|js |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
2 |
|
private function generateFilename(string $type): string |
51
|
|
|
{ |
52
|
2 |
|
return '_' . $type . DIRECTORY_SEPARATOR . md5(serialize($this->assetsCollection)) . '.' . $type; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @throws \Exception |
57
|
|
|
*/ |
58
|
2 |
|
private function init(): void |
59
|
|
|
{ |
60
|
2 |
|
Helpers::createDirectory(pathinfo($this->filePath, PATHINFO_DIRNAME), 0755, $this->logger); |
|
|
|
|
61
|
|
|
|
62
|
2 |
|
if (!file_exists($this->filePath)) { |
63
|
2 |
|
Helpers::createEmptyFile($this->filePath, $this->logger); |
64
|
2 |
|
$this->fileCreated = true; |
65
|
|
|
} |
66
|
2 |
|
} |
67
|
|
|
|
68
|
1 |
|
private function isCacheValid(): bool |
69
|
|
|
{ |
70
|
1 |
|
if ($this->fileCreated) { |
71
|
1 |
|
return false; |
72
|
|
|
} |
73
|
|
|
return (filemtime($this->filePath) + $this->cacheTime) > time(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return array<string> |
78
|
|
|
*/ |
79
|
1 |
|
public function getResult(): array |
80
|
|
|
{ |
81
|
|
|
try { |
82
|
1 |
|
if ($this->isCacheValid()) { |
83
|
|
|
$this->logger->info(sprintf('Use cached file: %s', $this->filePath)); |
84
|
|
|
$this->logger->info(sprintf('Return url: %s', $this->fileUrl)); |
85
|
|
|
return [$this->fileUrl]; |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
$output = ''; |
89
|
|
|
|
90
|
1 |
|
foreach ($this->assetsCollection as $asset) { |
91
|
1 |
|
$output .= (new Reader($asset, $this->minifyOptions, $this->logger))->getContents(); |
92
|
|
|
|
93
|
1 |
|
$optSymlinks = (array)$asset->getOption(Asset::CREATE_SYMLINK, []); |
94
|
1 |
|
foreach ($optSymlinks as $optLink => $optTarget) { |
95
|
|
|
Helpers::createSymlink($optLink, $optTarget, $this->logger); |
96
|
|
|
} |
97
|
|
|
} |
98
|
1 |
|
Helpers::writeFile($this->filePath, $output, 'w', $this->logger); |
99
|
|
|
} catch (\Exception $e) { |
100
|
|
|
$this->logger->notice($e->getMessage()); |
101
|
|
|
} |
102
|
|
|
|
103
|
1 |
|
$this->logger->info(sprintf('Return url: %s', $this->fileUrl)); |
104
|
1 |
|
return [$this->fileUrl]; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
|
|
public function getFileUrl(): string |
111
|
|
|
{ |
112
|
|
|
return $this->fileUrl; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
1 |
|
public function getFilePath(): string |
119
|
|
|
{ |
120
|
1 |
|
return $this->filePath; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|