Skip to content

Commit

Permalink
Cache imports handle (#41)
Browse files Browse the repository at this point in the history
* Handle imports last modified times for cache expiration
* Enable include cache optimization only for Phug engine
* Refactor compilers using traits
* Send coverage in only one build
* Isolate import test in a dedicated method
  • Loading branch information
kylekatarnls authored Nov 29, 2017
1 parent 6f15913 commit f6d64e1
Show file tree
Hide file tree
Showing 8 changed files with 311 additions and 169 deletions.
36 changes: 18 additions & 18 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,103 +16,103 @@ matrix:
- php: 5.5
env:
- LARAVEL_VERSION='4.2.*'
- PUG_VERSION='^3.0.0@beta'
- PUG_VERSION='^3.0.0'
- php: 5.5
env:
- LARAVEL_VERSION='5.2.*'
- PUG_VERSION='^2.7.1'
- php: 5.5
env:
- LARAVEL_VERSION='5.2.*'
- PUG_VERSION='^3.0.0@beta'
- PUG_VERSION='^3.0.0'
- php: 5.6
env:
- LARAVEL_VERSION='4.2.*'
- PUG_VERSION='^2.7.1'
- php: 5.6
env:
- LARAVEL_VERSION='4.2.*'
- PUG_VERSION='^3.0.0@beta'
- PUG_VERSION='^3.0.0'
- php: 5.6
env:
- LARAVEL_VERSION='5.4.*'
- PUG_VERSION='^2.7.1'
- php: 5.6
env:
- LARAVEL_VERSION='5.4.*'
- PUG_VERSION='^3.0.0@beta'
- PUG_VERSION='^3.0.0'
- php: 7.0
env:
- LARAVEL_VERSION='4.2.*'
- PUG_VERSION='^2.7.1'
- php: 7.0
env:
- LARAVEL_VERSION='4.2.*'
- PUG_VERSION='^3.0.0@beta'
- PUG_VERSION='^3.0.0'
- php: 7.0
env:
- LARAVEL_VERSION='5.4.*'
- PUG_VERSION='^2.7.1'
- php: 7.0
env:
- LARAVEL_VERSION='5.4.*'
- PUG_VERSION='^3.0.0@beta'
- PUG_VERSION='^3.0.0'
- php: 7.0
env:
- LARAVEL_VERSION='5.5.*'
- PUG_VERSION='^2.7.1'
- php: 7.0
env:
- LARAVEL_VERSION='5.5.*'
- PUG_VERSION='^3.0.0@beta'
- PUG_VERSION='^3.0.0'
- php: 7.1
env:
- LARAVEL_VERSION='4.2.*'
- PUG_VERSION='^2.7.1'
- php: 7.1
env:
- LARAVEL_VERSION='4.2.*'
- PUG_VERSION='^3.0.0@beta'
- PUG_VERSION='^3.0.0'
- php: 7.1
env:
- LARAVEL_VERSION='5.4.*'
- PUG_VERSION='^2.7.1'
- php: 7.1
env:
- LARAVEL_VERSION='5.4.*'
- PUG_VERSION='^3.0.0@beta'
- PUG_VERSION='^3.0.0'
- php: 7.1
env:
- LARAVEL_VERSION='5.5.*'
- PUG_VERSION='^2.7.1'
- php: 7.1
env:
- LARAVEL_VERSION='5.5.*'
- PUG_VERSION='^3.0.0@beta'
- PUG_VERSION='^3.0.0'
- php: 7.2.0RC5
env:
- LARAVEL_VERSION='4.2.*'
- PUG_VERSION='^2.7.1'
- php: 7.2.0RC5
env:
- LARAVEL_VERSION='4.2.*'
- PUG_VERSION='^3.0.0@beta'
- PUG_VERSION='^3.0.0'
- php: 7.2.0RC5
env:
- LARAVEL_VERSION='5.4.*'
- PUG_VERSION='^2.7.1'
- php: 7.2.0RC5
env:
- LARAVEL_VERSION='5.4.*'
- PUG_VERSION='^3.0.0@beta'
- PUG_VERSION='^3.0.0'
- php: 7.2.0RC5
env:
- LARAVEL_VERSION='5.5.*'
- PUG_VERSION='2.7.1'
- php: 7.2.0RC5
env:
- LARAVEL_VERSION='5.5.*'
- PUG_VERSION='^3.0.0@beta'
- PUG_VERSION='^3.0.0'
- php: hhvm
env:
- LARAVEL_VERSION='4.2.*'
Expand All @@ -122,7 +122,7 @@ matrix:
- php: hhvm
env:
- LARAVEL_VERSION='4.2.*'
- PUG_VERSION='^3.0.0@beta'
- PUG_VERSION='^3.0.0'
dist: trusty
sudo: required
- php: hhvm
Expand All @@ -134,23 +134,23 @@ matrix:
- php: hhvm
env:
- LARAVEL_VERSION='5.4.*'
- PUG_VERSION='^3.0.0@beta'
- PUG_VERSION='^3.0.0'
dist: trusty
sudo: required

install:
- travis_retry composer self-update
- if [ "LARAVEL_VERSION" != "" ]; then travis_retry php tests/setDependenciesVersions.php $LARAVEL_VERSION $PUG_VERSION; fi;
- travis_retry php tests/setDependenciesVersions.php $LARAVEL_VERSION $PUG_VERSION
- travis_retry composer update --no-interaction --prefer-stable

script:
- vendor/bin/phpunit --verbose --coverage-text --coverage-clover=coverage.xml

after_script:
- vendor/bin/test-reporter --coverage-report coverage.xml
- 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;

after_success:
- bash <(curl -s https://codecov.io/bash)
- 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;

addons:
code_climate:
Expand Down
79 changes: 5 additions & 74 deletions src/PugBladeCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,83 +2,34 @@

namespace Bkwld\LaravelPug;

// Dependencies
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Facades\Blade;
use Illuminate\View\Compilers\BladeCompiler;
use Illuminate\View\Compilers\CompilerInterface;
use InvalidArgumentException;
use Pug\Pug;

class PugBladeCompiler extends BladeCompiler implements CompilerInterface
{
/**
* The MtHaml instance.
*
* @var Pug
*/
protected $pug;
use PugHandlerTrait;

/**
* Create a new compiler instance.
*
* @param Pug $pug
* @param Filesystem $files
* @param string $cachePath
*/
public function __construct(Pug $pug, Filesystem $files)
{
$this->pug = $pug;
$cachePath = $this->getOption('cache');
if (!is_string($cachePath)) {
$cachePath = $this->getOption('defaultCache');
}
parent::__construct($files, $cachePath);
}

/**
* Get an option from pug engine or default value.
*
* @param string $name
* @param null $default
*
* @return mixed|null
*/
public function getOption($name, $default = null)
{
if (method_exists($this->pug, 'hasOption') && !$this->pug->hasOption($name)) {
return $default;
}

return $this->pug->getOption($name);
}

/**
* @param string $cachePath
*/
public function setCachePath($cachePath)
{
$this->cachePath = $cachePath;
$this->pug->setOption('cache', $cachePath);
}

/**
* Determine if the view at the given path is expired.
*
* @param string $path
*
* @return bool
*/
public function isExpired($path)
{
return !$this->getOption('cache') || parent::isExpired($path);
parent::__construct($files, $this->getCachePath($pug));
}

/**
* Compile the view at the given path.
*
* @param string $path
*
* @throws \InvalidArgumentException
*
* @return void
*/
public function compile($path = null)
Expand All @@ -90,27 +41,7 @@ public function compile($path = null)
$this->directive($name, $directive);
}
}
if ($path && method_exists($this, 'setPath')) {
$this->setPath($path);
}
if (!$path && method_exists($this, 'getPath')) {
$path = $this->getPath();
}
if (!$path) {
throw new InvalidArgumentException('Missing path argument.');
}

$this->footer = array();

if ($this->cachePath) {
// First compile the Pug syntax
$contents = $this->pug->compile($this->files->get($path), $path);

// Then the Blade syntax
$contents = $this->compileString($contents);

// Save
$this->files->put($this->getCompiledPath($path), $contents);
}
$this->compileWith($path, array($this, 'compileString'));
}
}
69 changes: 5 additions & 64 deletions src/PugCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,96 +2,37 @@

