Skip to content

Commit 108f5da

Browse files
committed
strict mode is handled in entrypoint lookup classes instead of twig extension
1 parent fb25389 commit 108f5da

File tree

6 files changed

+58
-38
lines changed

6 files changed

+58
-38
lines changed

src/Asset/EmptyEntrypointLookup.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Symfony\WebpackEncoreBundle\Asset;
4+
5+
class EmptyEntrypointLookup implements EntrypointLookupInterface
6+
{
7+
public function getJavaScriptFiles(string $entryName): array
8+
{
9+
return [];
10+
}
11+
12+
public function getCssFiles(string $entryName): array
13+
{
14+
return [];
15+
}
16+
17+
public function reset() {}
18+
}

src/Asset/EntrypointLookup.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,15 @@ class EntrypointLookup implements EntrypointLookupInterface
2929

3030
private $cache;
3131

32-
public function __construct(string $entrypointJsonPath, CacheItemPoolInterface $cache = null, string $cacheKey = null)
32+
private $strictMode;
33+
34+
public function __construct(string $entrypointJsonPath, CacheItemPoolInterface $cache = null,
35+
string $cacheKey = null, bool $strictMode = true)
3336
{
3437
$this->entrypointJsonPath = $entrypointJsonPath;
3538
$this->cache = $cache;
3639
$this->cacheKey = $cacheKey;
40+
$this->strictMode = $strictMode;
3741
}
3842

