Skip to content

Commit

Permalink
Merge pull request #22 from WebMamba/add_listener_to_pre_assets_compi…
Browse files Browse the repository at this point in the history
…le_event

Add Listener to PreAssetsCompileEvent
  • Loading branch information
bocharsky-bw committed Nov 14, 2023
2 parents e4cdfdc + 934787f commit b97c274
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
10 changes: 10 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'
])
;
;
};
4 changes: 4 additions & 0 deletions src/AssetMapper/SassPublicPathAssetPathResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public function resolvePublicPath(string $logicalPath): string

public function getPublicFilesystemPath(): string
{
if (!method_exists($this->decorator, 'getPublicFilesystemPath')) {
throw new \Exception('Something weird happened, we should never reach this line!');
}

$path = $this->decorator->getPublicFilesystemPath();

if (str_contains($path, '.scss')) {
Expand Down
40 changes: 40 additions & 0 deletions src/Listener/PreAssetsCompileEventListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of the SymfonyCasts SassBundle package.
* Copyright (c) SymfonyCasts <https://symfonycasts.com/>
* 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(sprintf('Error compiling sass: "%s"', $process->getErrorOutput()));
}
}

0 comments on commit b97c274

Please sign in to comment.