From 5fc38ffe570a17f6498974be8287d33ed45bbd9e Mon Sep 17 00:00:00 2001 From: Bastien Miclo Date: Mon, 20 Nov 2023 10:43:32 +0100 Subject: [PATCH] Fix compatibility with Blade extensions and custom directives --- src/PugBladeCompiler.php | 14 ++++++++++---- src/PugCompiler.php | 16 ++++++++-------- src/PugHandlerTrait.php | 20 ++++++++++++++------ src/ServiceProvider.php | 10 ++++++---- tests/PugExceptionTest.php | 1 - 5 files changed, 38 insertions(+), 23 deletions(-) diff --git a/src/PugBladeCompiler.php b/src/PugBladeCompiler.php index c1854e9..3a94c16 100644 --- a/src/PugBladeCompiler.php +++ b/src/PugBladeCompiler.php @@ -18,9 +18,14 @@ class PugBladeCompiler extends BladeCompiler implements PugHandlerInterface * @param array $config * @param string $defaultCachePath */ - public function __construct(array $pugTarget, Filesystem $files, array $config, $defaultCachePath = null) - { - $this->construct($pugTarget, $files, $config, $defaultCachePath); + public function __construct( + array $pugTarget, + Filesystem $files, + array $config, + $defaultCachePath = null, + $compiler = null + ) { + $this->construct($pugTarget, $files, $config, $defaultCachePath, $compiler); } /** @@ -56,6 +61,7 @@ public function compile($path = null): void } $this->footer = []; - $this->compileWith($path, [$this, 'compileString']); + $compiler = $this->compiler instanceof BladeCompiler ? $this->compiler : $this; + $this->compileWith($path, [$compiler, 'compileString']); } } diff --git a/src/PugCompiler.php b/src/PugCompiler.php index acd55d6..875d70f 100644 --- a/src/PugCompiler.php +++ b/src/PugCompiler.php @@ -11,15 +11,15 @@ class PugCompiler extends Compiler implements PugHandlerInterface /** * Create a new compiler instance. - * - * @param array $pugTarget - * @param Filesystem $files - * @param array $config - * @param string $defaultCachePath */ - public function __construct(array $pugTarget, Filesystem $files, array $config, $defaultCachePath = null) - { - $this->construct($pugTarget, $files, $config, $defaultCachePath); + public function __construct( + array $pugTarget, + Filesystem $files, + array $config, + $defaultCachePath = null, + $compiler = null + ) { + $this->construct($pugTarget, $files, $config, $defaultCachePath, $compiler); } /** diff --git a/src/PugHandlerTrait.php b/src/PugHandlerTrait.php index f47b37b..17968c5 100644 --- a/src/PugHandlerTrait.php +++ b/src/PugHandlerTrait.php @@ -3,6 +3,7 @@ namespace Bkwld\LaravelPug; use Illuminate\Filesystem\Filesystem; +use Illuminate\View\Compilers\BladeCompiler; use InvalidArgumentException; use Phug\Compiler; use Phug\CompilerInterface; @@ -20,16 +21,23 @@ trait PugHandlerTrait */ protected $pug; + /** + * @var BladeCompiler|mixed + */ + protected $compiler; + /** * Common pug compiler constructor. - * - * @param array $pugTarget - * @param Filesystem $files - * @param array $config */ - public function construct(array $pugTarget, Filesystem $files, array $config, $defaultCachePath = null) - { + public function construct( + array $pugTarget, + Filesystem $files, + array $config, + $defaultCachePath = null, + $compiler = null + ) { $this->pugTarget = $pugTarget; + $this->compiler = $compiler; $cachePath = null; foreach (['cache_dir', 'cache', 'defaultCache'] as $name) { diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 800ecea..89bd85f 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -114,7 +114,8 @@ protected function getCompilerCreator($compilerClass): Closure [$app, 'laravel-pug.pug'], $app['files'], $this->getConfig(), - $this->getDefaultCache() + $this->getDefaultCache(), + $this->app['blade.compiler'] ?? null ); }; } @@ -186,14 +187,15 @@ public function registerPugCompiler(string $subExtension = ''): void // Add resolver $this->getEngineResolver()->register($mainExtension, function () use ($subExtension) { - return new CompilerEngine($this->app['Bkwld\LaravelPug\Pug'.ucfirst(ltrim($subExtension, '.')).'Compiler']); + return new CompilerEngine( + $this->app['Bkwld\LaravelPug\Pug'.ucfirst(ltrim($subExtension, '.')).'Compiler'] + ); }); $this->app['view']->addExtension($mainExtension, $mainExtension); if ($subExtension !== '') { - $subExtensionPrefix = substr($subExtension, 1).'.'; - $this->app['view']->addExtension($subExtensionPrefix.'pug', $mainExtension); + $this->app['view']->addExtension(substr($subExtension, 1).'.pug', $mainExtension); } } diff --git a/tests/PugExceptionTest.php b/tests/PugExceptionTest.php index cb057bb..5835521 100644 --- a/tests/PugExceptionTest.php +++ b/tests/PugExceptionTest.php @@ -5,7 +5,6 @@ use Bkwld\LaravelPug\PugCompiler; use Bkwld\LaravelPug\PugException; use Bkwld\LaravelPug\ServiceProvider; -use Exception; use Facade\Ignition\Exceptions\ViewException; use Illuminate\Filesystem\Filesystem; use Illuminate\View\Engines\CompilerEngine;