diff --git a/src/DependencyInjection/WebpackEncoreExtension.php b/src/DependencyInjection/WebpackEncoreExtension.php index 053e5155..521a2e97 100644 --- a/src/DependencyInjection/WebpackEncoreExtension.php +++ b/src/DependencyInjection/WebpackEncoreExtension.php @@ -49,6 +49,9 @@ public function load(array $configs, ContainerBuilder $container) $cacheKeys[rawurlencode($name)] = $path.'/'.self::ENTRYPOINTS_FILE_NAME; } + $container->getDefinition('webpack_encore.exception_listener') + ->replaceArgument(1, array_keys($factories)); + $container->getDefinition('webpack_encore.entrypoint_lookup.cache_warmer') ->replaceArgument(0, $cacheKeys); diff --git a/src/EventListener/ExceptionListener.php b/src/EventListener/ExceptionListener.php new file mode 100644 index 00000000..57a851ee --- /dev/null +++ b/src/EventListener/ExceptionListener.php @@ -0,0 +1,32 @@ + + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\WebpackEncoreBundle\EventListener; + +use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupCollection; + +class ExceptionListener +{ + private $entrypointLookupCollection; + + private $buildNames; + + public function __construct(EntrypointLookupCollection $entrypointLookupCollection, array $buildNames) + { + $this->entrypointLookupCollection = $entrypointLookupCollection; + $this->buildNames = $buildNames; + } + + public function onKernelException() + { + foreach ($this->buildNames as $buildName) { + $this->entrypointLookupCollection->getEntrypointLookup($buildName)->reset(); + } + } +} diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml index 409c14b4..2464c368 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -50,6 +50,12 @@ + + + + + + diff --git a/tests/EventListener/ExceptionListenerTest.php b/tests/EventListener/ExceptionListenerTest.php new file mode 100644 index 00000000..7e0df5a0 --- /dev/null +++ b/tests/EventListener/ExceptionListenerTest.php @@ -0,0 +1,51 @@ + + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\WebpackEncoreBundle\Tests\EventListener; + +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; +use Symfony\Component\HttpKernel\HttpKernelInterface; +use PHPUnit\Framework\TestCase; +use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupCollection; +use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupInterface; +use Symfony\WebpackEncoreBundle\EventListener\ExceptionListener; + +class ExceptionListenerTest extends TestCase +{ + public function testItResetsAllEntrypointLookups() + { + /** @var EntrypointLookupInterface[]|Prophecy\Prophecy\ObjectProphecy[] $entrypointLookups */ + $entrypointLookups = []; + $entrypointLookupsValueMap = []; + + $buildNames = ['_default', '_test']; + foreach ($buildNames as $buildName) { + $entrypointLookups[$buildName] = $this->createMock(EntrypointLookupInterface::class); + $entrypointLookups[$buildName]->expects($this->once())->method('reset'); + + $entrypointLookupsValueMap[] = [$buildName, $entrypointLookups[$buildName]]; + } + + $entrypointLookupCollection = $this->createMock(EntrypointLookupCollection::class); + $entrypointLookupCollection->method('getEntrypointLookup') + ->willReturnMap($entrypointLookupsValueMap); + + $request = new Request(); + $exception = new \Exception(); + $event = new GetResponseForExceptionEvent( + $this->createMock(HttpKernelInterface::class), + $request, + HttpKernelInterface::MASTER_REQUEST, + $exception + ); + $listener = new ExceptionListener($entrypointLookupCollection, $buildNames); + $listener->onKernelException($event); + } +}