From c40000728e33c4a6310448472482cb2954d1cdfb Mon Sep 17 00:00:00 2001 From: Eric Richer Date: Tue, 31 Jan 2023 14:18:02 -0500 Subject: [PATCH] Added support for routeNotFoundUseDefault --- src/Config/WebpackOptions.php | 8 +++++++- src/View/Helper/ScriptLoaderHelper.php | 19 +++++++++++++++++-- src/View/Helper/ScriptLoaderHelperFactory.php | 6 ++++-- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/Config/WebpackOptions.php b/src/Config/WebpackOptions.php index da7d4ad..10d02e5 100644 --- a/src/Config/WebpackOptions.php +++ b/src/Config/WebpackOptions.php @@ -102,7 +102,8 @@ public function getScriptListByRoute($routeMatched) return $this->entry_point_map[$this->default_entry_point]; } } - return []; + if ($this->routeNotFoundUseDefault) return $this->getScriptListByTemplate(); + else return []; } /** @@ -121,6 +122,11 @@ public function getScriptListByTemplate($template) return []; } + public function getDefaultScriptList(): array + { + return $this->entry_point_map[$this->default_entry_point]; + } + public function getRouteNotFoundUseDefault(): bool { return $this->routeNotFoundUseDefault; diff --git a/src/View/Helper/ScriptLoaderHelper.php b/src/View/Helper/ScriptLoaderHelper.php index c6c3dcc..0d902de 100644 --- a/src/View/Helper/ScriptLoaderHelper.php +++ b/src/View/Helper/ScriptLoaderHelper.php @@ -10,6 +10,7 @@ use Laminas\View\Helper\AbstractHelper; use Laminas\View\Renderer\PhpRenderer; +use Webpack\Config\WebpackOptions; class ScriptLoaderHelper extends AbstractHelper { @@ -21,9 +22,12 @@ class ScriptLoaderHelper extends AbstractHelper protected $rendered = false; - public function __construct($renderer) + protected WebpackOptions $options; + + public function __construct($renderer, $options) { $this->renderer = $renderer; + $this->options = $options; } /** @@ -38,6 +42,7 @@ public function __invoke() $view = $this->getView(); // Get the list of scripts $scriptlist = $this->renderer->scriptlist; + if ($scriptlist && is_array($scriptlist)) { if (method_exists($view, 'plugin')) { $helper = $view->plugin('headScript'); @@ -47,6 +52,16 @@ public function __invoke() } $this->rendered = true; } + } elseif ($this->options->getRouteNotFoundUseDefault()) { + if (method_exists($view, 'plugin')) { + $helper = $view->plugin('headScript'); + $defautlScriptList = $this->options->getDefaultScriptList(); + foreach ($defautlScriptList as $key => $script) { + // Append the list of scripts in the HEAD section + $helper->appendFile($script); + } + $this->rendered = true; + } } } -} \ No newline at end of file +} diff --git a/src/View/Helper/ScriptLoaderHelperFactory.php b/src/View/Helper/ScriptLoaderHelperFactory.php index febf68e..9a1addb 100644 --- a/src/View/Helper/ScriptLoaderHelperFactory.php +++ b/src/View/Helper/ScriptLoaderHelperFactory.php @@ -15,6 +15,7 @@ use Interop\Container\ContainerInterface; use Laminas\ServiceManager\Factory\FactoryInterface; use Laminas\View\Renderer\PhpRenderer; +use Webpack\Config\WebpackOptions; class ScriptLoaderHelperFactory implements FactoryInterface { @@ -28,6 +29,7 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o // Get the PHP Renderer service $renderer = $container->get(PhpRenderer::class); if (!$renderer) throw new Exception('No PHP Renderer defined'); - return new ScriptLoaderHelper($renderer); + $webpackOptions = $container->get(WebpackOptions::class); + return new ScriptLoaderHelper($renderer, $webpackOptions); } -} \ No newline at end of file +}