/
AddWebPackAssetsListener.php
96 lines (80 loc) · 2.74 KB
/
AddWebPackAssetsListener.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
declare(strict_types=1);
/*
* This file is part of the Zikula package.
*
* Copyright Zikula - https://ziku.la/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Zikula\ThemeModule\EventListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupCollectionInterface;
use Zikula\ThemeModule\Engine\AssetBag;
class AddWebPackAssetsListener implements EventSubscriberInterface
{
/* @var AssetBag */
private $cssAssetBag;
/* @var AssetBag */
private $jsAssetBag;
/* @var EntrypointLookupCollectionInterface */
private $lookupCollection;
/* @var string */
private $entryPoint;
/* @var string */
private $entryName;
/* @var bool */
private $installed;
/* @var string */
private $projectDir;
public function __construct(
AssetBag $jsAssetBag,
AssetBag $cssAssetBag,
EntrypointLookupCollectionInterface $lookupCollection,
string $installed,
string $projectDir,
string $entryPoint = '_default',
string $entryName = 'app'
) {
$this->jsAssetBag = $jsAssetBag;
$this->cssAssetBag = $cssAssetBag;
$this->lookupCollection = $lookupCollection;
$this->entryPoint = $entryPoint;
$this->entryName = $entryName;
$this->installed = '0.0.0' !== $installed;
$this->projectDir = $projectDir;
}
public static function getSubscribedEvents()
{
return [
KernelEvents::RESPONSE => [
['addWebPackAssets', 1020]
]
];
}
public function addWebPackAssets(ResponseEvent $event): void
{
if (!$event->isMasterRequest()) {
return;
}
if (!$this->installed) {
return;
}
if (!file_exists($this->projectDir . '/public/build/manifest.json')) {
return;
}
$webPackCssFiles = $this->lookupCollection->getEntrypointLookup($this->entryPoint)
->getCssFiles($this->entryName);
$cssFiles = array_flip($webPackCssFiles);
array_walk($cssFiles, function (&$weight) { $weight += AssetBag::WEIGHT_WEBPACK_OFFSET; });
$this->cssAssetBag->add($cssFiles);
$webPackJsFiles = $this->lookupCollection->getEntrypointLookup($this->entryPoint)
->getJavaScriptFiles($this->entryName);
$jsFiles = array_flip($webPackJsFiles);
array_walk($jsFiles, function (&$weight) { $weight += AssetBag::WEIGHT_WEBPACK_OFFSET; });
$this->jsAssetBag->add($jsFiles);
}
}