Skip to content

Commit badc55d

Browse files
-
1 parent 243c87f commit badc55d

File tree

4 files changed

+66
-98
lines changed

4 files changed

+66
-98
lines changed

src/Asset/EntrypointLookup.php

Lines changed: 20 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ class EntrypointLookup implements EntrypointLookupInterface
2929

3030
private $cache;
3131

32-
public function __construct(string $entrypointJsonPath)
32+
public function __construct(string $entrypointJsonPath, CacheItemPoolInterface $cache = null, string $cacheKey = null)
3333
{
3434
$this->entrypointJsonPath = $entrypointJsonPath;
35+
$this->cache = $cache;
36+
$this->cacheKey = $cacheKey;
3537
}
3638

3739
public function getJavaScriptFiles(string $entryName): array
@@ -44,11 +46,6 @@ public function getCssFiles(string $entryName): array
4446
return $this->getEntryFiles($entryName, 'css');
4547
}
4648

47-
public function setCache(CacheItemPoolInterface $cache)
48-
{
49-
$this->cache = $cache;
50-
}
51-
5249
/**
5350
* Resets the state of this service.
5451
*/
@@ -92,52 +89,36 @@ private function validateEntryName(string $entryName)
9289

9390
private function getEntriesData(): array
9491
{
92+
if (null !== $this->entriesData) {
93+
return $this->entriesData;
94+
}
95+
9596
if ($this->cache) {
96-
$cached = $this->cache->getItem('entrypoint_lookup.data.'.$this->getCacheName());
97+
$cached = $this->cache->getItem($this->cacheKey);
98+
9799
if ($cached->isHit()) {
98-
return $cached->get();
100+
return $this->entriesData = $cached->get();
99101
}
100102
}
101103

102-
if (null === $this->entriesData) {
103-
if (!file_exists($this->entrypointJsonPath)) {
104-
throw new \InvalidArgumentException(sprintf('Could not find the entrypoints file from Webpack: the file "%s" does not exist.', $this->entrypointJsonPath));
105-
}
104+
if (!file_exists($this->entrypointJsonPath)) {
105+
throw new \InvalidArgumentException(sprintf('Could not find the entrypoints file from Webpack: the file "%s" does not exist.', $this->entrypointJsonPath));
106+
}
106107

107-
$this->entriesData = json_decode(file_get_contents($this->entrypointJsonPath), true);
108+
$this->entriesData = json_decode(file_get_contents($this->entrypointJsonPath), true);
108109

109-
if (null === $this->entriesData) {
110-
throw new \InvalidArgumentException(sprintf('There was a problem JSON decoding the "%s" file', $this->entrypointJsonPath));
111-
}
110+
if (null === $this->entriesData) {
111+
throw new \InvalidArgumentException(sprintf('There was a problem JSON decoding the "%s" file', $this->entrypointJsonPath));
112+
}
112113

113-
if (!isset($this->entriesData['entrypoints'])) {
114-
throw new \InvalidArgumentException(sprintf('Could not find an "entrypoints" key in the "%s" file', $this->entrypointJsonPath));
115-
}
114+
if (!isset($this->entriesData['entrypoints'])) {
115+
throw new \InvalidArgumentException(sprintf('Could not find an "entrypoints" key in the "%s" file', $this->entrypointJsonPath));
116116
}
117117

118118
if ($this->cache) {
119-
$cached->set($this->entriesData);
120-
$this->cache->save($cached);
119+
$this->cache->save($cached->set($this->entriesData));
121120
}
122121

123122
return $this->entriesData;
124123
}
125-
126-
private function getCacheName()
127-
{
128-
return str_replace('/', '.', $this->entrypointJsonPath);
129-
}
130-
131-
public function warmUp($cacheDir)
132-
{
133-
// If cache is enabled then run through the parser since it will save the results to cache
134-
if (!$this->cache) {
135-
return;
136-
}
137-
try {
138-
$this->getEntriesData();
139-
} catch (\InvalidArgumentException $e) {
140-
// If the fails are invalid for any reason just ignore it.
141-
}
142-
}
143124
}