3943
public function getJavaScriptFiles(string $entryName): array
@@ -58,7 +62,7 @@ private function getEntryFiles(string $entryName, string $key): array
5862
{
5963
$this->validateEntryName($entryName);
6064
$entriesData = $this->getEntriesData();
61-
$entryData = $entriesData['entrypoints'][$entryName];
65+
$entryData = $entriesData['entrypoints'][$entryName] ?? [];
6266

6367
if (!isset($entryData[$key])) {
6468
// If we don't find the file type then just send back nothing.
@@ -76,7 +80,7 @@ private function getEntryFiles(string $entryName, string $key): array
7680
private function validateEntryName(string $entryName)
7781
{
7882
$entriesData = $this->getEntriesData();
79-
if (!isset($entriesData['entrypoints'][$entryName])) {
83+
if (!isset($entriesData['entrypoints'][$entryName]) && $this->strictMode) {
8084
$withoutExtension = substr($entryName, 0, strrpos($entryName, '.'));
8185

8286
if (isset($entriesData['entrypoints'][$withoutExtension])) {
@@ -102,7 +106,11 @@ private function getEntriesData(): array
102106
}
103107

104108
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));
109+
if ($this->strictMode) {
110+
throw new \InvalidArgumentException(sprintf('Could not find the entrypoints file from Webpack: the file "%s" does not exist.', $this->entrypointJsonPath));
111+
} else {
112+
return [];
113+
}
106114
}
107115

108116
$this->entriesData = json_decode(file_get_contents($this->entrypointJsonPath), true);

src/Asset/EntrypointLookupCollection.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,28 @@ class EntrypointLookupCollection implements EntrypointLookupCollectionInterface
2525

2626
private $defaultBuildName;
2727

28-
public function __construct(ContainerInterface $buildEntrypoints, string $defaultBuildName = null)
28+
private $strictMode;
29+
30+
public function __construct(ContainerInterface $buildEntrypoints, string $defaultBuildName = null, bool $strictMode = true)
2931
{
3032
$this->buildEntrypoints = $buildEntrypoints;
3133
$this->defaultBuildName = $defaultBuildName;
34+
$this->strictMode = $strictMode;
3235
}
3336

3437
public function getEntrypointLookup(string $buildName = null): EntrypointLookupInterface
3538
{
3639
if (null === $buildName) {
3740
if (null === $this->defaultBuildName) {
41+
if (!$this->strictMode) return new EmptyEntrypointLookup();
3842
throw new UndefinedBuildException('There is no default build configured: please pass an argument to getEntrypointLookup().');
3943
}
4044

4145
$buildName = $this->defaultBuildName;
4246
}
4347

4448
if (!$this->buildEntrypoints->has($buildName)) {
49+
if (!$this->strictMode) return new EmptyEntrypointLookup();
4550
throw new UndefinedBuildException(sprintf('The build "%s" is not configured', $buildName));
4651
}
4752

src/DependencyInjection/WebpackEncoreExtension.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,32 +33,39 @@ public function load(array $configs, ContainerBuilder $container)
3333
$config = $this->processConfiguration($configuration, $configs);
3434

3535
$factories = [
36-
'_default' => $this->entrypointFactory($container, '_default', $config['output_path'], $config['cache']),
36+
'_default' => $this->entrypointFactory($container, '_default', $config['output_path'], $config['cache'], $config['strict_mode']),
3737
];
3838
$cacheKeys = [
3939
'_default' => $config['output_path'].'/'.self::ENTRYPOINTS_FILE_NAME,
4040
];
4141
foreach ($config['builds'] as $name => $path) {
42-
$factories[$name] = $this->entrypointFactory($container, $name, $path, $config['cache']);
42+
$factories[$name] = $this->entrypointFactory($container, $name, $path, $config['cache'], $config['strict_mode']);
4343
$cacheKeys[rawurlencode($name)] = $path.'/'.self::ENTRYPOINTS_FILE_NAME;
4444
}
4545

4646
$container->getDefinition('webpack_encore.entrypoint_lookup.cache_warmer')
4747
->replaceArgument(0, $cacheKeys);
4848

4949
$container->getDefinition('webpack_encore.entrypoint_lookup_collection')
50-
->replaceArgument(0, ServiceLocatorTagPass::register($container, $factories));
50+
->replaceArgument(0, ServiceLocatorTagPass::register($container, $factories))
51+
->replaceArgument(2, $config['strict_mode']);
5152

5253
$container->getDefinition('webpack_encore.twig_entry_files_extension')
5354
->replaceArgument(1, $config['strict_mode']);
5455

5556
$container->setAlias(EntrypointLookupInterface::class, new Alias($this->getEntrypointServiceId('_default')));
5657
}
5758

58-
private function entrypointFactory(ContainerBuilder $container, string $name, string $path, bool $cacheEnabled): Reference
59+
private function entrypointFactory(ContainerBuilder $container, string $name, string $path,
60+
bool $cacheEnabled, bool $strictMode): Reference
5961
{
6062
$id = $this->getEntrypointServiceId($name);
61-
$arguments = [$path.'/'.self::ENTRYPOINTS_FILE_NAME, $cacheEnabled ? new Reference('webpack_encore.cache') : null, $name];
63+
$arguments = [
64+
$path.'/'.self::ENTRYPOINTS_FILE_NAME,
65+
$cacheEnabled ? new Reference('webpack_encore.cache') : null,
66+
$name,
67+
$strictMode,
68+
];
6269
$container->setDefinition($id, new Definition(EntrypointLookup::class, $arguments));
6370

6471
return new Reference($id);

src/Resources/config/services.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
<service id="webpack_encore.entrypoint_lookup_collection" class="Symfony\WebpackEncoreBundle\Asset\EntrypointLookupCollection">
1111
<argument /> <!-- build list of entrypoints locator -->
12+
<argument /> <!-- defaultBuildName -->
13+
<argument /> <!-- strictMode -->
1214
</service>
1315

1416
<service id="Symfony\WebpackEncoreBundle\Asset\EntrypointLookupCollectionInterface" alias="webpack_encore.entrypoint_lookup_collection" />

src/Twig/EntryFilesTwigExtension.php

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -38,46 +38,26 @@ public function getFunctions()
3838

3939
public function getWebpackJsFiles(string $entryName, string $entrypointName = '_default'): array
4040
{
41-
try {
42-
return $this->getEntrypointLookup($entrypointName)
43-
->getJavaScriptFiles($entryName);
44-
} catch (\InvalidArgumentException $e) {
45-
if ($this->strictMode) throw $e;
46-
return [];
47-
}
41+
return $this->getEntrypointLookup($entrypointName)
42+
->getJavaScriptFiles($entryName);
4843
}
4944

5045
public function getWebpackCssFiles(string $entryName, string $entrypointName = '_default'): array
5146
{
52-
try {
53-
return $this->getEntrypointLookup($entrypointName)
54-
->getCssFiles($entryName);
55-
} catch (\InvalidArgumentException $e) {
56-
if ($this->strictMode) throw $e;
57-
return [];
58-
}
47+
return $this->getEntrypointLookup($entrypointName)
48+
->getCssFiles($entryName);
5949
}
6050

6151
public function renderWebpackScriptTags(string $entryName, string $packageName = null, string $entrypointName = '_default'): string
6252
{
63-
try {
64-
return $this->getTagRenderer()
65-
->renderWebpackScriptTags($entryName, $packageName, $entrypointName);
66-
} catch (\InvalidArgumentException $e) {
67-
if ($this->strictMode) throw $e;
68-
return '';
69-
}
53+
return $this->getTagRenderer()
54+
->renderWebpackScriptTags($entryName, $packageName, $entrypointName);
7055
}
7156

7257
public function renderWebpackLinkTags(string $entryName, string $packageName = null, string $entrypointName = '_default'): string
7358
{
74-
try {
75-
return $this->getTagRenderer()
76-
->renderWebpackLinkTags($entryName, $packageName, $entrypointName);
77-
} catch (\InvalidArgumentException $e) {
78-
if ($this->strictMode) throw $e;
79-
return '';
80-
}
59+
return $this->getTagRenderer()
60+
->renderWebpackLinkTags($entryName, $packageName, $entrypointName);
8161
}
8262

8363
private function getEntrypointLookup(string $entrypointName): EntrypointLookupInterface

0 commit comments

Comments
 (0)