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