From 7cdba96adae71203b55bb70fcc80d509b81f1aad Mon Sep 17 00:00:00 2001 From: matheo Date: Mon, 23 Oct 2023 14:24:25 +0200 Subject: [PATCH 1/4] Add Listener to PreAssetsCompileEventListener --- config/services.php | 10 +++++ .../PreAssetsCompileEventListener.php | 38 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 src/Listener/PreAssetsCompileEventListener.php diff --git a/config/services.php b/config/services.php index e54490f..a4cacfe 100644 --- a/config/services.php +++ b/config/services.php @@ -5,6 +5,8 @@ use Symfonycasts\SassBundle\SassBuilder; use Symfonycasts\SassBundle\AssetMapper\SassCssCompiler; use Symfonycasts\SassBundle\AssetMapper\SassPublicPathAssetPathResolver; +use Symfonycasts\SassBundle\Listener\PreAssetsCompileEventListener; +use Symfony\Component\AssetMapper\Event\PreAssetsCompileEvent; use function Symfony\Component\DependencyInjection\Loader\Configurator\abstract_arg; use function Symfony\Component\DependencyInjection\Loader\Configurator\service; use function Symfony\Component\DependencyInjection\Loader\Configurator\param; @@ -35,10 +37,18 @@ abstract_arg('path to css output directory'), service('sass.builder'), ]) + ->set('sass.public_asset_path_resolver', SassPublicPathAssetPathResolver::class) ->decorate('asset_mapper.public_assets_path_resolver') ->args([ service('.inner') ]) + + ->set('sass.listener.pre_assets_compile', PreAssetsCompileEventListener::class) + ->tag('kernel.event_listener', [ + 'event' => PreAssetsCompileEvent::class, + 'method' => '__invoke' + ]) + ; ; }; diff --git a/src/Listener/PreAssetsCompileEventListener.php b/src/Listener/PreAssetsCompileEventListener.php new file mode 100644 index 0000000..99b6fd9 --- /dev/null +++ b/src/Listener/PreAssetsCompileEventListener.php @@ -0,0 +1,38 @@ + + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfonycasts\SassBundle\Listener; + +use Symfony\Component\AssetMapper\Event\PreAssetsCompileEvent; +use Symfony\Component\Console\Input\ArrayInput; +use Symfony\Component\Console\Style\SymfonyStyle; +use Symfonycasts\SassBundle\SassBuilder; + +class PreAssetsCompileEventListener +{ + public function __construct(private readonly SassBuilder $sassBuilder) {} + + public function __invoke(PreAssetsCompileEvent $preAssetsCompileEvent): void + { + $this->sassBuilder->setOutput( + new SymfonyStyle( + new ArrayInput([]), + $preAssetsCompileEvent->getOutput() + ) + ); + + $process = $this->sassBuilder->runBuild(false); + + if ($process->isSuccessful()) { + return; + } + + throw new \RuntimeException($process->getErrorOutput()); + } +} \ No newline at end of file From 3931ad14717edfcc76a36fb77785dc3396a2ebbe Mon Sep 17 00:00:00 2001 From: matheo Date: Mon, 23 Oct 2023 14:49:20 +0200 Subject: [PATCH 2/4] apply phpcsfixer --- src/Listener/PreAssetsCompileEventListener.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Listener/PreAssetsCompileEventListener.php b/src/Listener/PreAssetsCompileEventListener.php index 99b6fd9..885a26c 100644 --- a/src/Listener/PreAssetsCompileEventListener.php +++ b/src/Listener/PreAssetsCompileEventListener.php @@ -16,7 +16,9 @@ class PreAssetsCompileEventListener { - public function __construct(private readonly SassBuilder $sassBuilder) {} + public function __construct(private readonly SassBuilder $sassBuilder) + { + } public function __invoke(PreAssetsCompileEvent $preAssetsCompileEvent): void { @@ -35,4 +37,4 @@ public function __invoke(PreAssetsCompileEvent $preAssetsCompileEvent): void throw new \RuntimeException($process->getErrorOutput()); } -} \ No newline at end of file +} From 6538240485bcbb3fd1b007af881858abdbd7f1c3 Mon Sep 17 00:00:00 2001 From: matheo Date: Mon, 23 Oct 2023 18:48:51 +0200 Subject: [PATCH 3/4] better error message and fix phpstan error --- src/AssetMapper/SassPublicPathAssetPathResolver.php | 4 ++++ src/Listener/PreAssetsCompileEventListener.php | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/AssetMapper/SassPublicPathAssetPathResolver.php b/src/AssetMapper/SassPublicPathAssetPathResolver.php index 053b66d..d50419c 100644 --- a/src/AssetMapper/SassPublicPathAssetPathResolver.php +++ b/src/AssetMapper/SassPublicPathAssetPathResolver.php @@ -30,6 +30,10 @@ public function resolvePublicPath(string $logicalPath): string public function getPublicFilesystemPath(): string { + if (!method_exists($this->decorator, 'getPublicFilesystemPath')) { + throw new \Exception('...'); + } + $path = $this->decorator->getPublicFilesystemPath(); if (str_contains($path, '.scss')) { diff --git a/src/Listener/PreAssetsCompileEventListener.php b/src/Listener/PreAssetsCompileEventListener.php index 885a26c..8fc99da 100644 --- a/src/Listener/PreAssetsCompileEventListener.php +++ b/src/Listener/PreAssetsCompileEventListener.php @@ -35,6 +35,6 @@ public function __invoke(PreAssetsCompileEvent $preAssetsCompileEvent): void return; } - throw new \RuntimeException($process->getErrorOutput()); + throw new \RuntimeException(sprintf('Error compiling sass: "%s"', $process->getErrorOutput())); } } From 934787fc987383a7d0061d40cfe739f57ff25722 Mon Sep 17 00:00:00 2001 From: Matheo D <32077734+WebMamba@users.noreply.github.com> Date: Tue, 24 Oct 2023 18:39:57 +0200 Subject: [PATCH 4/4] Update error message --- src/AssetMapper/SassPublicPathAssetPathResolver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AssetMapper/SassPublicPathAssetPathResolver.php b/src/AssetMapper/SassPublicPathAssetPathResolver.php index d50419c..9c05c00 100644 --- a/src/AssetMapper/SassPublicPathAssetPathResolver.php +++ b/src/AssetMapper/SassPublicPathAssetPathResolver.php @@ -31,7 +31,7 @@ public function resolvePublicPath(string $logicalPath): string public function getPublicFilesystemPath(): string { if (!method_exists($this->decorator, 'getPublicFilesystemPath')) { - throw new \Exception('...'); + throw new \Exception('Something weird happened, we should never reach this line!'); } $path = $this->decorator->getPublicFilesystemPath();