src/CacheWarmer/EntrypointCacheWarmer.php

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,41 +9,37 @@
99

1010
namespace Symfony\WebpackEncoreBundle\CacheWarmer;
1111

12-
use Symfony\Component\DependencyInjection\ContainerInterface;
13-
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
14-
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupCollection;
12+
use Symfony\Bundle\FrameworkBundle\CacheWarmer\AbstractPhpFileCacheWarmer;
13+
use Symfony\Component\Cache\Adapter\ArrayAdapter;
14+
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookup;
1515

16-
class EntrypointCacheWarmer implements CacheWarmerInterface
16+
class EntrypointCacheWarmer extends AbstractPhpFileCacheWarmer
1717
{
18-
private $builds;
19-
private $container;
18+
private $cacheKeys;
2019

21-
public function __construct(array $builds, ContainerInterface $container)
20+
public function __construct(array $cacheKeys, string $phpArrayFile, CacheItemPoolInterface $fallbackPool)
2221
{
23-
$this->builds = $builds;
24-
$this->container = $container;
22+
$this->cacheKeys = $cacheKeys;
23+
parent::__construct($phpArrayFile, $fallbackPool);
2524
}
2625

27-
public function isOptional()
26+
/**
27+
* {@inheritdoc}
28+
*/
29+
protected function doWarmUp($cacheDir, ArrayAdapter $arrayAdapter)
2830
{
29-
return true;
30-
}
31-
32-
public function warmUp($cacheDir)
33-
{
34-
$entryPointCollection = $this->container->get('webpack_encore.entrypoint_lookup_collection');
35-
36-
if ($entryPointCollection instanceof EntrypointLookupCollection) {
37-
foreach ($this->builds as $build => $path) {
38-
$fullPath = $path.'/entrypoints.json';
31+
foreach ($this->cacheKeys as $cacheKey => $path) {
32+
// If the file does not exist then just skip past this entry point.
33+
if (!file_exists($path)) {
34+
continue;
35+
}
3936

40-
// If the file does not exist then just skip past this entry point.
41-
if (!file_exists($fullPath)) {
42-
continue;
43-
}
37+
$entryPointLookup = new EntrypointLookup($path, $arrayAdapter, $cacheKey);
4438

45-
$entryPointLookup = $entryPointCollection->getEntrypointLookup($build);
46-
$entryPointLookup->warmUp($cacheDir);
39+
try {
40+
$entryPointLookup->getJavaScriptFiles('dummy');
41+
} catch (EntrypointNotFoundException $e) {
42+
// ignore exception
4743
}
4844
}
4945
}

src/DependencyInjection/WebpackEncoreExtension.php

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,33 +29,30 @@ public function load(array $configs, ContainerBuilder $container)
2929
$config = $this->processConfiguration($configuration, $configs);
3030

3131
$factories = [
32-
'_default' => new Reference($this->entrypointFactory($container, '_default', $config['output_path'])),
32+
'_default' => $this->entrypointFactory($container, '_default', $config['output_path']),
33+
];
34+
$cacheKeys = [
35+
'_default' => $config['output_path'].'/entrypoints.json',
3336
];
3437
foreach ($config['builds'] as $name => $path) {
35-
$factories[$name] = new Reference($this->entrypointFactory($container, $name, $path));
38+
$path .= '/entrypoints.json';
39+
$factories[$name] = $this->entrypointFactory($container, $name, $path);
40+
$cacheKeys[rawurlencode($name)] = $path;
3641
}
3742

38-
$builds = [
39-
'_default' => $config['output_path'],
40-
];
41-
$builds = array_merge($builds, $config['builds']);
42-
$container->getDefinition('webpack_encore.entrypoint_lookup.warmer')
43-
->setArgument(0, $builds);
43+
$container->getDefinition('webpack_encore.entrypoint_lookup.cache_warmer')
44+
->replaceArgument(0, $cacheKeys);
4445

45-
$container->getDefinition('webpack_encore.entrypoint_lookup')
46-
->replaceArgument(0, $factories['_default']);
4746
$container->getDefinition('webpack_encore.entrypoint_lookup_collection')
4847
->replaceArgument(0, ServiceLocatorTagPass::register($container, $factories));
4948
}
5049

51-
private function entrypointFactory(ContainerBuilder $container, string $name, string $path): string
50+
private function entrypointFactory(ContainerBuilder $container, string $name, string $path): Reference
5251
{
5352
$id = sprintf('webpack_encore.entrypoint_lookup[%s]', $name);
54-
$cache = $container->findDefinition('webpack_encore.cache');
55-
$definition = new Definition(EntrypointLookup::class, [$path.'/entrypoints.json']);
56-
$definition->addMethodCall('setCache', [$cache]);
57-
$container->setDefinition($id, $definition);
53+
$arguments = [$path, new Reference('webpack_encore.cache'), $name];
54+
$container->setDefinition($id, new Definition(EntrypointLookup::class, $arguments));
5855

59-
return $id;
56+
return new Reference($id);
6057
}
6158
}

src/Resources/config/services.xml

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,8 @@
77
<services>
88
<defaults public="false" />
99

10-
<service id="webpack_encore.entrypoint_lookup" class="Symfony\WebpackEncoreBundle\Asset\EntrypointLookup">
11-
<argument /> <!-- entrypoints.json path -->
12-
<call method="setCache">
13-
<argument key="$cache" type="service" id="webpack_encore.cache" />
14-
</call>
15-
</service>
1610
<service id="webpack_encore.entrypoint_lookup_collection" class="Symfony\WebpackEncoreBundle\Asset\EntrypointLookupCollection">
17-
<argument /> <!-- build list of entrypoints path -->
11+
<argument /> <!-- build list of entrypoints locator -->
1812
</service>
1913

2014
<service id="webpack_encore.tag_renderer" class="Symfony\WebpackEncoreBundle\Asset\TagRenderer">
@@ -36,21 +30,21 @@
3630
</argument>
3731
</service>
3832

39-
<service id="webpack_encore.entrypoint_lookup.warmer" class="Symfony\WebpackEncoreBundle\CacheWarmer\EntrypointCacheWarmer">
33+
<service id="webpack_encore.entrypoint_lookup.cache_warmer" class="Symfony\WebpackEncoreBundle\CacheWarmer\EntrypointCacheWarmer">
4034
<tag name="kernel.cache_warmer" />
41-
<argument />
42-
<argument type="service" id="Symfony\Component\DependencyInjection\ContainerInterface" />
35+
<argument /> <!-- build list of entrypoint paths -->
36+
<argument>%kernel.cache_dir%/webpack_encore.cache.php</argument>
37+
<argument type="service" id="cache.webpack_encore" />
4338
</service>
4439

45-
<service id="webpack_encore.cache_fallback" class="Symfony\Component\Cache\Adapter\FilesystemAdapter">
46-
<argument />
47-
<argument>0</argument>
48-
<argument key="$directory">%kernel.cache_dir%/encore/</argument>
40+
<service id="webpack_encore.cache" class="Symfony\Component\Cache\Adapter\PhpArrayAdapter">
41+
<factory class="Symfony\Component\Cache\Adapter\PhpArrayAdapter" method="create" />
42+
<argument>%kernel.cache_dir%/webpack_encore.cache.php</argument>
43+
<argument type="service" id="cache.webpack_encore" />
4944
</service>
5045

51-
<service id="webpack_encore.cache" class="Symfony\Component\Cache\Adapter\PhpArrayAdapter">
52-
<argument key="$file">%kernel.cache_dir%/encore/lookup.cache</argument>
53-
<argument key="$fallbackPool" type="service" id="webpack_encore.cache_fallback"/>
46+
<service id="cache.webpack_encore" parent="cache.system" public="false">
47+
<tag name="cache.pool" />
5448
</service>
5549
</services>
5650
</container>

0 commit comments

Comments
 (0)