Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Listener to PreAssetsCompileEvent #22

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link

@ogizanagi ogizanagi Nov 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The argument is missing:

            ->args([
                service('sass.builder')
            ])

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds valid. Would you like to create a PR?

//cc @WebMamba

->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()));
}
}