Skip to content

Commit

Permalink
Fix compatibility with Blade extensions and custom directives
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Nov 20, 2023
1 parent 180bd0b commit 5fc38ff
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 23 deletions.
14 changes: 10 additions & 4 deletions src/PugBladeCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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']);
}
}
16 changes: 8 additions & 8 deletions src/PugCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
20 changes: 14 additions & 6 deletions src/PugHandlerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Bkwld\LaravelPug;

use Illuminate\Filesystem\Filesystem;
use Illuminate\View\Compilers\BladeCompiler;
use InvalidArgumentException;
use Phug\Compiler;
use Phug\CompilerInterface;
Expand All @@ -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) {
Expand Down
10 changes: 6 additions & 4 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
};
}
Expand Down Expand Up @@ -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);
}
}

Expand Down
1 change: 0 additions & 1 deletion tests/PugExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 5fc38ff

Please sign in to comment.