Skip to content

Commit 5c0f659

Browse files
committed
bug #74 Reset default EntrypointLookup on exception to fix #21 and #73 (tbmatuka)
This PR was squashed before being merged into the master branch (closes #74). Discussion ---------- Reset default EntrypointLookup on exception to fix #21 and #73 Since @ckrack seems to be unavailable to continue working on #21, I figured this would be faster :) I don't like having `_default` hardcoded in the listener, but I see no other options right now. I thought about adding a `resetAll()` method to EntrypointLookupCollection, but there were a couple of issues with that: 1. It would either be a BC break if I added it to the interface, or an important method that isn't interfaced and I don't really like either of those. 2. I couldn't even implement it because the container that we get in the collection only has `has()` and `get()` methods, so I couldn't go through it. This would also have to be replaced (and break BC) to implement `resetAll()`. Fixes symfony/demo#910 Commits ------- da36629 Reset default EntrypointLookup on exception to fix #21 and #73
2 parents 787c2fd + da36629 commit 5c0f659

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

src/DependencyInjection/WebpackEncoreExtension.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ public function load(array $configs, ContainerBuilder $container)
4949
$cacheKeys[rawurlencode($name)] = $path.'/'.self::ENTRYPOINTS_FILE_NAME;
5050
}
5151

52+
$container->getDefinition('webpack_encore.exception_listener')
53+
->replaceArgument(1, array_keys($factories));
54+
5255
$container->getDefinition('webpack_encore.entrypoint_lookup.cache_warmer')
5356
->replaceArgument(0, $cacheKeys);
5457

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony WebpackEncoreBundle package.
5+
* (c) Fabien Potencier <[email protected]>
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace Symfony\WebpackEncoreBundle\EventListener;
11+
12+
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupCollection;
13+
14+
class ExceptionListener
15+
{
16+
private $entrypointLookupCollection;
17+
18+
private $buildNames;
19+
20+
public function __construct(EntrypointLookupCollection $entrypointLookupCollection, array $buildNames)
21+
{
22+
$this->entrypointLookupCollection = $entrypointLookupCollection;
23+
$this->buildNames = $buildNames;
24+
}
25+
26+
public function onKernelException()
27+
{
28+
foreach ($this->buildNames as $buildName) {
29+
$this->entrypointLookupCollection->getEntrypointLookup($buildName)->reset();
30+
}
31+
}
32+
}

src/Resources/config/services.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@
5050
<tag name="cache.pool" />
5151
</service>
5252

53+
<service id="webpack_encore.exception_listener" class="Symfony\WebpackEncoreBundle\EventListener\ExceptionListener">
54+
<tag name="kernel.event_listener" event="kernel.exception" />
55+
<argument type="service" id="webpack_encore.entrypoint_lookup_collection" />
56+
<argument /> <!-- build list of build names -->
57+
</service>
58+
5359
<service id="webpack_encore.preload_assets_event_listener" class="Symfony\WebpackEncoreBundle\EventListener\PreLoadAssetsEventListener">
5460
<tag name="kernel.event_subscriber" />
5561
<argument type="service" id="webpack_encore.tag_renderer" />
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony WebpackEncoreBundle package.
5+
* (c) Fabien Potencier <[email protected]>
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace Symfony\WebpackEncoreBundle\Tests\EventListener;
11+
12+
use Symfony\Component\HttpFoundation\Request;
13+
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
14+
use Symfony\Component\HttpKernel\HttpKernelInterface;
15+
use PHPUnit\Framework\TestCase;
16+
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupCollection;
17+
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupInterface;
18+
use Symfony\WebpackEncoreBundle\EventListener\ExceptionListener;
19+
20+
class ExceptionListenerTest extends TestCase
21+
{
22+
public function testItResetsAllEntrypointLookups()
23+
{
24+
/** @var EntrypointLookupInterface[]|Prophecy\Prophecy\ObjectProphecy[] $entrypointLookups */
25+
$entrypointLookups = [];
26+
$entrypointLookupsValueMap = [];
27+
28+
$buildNames = ['_default', '_test'];
29+
foreach ($buildNames as $buildName) {
30+
$entrypointLookups[$buildName] = $this->createMock(EntrypointLookupInterface::class);
31+
$entrypointLookups[$buildName]->expects($this->once())->method('reset');
32+
33+
$entrypointLookupsValueMap[] = [$buildName, $entrypointLookups[$buildName]];
34+
}
35+
36+
$entrypointLookupCollection = $this->createMock(EntrypointLookupCollection::class);
37+
$entrypointLookupCollection->method('getEntrypointLookup')
38+
->willReturnMap($entrypointLookupsValueMap);
39+
40+
$request = new Request();
41+
$exception = new \Exception();
42+
$event = new GetResponseForExceptionEvent(
43+
$this->createMock(HttpKernelInterface::class),
44+
$request,
45+
HttpKernelInterface::MASTER_REQUEST,
46+
$exception
47+
);
48+
$listener = new ExceptionListener($entrypointLookupCollection, $buildNames);
49+
$listener->onKernelException($event);
50+
}
51+
}

0 commit comments

Comments
 (0)