Skip to content

Added strict mode #43 #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/Asset/EntrypointLookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@ class EntrypointLookup implements EntrypointLookupInterface, IntegrityDataProvid

private $cache;

public function __construct(string $entrypointJsonPath, CacheItemPoolInterface $cache = null, string $cacheKey = null)
private $strictMode;

public function __construct(string $entrypointJsonPath, CacheItemPoolInterface $cache = null, string $cacheKey = null, bool $strictMode = true)
{
$this->entrypointJsonPath = $entrypointJsonPath;
$this->cache = $cache;
$this->cacheKey = $cacheKey;
$this->strictMode = $strictMode;
}

public function getJavaScriptFiles(string $entryName): array
Expand Down Expand Up @@ -69,7 +72,7 @@ private function getEntryFiles(string $entryName, string $key): array
{
$this->validateEntryName($entryName);
$entriesData = $this->getEntriesData();
$entryData = $entriesData['entrypoints'][$entryName];
$entryData = $entriesData['entrypoints'][$entryName] ?? [];

if (!isset($entryData[$key])) {
// If we don't find the file type then just send back nothing.
Expand All @@ -87,7 +90,7 @@ private function getEntryFiles(string $entryName, string $key): array
private function validateEntryName(string $entryName)
{
$entriesData = $this->getEntriesData();
if (!isset($entriesData['entrypoints'][$entryName])) {
if (!isset($entriesData['entrypoints'][$entryName]) && $this->strictMode) {
$withoutExtension = substr($entryName, 0, strrpos($entryName, '.'));

if (isset($entriesData['entrypoints'][$withoutExtension])) {
Expand All @@ -113,6 +116,9 @@ private function getEntriesData(): array
}

if (!file_exists($this->entrypointJsonPath)) {
if (!$this->strictMode) {
return [];
}
throw new \InvalidArgumentException(sprintf('Could not find the entrypoints file from Webpack: the file "%s" does not exist.', $this->entrypointJsonPath));
}

Expand Down
4 changes: 4 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public function getConfigTreeBuilder()
->info('Enable caching of the entry point file(s)')
->defaultFalse()
->end()
->booleanNode('strict_mode')
->info('Throw an exception if the entrypoints.json file is missing or an entry is missing from the data')
->defaultTrue()
->end()
->arrayNode('builds')
->useAttributeAsKey('name')
->normalizeKeys(false)
Expand Down
13 changes: 9 additions & 4 deletions src/DependencyInjection/WebpackEncoreExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ public function load(array $configs, ContainerBuilder $container)
$cacheKeys = [];

if (false !== $config['output_path']) {
$factories['_default'] = $this->entrypointFactory($container, '_default', $config['output_path'], $config['cache']);
$factories['_default'] = $this->entrypointFactory($container, '_default', $config['output_path'], $config['cache'], $config['strict_mode']);
$cacheKeys['_default'] = $config['output_path'].'/'.self::ENTRYPOINTS_FILE_NAME;
}

foreach ($config['builds'] as $name => $path) {
$factories[$name] = $this->entrypointFactory($container, $name, $path, $config['cache']);
$factories[$name] = $this->entrypointFactory($container, $name, $path, $config['cache'], $config['strict_mode']);
$cacheKeys[rawurlencode($name)] = $path.'/'.self::ENTRYPOINTS_FILE_NAME;
}

Expand All @@ -64,10 +64,15 @@ public function load(array $configs, ContainerBuilder $container)
->replaceArgument(2, $defaultAttributes);
}

private function entrypointFactory(ContainerBuilder $container, string $name, string $path, bool $cacheEnabled): Reference
private function entrypointFactory(ContainerBuilder $container, string $name, string $path, bool $cacheEnabled, bool $strictMode): Reference
{
$id = $this->getEntrypointServiceId($name);
$arguments = [$path.'/'.self::ENTRYPOINTS_FILE_NAME, $cacheEnabled ? new Reference('webpack_encore.cache') : null, $name];
$arguments = [
$path.'/'.self::ENTRYPOINTS_FILE_NAME,
$cacheEnabled ? new Reference('webpack_encore.cache') : null,
$name,
$strictMode,
];
$container->setDefinition($id, new Definition(EntrypointLookup::class, $arguments));

return new Reference($id);
Expand Down
38 changes: 34 additions & 4 deletions tests/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,32 @@ public function testCacheWarmer()
$this->assertEquals(['_default' => 0, 'different_build' => 1], $data[0]);
}

/**
* @expectedException \Twig\Error\RuntimeError
* @expectedExceptionMessageRegExp /Could not find the entrypoints file/
*/
public function testEnabledStrictMode_throwsException_ifBuildMissing()
{
$kernel = new WebpackEncoreIntegrationTestKernel(true);
$kernel->outputPath = 'missing_build';
$kernel->builds = ['different_build' => 'missing_build'];
$kernel->boot();
$container = $kernel->getContainer();
$container->get('twig')->render('@integration_test/template.twig');
}

public function testDisabledStrictMode_ignoresMissingBuild()
{
$kernel = new WebpackEncoreIntegrationTestKernel(true);
$kernel->outputPath = 'missing_build';
$kernel->strictMode = false;
$kernel->builds = ['different_build' => 'missing_build'];
$kernel->boot();
$container = $kernel->getContainer();
$html = $container->get('twig')->render('@integration_test/template.twig');
self::assertSame('', trim($html));
}

public function testAutowireableInterfaces()
{
$kernel = new WebpackEncoreIntegrationTestKernel(true);
Expand All @@ -118,6 +144,11 @@ public function testAutowireableInterfaces()
class WebpackEncoreIntegrationTestKernel extends Kernel
{
private $enableAssets;
public $strictMode = true;
public $outputPath = __DIR__.'/fixtures/build';
public $builds = [
'different_build' => __DIR__.'/fixtures/different_build'
];

public function __construct($enableAssets)
{
Expand Down Expand Up @@ -152,12 +183,11 @@ public function registerContainerConfiguration(LoaderInterface $loader)
]);

$container->loadFromExtension('webpack_encore', [
'output_path' => __DIR__.'/fixtures/build',
'output_path' => $this->outputPath,
'cache' => true,
'crossorigin' => false,
'builds' => [
'different_build' => __DIR__.'/fixtures/different_build',
],
'builds' => $this->builds,
'strict_mode' => $this->strictMode,
]);

$container->register(WebpackEncoreCacheWarmerTester::class)
Expand Down