|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Enjoys\AssetsCollector\CollectStrategy\Strategy; |
|
4
|
|
|
|
|
5
|
|
|
use Enjoys\AssetsCollector\Asset; |
|
6
|
|
|
use Enjoys\AssetsCollector\Assets; |
|
7
|
|
|
use Enjoys\AssetsCollector\CollectStrategy\StrategyAbstract; |
|
8
|
|
|
use Enjoys\AssetsCollector\Content\Reader; |
|
9
|
|
|
use Enjoys\AssetsCollector\Environment; |
|
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
|
|
|
* @param string $namespace |
|
24
|
|
|
* @throws \Exception |
|
25
|
|
|
*/ |
|
26
|
2 |
|
public function __construct( |
|
27
|
|
|
Environment $environment, |
|
28
|
|
|
array $assetsCollection, |
|
29
|
|
|
string $type, |
|
30
|
|
|
string $namespace = Assets::NAMESPACE_COMMON |
|
31
|
|
|
) { |
|
32
|
2 |
|
parent::__construct($environment, $assetsCollection, $type, $namespace); |
|
33
|
|
|
|
|
34
|
2 |
|
$this->cacheTime = $environment->getCacheTime(); |
|
35
|
|
|
|
|
36
|
2 |
|
$filename = $this->generateFilename( |
|
37
|
2 |
|
$environment->getPageId(), |
|
38
|
|
|
$type |
|
39
|
|
|
); |
|
40
|
|
|
|
|
41
|
2 |
|
$this->filePath = $environment->getCompileDir() . DIRECTORY_SEPARATOR . $filename; |
|
42
|
2 |
|
$this->fileUrl = $environment->getBaseUrl() . DIRECTORY_SEPARATOR . $filename; |
|
43
|
2 |
|
$this->init(); |
|
44
|
2 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param string|null $pageId |
|
48
|
|
|
* @param string $type css|js |
|
49
|
|
|
* @return string |
|
50
|
|
|
*/ |
|
51
|
2 |
|
private function generateFilename(?string $pageId, string $type): string |
|
52
|
|
|
{ |
|
53
|
2 |
|
$type = \strtolower($type); |
|
54
|
2 |
|
if ($pageId === null) { |
|
55
|
2 |
|
$pageId = ''; |
|
56
|
2 |
|
if (isset($_SERVER['REQUEST_URI'])) { |
|
57
|
|
|
$pageId = $_SERVER['REQUEST_URI']; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
2 |
|
return '_' . $type . DIRECTORY_SEPARATOR . md5($this->getNamespace() . $pageId) . '.' . $type; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @throws \Exception |
|
65
|
|
|
*/ |
|
66
|
2 |
|
private function init(): void |
|
67
|
|
|
{ |
|
68
|
2 |
|
$this->createDirectory(pathinfo($this->filePath, PATHINFO_DIRNAME)); |
|
|
|
|
|
|
69
|
2 |
|
if (!file_exists($this->filePath)) { |
|
70
|
2 |
|
$this->writeFile($this->filePath, ''); |
|
71
|
2 |
|
$this->fileCreated = true; |
|
72
|
2 |
|
$this->logger->info(sprintf('Create new file %s', $this->filePath)); |
|
73
|
|
|
} |
|
74
|
2 |
|
} |
|
75
|
|
|
|
|
76
|
1 |
|
private function isCacheValid(): bool |
|
77
|
|
|
{ |
|
78
|
1 |
|
if ($this->fileCreated) { |
|
79
|
1 |
|
return false; |
|
80
|
|
|
} |
|
81
|
|
|
return (filemtime($this->filePath) + $this->cacheTime) > time(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @return array<string> |
|
86
|
|
|
*/ |
|
87
|
1 |
|
public function getResult(): array |
|
88
|
|
|
{ |
|
89
|
|
|
try { |
|
90
|
1 |
|
if ($this->isCacheValid()) { |
|
91
|
|
|
$this->logger->info(sprintf('Use cached file: %s', $this->filePath)); |
|
92
|
|
|
$this->logger->info(sprintf('Return url: %s', $this->fileUrl)); |
|
93
|
|
|
return [$this->fileUrl]; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
1 |
|
$output = ''; |
|
97
|
|
|
|
|
98
|
1 |
|
foreach ($this->assetsCollection as $asset) { |
|
99
|
1 |
|
$output .= (new Reader($asset, $this->logger))->getContents(); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
1 |
|
$this->writeFile($this->filePath, $output); |
|
103
|
1 |
|
$this->logger->info(sprintf('Write to: %s', $this->filePath)); |
|
104
|
|
|
} catch (\Exception $e) { |
|
105
|
|
|
$this->logger->notice($e->getMessage()); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
1 |
|
$this->logger->info(sprintf('Return url: %s', $this->fileUrl)); |
|
109
|
1 |
|
return [$this->fileUrl]; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @return string |
|
114
|
|
|
*/ |
|
115
|
|
|
public function getFileUrl(): string |
|
116
|
|
|
{ |
|
117
|
|
|
return $this->fileUrl; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @return string |
|
122
|
|
|
*/ |
|
123
|
1 |
|
public function getFilePath(): string |
|
124
|
|
|
{ |
|
125
|
1 |
|
return $this->filePath; |
|
126
|
|
|
} |
|
127
|
|
|
} |