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

Fix compatibility with Blade extensions and custom directives #101

Merged
merged 1 commit into from
Nov 20, 2023
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
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
Loading