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\Helpers; |
11
|
|
|
use Enjoys\AssetsCollector\Strategy; |
12
|
|
|
use Exception; |
13
|
|
|
|
14
|
|
|
use function Enjoys\FileSystem\createFile; |
15
|
|
|
use function Enjoys\FileSystem\makeSymlink; |
16
|
|
|
|
17
|
|
|
class ManyFilesStrategy implements Strategy |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @inheritdoc |
23
|
|
|
* @throws Exception |
24
|
|
|
* @psalm-suppress PossiblyFalseOperand |
25
|
|
|
*/ |
26
|
|
|
public function getAssets(AssetType $type, array $assetsCollection, Environment $environment): array |
27
|
|
|
{ |
28
|
|
|
$cacheDir = $environment->getCompileDir() . '/.cache'; |
29
|
|
|
|
30
|
|
|
$logger = $environment->getLogger(); |
31
|
|
|
|
32
|
|
|
foreach ($assetsCollection as $asset) { |
33
|
|
|
if (!$asset->isValid()) { |
34
|
|
|
continue; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$assetAttributeCollection = $asset->getAttributeCollection(); |
38
|
|
|
|
39
|
|
|
if ($asset->isUrl()) { |
40
|
|
|
$assetAttributeCollection->set( |
41
|
|
|
$type->htmlAttribute(), |
42
|
|
|
Helpers::addVersionToPath($asset->getPath(), $environment->getVersionQuery()) |
43
|
|
|
); |
44
|
|
|
continue; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$link = str_replace( |
48
|
|
|
[ |
49
|
|
|
$environment->getCompileDir(), |
50
|
|
|
$environment->getProjectDir() |
51
|
|
|
], |
52
|
|
|
'', |
53
|
|
|
$asset->getPath() |
54
|
|
|
); |
55
|
|
|
|
56
|
|
|
$cacheFile = $cacheDir . '/' . $asset->getId(); |
57
|
|
|
|
58
|
|
|
if (!file_exists($cacheFile) || (filemtime($cacheFile) + $environment->getCacheTime()) < time()) { |
59
|
|
|
// feel AssetOption::SYMLINKS |
60
|
|
|
(new Reader($asset, $environment))->replaceRelativeUrls(); |
61
|
|
|
createFile($cacheFile); |
62
|
|
|
$logger->info(sprintf('Create file: %s', $cacheFile)); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
try { |
66
|
|
|
/** @infection-ignore-all */ |
67
|
|
|
$asset->getOptions()->setOption( |
68
|
|
|
AssetOption::SYMLINKS, |
69
|
|
|
array_merge( |
70
|
|
|
[$environment->getCompileDir() . $link => $asset->getPath()], |
71
|
|
|
$asset->getOptions()->getSymlinks() |
72
|
|
|
) |
73
|
|
|
); |
74
|
|
|
|
75
|
|
|
foreach ($asset->getOptions()->getSymlinks() as $optLink => $optTarget) { |
76
|
|
|
if (makeSymlink($optLink, $optTarget)) { |
77
|
|
|
$logger->info(sprintf('Created symlink: %s', $optLink)); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} catch (Exception $e) { |
81
|
|
|
$logger->error($e->getMessage()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** @infection-ignore-all */ |
85
|
|
|
$assetAttributeCollection->set( |
86
|
|
|
$type->htmlAttribute(), |
87
|
|
|
Helpers::addVersionToPath( |
88
|
|
|
$environment->getBaseUrl() . str_replace( |
89
|
|
|
DIRECTORY_SEPARATOR, |
90
|
|
|
'/', |
91
|
|
|
$link |
92
|
|
|
), |
93
|
|
|
$environment->getVersionQuery() |
94
|
|
|
) |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return array_filter($assetsCollection, function (Asset $asset) { |
99
|
|
|
return $asset->isValid(); |
100
|
|
|
}); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
} |
105
|
|
|
|