namespace Bkwld\LaravelPug;

// Dependencies
use Illuminate\Filesystem\Filesystem;
use Illuminate\View\Compilers\Compiler;
use Illuminate\View\Compilers\CompilerInterface;
use InvalidArgumentException;
use Pug\Pug;

class PugCompiler extends Compiler implements CompilerInterface
{
/**
* @var Pug
*/
protected $pug;
use PugHandlerTrait;

/**
* Create a new compiler instance.
*
* @param Pug $pug
* @param Filesystem $files
* @param string $cachePath
*/
public function __construct(Pug $pug, Filesystem $files)
{
$this->pug = $pug;
$cachePath = $this->getOption('cache');
if (!is_string($cachePath)) {
$cachePath = $this->getOption('defaultCache');
}
parent::__construct($files, $cachePath);
}

/**
* Get an option from pug engine or default value.
*
* @param string $name
* @param null $default
*
* @return mixed|null
*/
public function getOption($name, $default = null)
{
if (method_exists($this->pug, 'hasOption') && !$this->pug->hasOption($name)) {
return $default;
}

return $this->pug->getOption($name);
}

/**
* @param string $cachePath
*/
public function setCachePath($cachePath)
{
$this->cachePath = $cachePath;
$this->pug->setOption('cache', $cachePath);
}

/**
* Determine if the view at the given path is expired.
*
* @param string $path
*
* @return bool
*/
public function isExpired($path)
{
return !$this->getOption('cache') || parent::isExpired($path);
parent::__construct($files, $this->getCachePath($pug));
}

/**
* Compile the view at the given path.
*
* @param string $path
*
* @throws \InvalidArgumentException
*
* @return void
*/
public function compile($path)
{
if ($path && method_exists($this, 'setPath')) {
$this->setPath($path);
}
if (!$path && method_exists($this, 'getPath')) {
$path = $this->getPath();
}
if (!$path) {
throw new InvalidArgumentException('Missing path argument.');
}
if ($this->cachePath) {
$contents = $this->pug->compile($this->files->get($path), $path);
$this->files->put($this->getCompiledPath($path), $contents);
}
$this->compileWith($path);
}
}
Loading

0 comments on commit f6d64e1

Please sign in to comment.