Skip to content

Commit

Permalink
Isolate compiler (#57)
Browse files Browse the repository at this point in the history
* Isolate compiler

* Fix TravisCI versions

* Add a getCompiler class

* Add an enableBladeDirectives method

* Fix Laravel 5.8 detection

* Add annotations for getCompiler
  • Loading branch information
kylekatarnls authored Dec 2, 2018
1 parent 63f3a80 commit 9eb4efc
Show file tree
Hide file tree
Showing 9 changed files with 465 additions and 441 deletions.
126 changes: 87 additions & 39 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,133 +8,181 @@ matrix:
- php: 5.4
env:
- LARAVEL_VERSION='4.2.*'
- PUG_VERSION='^2.7.1'
- PUG_VERSION='^2.7'
- php: 5.5
env:
- LARAVEL_VERSION='4.2.*'
- PUG_VERSION='^2.7.1'
- PUG_VERSION='^2.7'
- php: 5.5
env:
- LARAVEL_VERSION='4.2.*'
- PUG_VERSION='^3.0.0'
- PUG_VERSION='^3.0'
- php: 5.5
env:
- LARAVEL_VERSION='5.2.*'
- PUG_VERSION='^2.7.1'
- PUG_VERSION='^2.7'
- php: 5.5
env:
- LARAVEL_VERSION='5.2.*'
- PUG_VERSION='^3.0.0'
- PUG_VERSION='^3.0'
- php: 5.6
env:
- LARAVEL_VERSION='4.2.*'
- PUG_VERSION='^2.7.1'
- PUG_VERSION='^2.7'
- php: 5.6
env:
- LARAVEL_VERSION='4.2.*'
- PUG_VERSION='^3.0.0'
- PUG_VERSION='^3.0'
- php: 5.6
env:
- LARAVEL_VERSION='5.4.*'
- PUG_VERSION='^2.7.1'
- PUG_VERSION='^2.7'
- php: 5.6
env:
- LARAVEL_VERSION='5.4.*'
- PUG_VERSION='^3.0.0'
- PUG_VERSION='^3.0'
- php: 7.0
env:
- LARAVEL_VERSION='4.2.*'
- PUG_VERSION='^2.7.1'
- PUG_VERSION='^2.7'
- php: 7.0
env:
- LARAVEL_VERSION='4.2.*'
- PUG_VERSION='^3.0.0'
- PUG_VERSION='^3.0'
- php: 7.0
env:
- LARAVEL_VERSION='5.4.*'
- PUG_VERSION='^2.7.1'
- PUG_VERSION='^2.7'
- php: 7.0
env:
- LARAVEL_VERSION='5.4.*'
- PUG_VERSION='^3.0.0'
- PUG_VERSION='^3.0'
- php: 7.0
env:
- LARAVEL_VERSION='5.5.*'
- PUG_VERSION='^2.7.1'
- PUG_VERSION='^2.7'
- php: 7.0
env:
- LARAVEL_VERSION='5.5.*'
- PUG_VERSION='^3.0.0'
- PUG_VERSION='^3.0'
- php: 7.1
env:
- LARAVEL_VERSION='4.2.*'
- PUG_VERSION='^2.7.1'
- PUG_VERSION='^2.7'
- php: 7.1
env:
- LARAVEL_VERSION='4.2.*'
- PUG_VERSION='^3.0.0'
- PUG_VERSION='^3.0'
- php: 7.1
env:
- LARAVEL_VERSION='5.4.*'
- PUG_VERSION='^2.7.1'
- PUG_VERSION='^2.7'
- php: 7.1
env:
- LARAVEL_VERSION='5.4.*'
- PUG_VERSION='^3.0.0'
- PUG_VERSION='^3.0'
- php: 7.1
env:
- LARAVEL_VERSION='5.5.*'
- PUG_VERSION='^2.7.1'
- PUG_VERSION='^2.7'
- php: 7.1
env:
- LARAVEL_VERSION='5.5.*'
- PUG_VERSION='^3.0.0'
- php: 7.2.0
- PUG_VERSION='^3.0'
- php: 7.2
env:
- LARAVEL_VERSION='4.2.*'
- PUG_VERSION='^2.7.1'
- php: 7.2.0
- PUG_VERSION='^2.7'
- php: 7.2
env:
- LARAVEL_VERSION='4.2.*'
- PUG_VERSION='^3.0.0'
- php: 7.2.0
- PUG_VERSION='^3.0'
- php: 7.2
env:
- LARAVEL_VERSION='5.4.*'
- PUG_VERSION='^2.7.1'
- php: 7.2.0
- PUG_VERSION='^2.7'
- php: 7.2
env:
- LARAVEL_VERSION='5.4.*'
- PUG_VERSION='^3.0.0'
- php: 7.2.0
- PUG_VERSION='^3.0'
- php: 7.2
env:
- LARAVEL_VERSION='5.5.*'
- PUG_VERSION='2.7.1'
- php: 7.2.0
- PUG_VERSION='^2.7'
- php: 7.2
env:
- LARAVEL_VERSION='5.5.*'
- PUG_VERSION='^3.0.0'
- PUG_VERSION='^3.0'
- php: 7.3
env:
- LARAVEL_VERSION='4.2.*'
- PUG_VERSION='^2.7'
- php: 7.3
env:
- LARAVEL_VERSION='4.2.*'
- PUG_VERSION='^3.0'
- php: 7.3
env:
- LARAVEL_VERSION='5.4.*'
- PUG_VERSION='^2.7'
- php: 7.3
env:
- LARAVEL_VERSION='5.4.*'
- PUG_VERSION='^3.0'
- php: 7.3
env:
- LARAVEL_VERSION='5.5.*'
- PUG_VERSION='^2.7'
- php: 7.3
env:
- LARAVEL_VERSION='5.5.*'
- PUG_VERSION='^3.0'
- php: 7.3
env:
- LARAVEL_VERSION='5.6.*'
- PUG_VERSION='^2.7'
- php: 7.3
env:
- LARAVEL_VERSION='5.6.*'
- PUG_VERSION='^3.0'
- php: 7.3
env:
- LARAVEL_VERSION='5.7.*'
- PUG_VERSION='^2.7'
- php: 7.3
env:
- LARAVEL_VERSION='5.7.*'
- PUG_VERSION='^3.0'
- php: 7.3
env:
- LARAVEL_VERSION='5.8.*'
- PUG_VERSION='^2.7'
- php: 7.3
env:
- LARAVEL_VERSION='5.8.*'
- PUG_VERSION='^3.0'
- php: hhvm
env:
- LARAVEL_VERSION='4.2.*'
- PUG_VERSION='2.7.1'
- PUG_VERSION='^2.7'
dist: trusty
sudo: required
- php: hhvm
env:
- LARAVEL_VERSION='4.2.*'
- PUG_VERSION='^3.0.0'
- PUG_VERSION='^3.0'
dist: trusty
sudo: required
- php: hhvm
env:
- LARAVEL_VERSION='5.4.*'
- PUG_VERSION='^2.7.1'
- PUG_VERSION='^2.7'
dist: trusty
sudo: required
- php: hhvm
env:
- LARAVEL_VERSION='5.4.*'
- PUG_VERSION='^3.0.0'
- PUG_VERSION='^3.0'
dist: trusty
sudo: required

Expand All @@ -147,10 +195,10 @@ script:
- vendor/bin/phpunit --verbose --coverage-text --coverage-clover=coverage.xml

after_script:
- if [[ $LARAVEL_VERSION == "5.4.*" && $PUG_VERSION == "^3.0.0" && ${TRAVIS_PHP_VERSION:0:3} == "5.6" ]]; then vendor/bin/test-reporter --coverage-report coverage.xml; fi;
- if [[ $LARAVEL_VERSION == "5.4.*" && $PUG_VERSION == "^3.0" && ${TRAVIS_PHP_VERSION:0:3} == "5.6" ]]; then vendor/bin/test-reporter --coverage-report coverage.xml; fi;

after_success:
- if [[ $LARAVEL_VERSION == "5.4.*" && $PUG_VERSION == "^3.0.0" && ${TRAVIS_PHP_VERSION:0:3} == "5.6" ]]; then bash <(curl -s https://codecov.io/bash); fi;
- if [[ $LARAVEL_VERSION == "5.4.*" && $PUG_VERSION == "^3.0" && ${TRAVIS_PHP_VERSION:0:3} == "5.6" ]]; then bash <(curl -s https://codecov.io/bash); fi;

addons:
code_climate:
Expand Down
16 changes: 12 additions & 4 deletions src/PugBladeCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ public function __construct(array $pugTarget, Filesystem $files, array $config,
$this->construct($pugTarget, $files, $config, $defaultCachePath);
}

protected function enableBladeDirectives()
{
/** @var \Illuminate\View\Compilers\BladeCompiler $blade */
$blade = Blade::getFacadeRoot();
if ($blade && method_exists($blade, 'getCustomDirectives')) {
foreach ($blade->getCustomDirectives() as $name => $directive) {
$this->directive($name, $directive);
}
}
}

/**
* Compile the view at the given path.
*
Expand All @@ -37,10 +48,7 @@ public function compile($path = null)
{
$app = Blade::getFacadeApplication();
if (isset($app['view'])) {
$blade = Blade::getFacadeRoot();
foreach ($blade->getCustomDirectives() as $name => $directive) {
$this->directive($name, $directive);
}
$this->enableBladeDirectives();
}
$this->footer = array();
$this->compileWith($path, array($this, 'compileString'));
Expand Down
24 changes: 18 additions & 6 deletions src/PugHandlerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,21 @@ public function extractPath($path)
return $path;
}

/**
* Returns the object the more appropriate to compile (\Pug\Pug with version < 3), \Phug\Compilser for >= 3.
*
* @return \Phug\CompilerInterface|Pug
*/
public function getCompiler()
{
$pug = $this->getPug();
if ($pug instanceof \Phug\Renderer) {
$pug = clone $pug->getCompiler();
}

return $pug;
}

/**
* Compile the view at the given path.
*
Expand All @@ -186,19 +201,16 @@ public function compileWith($path, callable $callback = null)
{
$path = $this->extractPath($path);
if ($this->cachePath) {
$pug = $this->getPug();
if (method_exists($pug, 'initCompiler')) {
$pug->initCompiler();
}
$pug = $this->getCompiler();
$compiled = $this->getCompiledPath($path);
$contents = $pug->compile($this->files->get($path), $path);
if ($callback) {
$contents = call_user_func($callback, $contents);
}
if ($pug instanceof \Phug\Renderer) {
if ($pug instanceof \Phug\Compiler) {
$this->files->put(
$compiled . '.imports.serialize.txt',
serialize($pug->getCompiler()->getCurrentImportPaths())
serialize($pug->getCurrentImportPaths())
);
}
$this->files->put($compiled, $contents);
Expand Down
25 changes: 8 additions & 17 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,18 +214,18 @@ public function getEngineResolver()
*
* @return void
*/
public function registerPugCompiler()
public function registerPugCompiler($subExtension = '')
{
// Add resolver
$this->getEngineResolver()->register('pug', function () {
return new CompilerEngine($this->app['Bkwld\LaravelPug\PugCompiler']);
$this->getEngineResolver()->register('pug' . $subExtension, function () use ($subExtension) {
return new CompilerEngine($this->app['Bkwld\LaravelPug\Pug' . ucfirst(ltrim($subExtension, '.')) . 'Compiler']);
});

// Add extensions
$this->app['view']->addExtension('pug', 'pug');
$this->app['view']->addExtension('pug.php', 'pug');
$this->app['view']->addExtension('jade', 'pug');
$this->app['view']->addExtension('jade.php', 'pug');
$this->app['view']->addExtension('pug' . $subExtension, 'pug' . $subExtension);
$this->app['view']->addExtension('pug' . $subExtension . '.php', 'pug' . $subExtension);
$this->app['view']->addExtension('jade' . $subExtension, 'pug' . $subExtension);
$this->app['view']->addExtension('jade' . $subExtension . '.php', 'pug' . $subExtension);
}

/**
Expand All @@ -235,16 +235,7 @@ public function registerPugCompiler()
*/
public function registerPugBladeCompiler()
{
// Add resolver
$this->getEngineResolver()->register('pug.blade', function () {
return new CompilerEngine($this->app['Bkwld\LaravelPug\PugBladeCompiler']);
});

// Add extensions
$this->app['view']->addExtension('pug.blade', 'pug.blade');
$this->app['view']->addExtension('pug.blade.php', 'pug.blade');
$this->app['view']->addExtension('jade.blade', 'jade.blade');
$this->app['view']->addExtension('jade.blade.php', 'jade.blade');
$this->registerPugCompiler('.blade');
}

/**
Expand Down
Loading

0 comments on commit 9eb4efc

Please sign in to comment.