1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Enjoys\AssetsCollector\CollectStrategy\Strategy; |
4
|
|
|
|
5
|
|
|
use Enjoys\AssetsCollector\AssetOption; |
6
|
|
|
use Enjoys\AssetsCollector\CollectStrategy\StrategyAbstract; |
7
|
|
|
use Enjoys\AssetsCollector\Content\Reader; |
8
|
|
|
use Enjoys\AssetsCollector\Helpers; |
9
|
|
|
|
10
|
|
|
class ManyFilesStrategy extends StrategyAbstract |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @return array<string, array|null> |
17
|
|
|
* @throws \Exception |
18
|
|
|
*/ |
19
|
16 |
|
public function getResult(): array |
20
|
|
|
{ |
21
|
16 |
|
$cacheDir = $this->environment->getCompileDir().'/.cache'; |
22
|
|
|
|
23
|
16 |
|
$result = []; |
24
|
|
|
|
25
|
16 |
|
foreach ($this->assets as $asset) { |
26
|
14 |
|
if (false === $path = $asset->getPath()) { |
27
|
|
|
continue; |
28
|
|
|
} |
29
|
|
|
|
30
|
14 |
|
if ($asset->isUrl()) { |
31
|
7 |
|
$result[$path] = $asset->getOptions()->getAttributes(); |
32
|
7 |
|
continue; |
33
|
|
|
} |
34
|
|
|
|
35
|
9 |
|
$link = str_replace( |
36
|
9 |
|
[ |
37
|
9 |
|
$this->environment->getCompileDir(), |
38
|
9 |
|
$this->environment->getProjectDir() |
39
|
9 |
|
], |
40
|
9 |
|
'', |
41
|
9 |
|
$path |
42
|
9 |
|
); |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* TODO |
46
|
|
|
* @psalm-suppress PossiblyNullOperand |
47
|
|
|
*/ |
48
|
9 |
|
$cacheFile = $cacheDir.'/'.$asset->getId(); |
49
|
9 |
|
if(!file_exists($cacheFile) || (filemtime($cacheFile) + $this->environment->getCacheTime()) < time()){ |
50
|
9 |
|
(new Reader($asset, $this->environment, $this->logger))->replaceRelativeUrlsAndCreatedSymlinks(); |
51
|
9 |
|
Helpers::createEmptyFile($cacheFile, $this->logger); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
try { |
55
|
9 |
|
$asset->getOptions()->setOption( |
56
|
9 |
|
AssetOption::SYMLINKS, |
57
|
9 |
|
array_merge( |
58
|
9 |
|
[$this->environment->getCompileDir() . $link => $path], |
59
|
9 |
|
$asset->getOptions()->getSymlinks() |
60
|
9 |
|
) |
61
|
9 |
|
); |
62
|
|
|
|
63
|
9 |
|
foreach ($asset->getOptions()->getSymlinks() as $optLink => $optTarget) { |
64
|
9 |
|
Helpers::createSymlink($optLink, $optTarget, $this->logger); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
|
69
|
|
|
} catch (\Exception $e) { |
70
|
|
|
$this->logger->error($e->getMessage()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
9 |
|
$result[$this->environment->getBaseUrl() . str_replace( |
75
|
9 |
|
DIRECTORY_SEPARATOR, |
76
|
9 |
|
'/', |
77
|
9 |
|
$link |
78
|
9 |
|
)] = $asset->getOptions()->getAttributes(); |
79
|
|
|
} |
80
|
|
|
|
81
|
16 |
|
return $result; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|