Skip to content

Commit

Permalink
Lazy load (#44)
Browse files Browse the repository at this point in the history
* Lazy load the pug instance

* Fix compatibility

* Add default cache path

* Add default cache path fallback

* Expiration test no longer need pug instance

* Use the stable PHP 7.2

* Wait longer for composer to update

* Split code into smaller functions

* Fix code style and long lines

* Add unit tests

* Cleanup whitspace

* Fix getOption Pug-php 2 compatibility

* Add coverage annotation
  • Loading branch information
kylekatarnls authored Jan 30, 2018
1 parent 7793e90 commit 3dc5db5
Show file tree
Hide file tree
Showing 10 changed files with 261 additions and 95 deletions.
14 changes: 7 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,27 +89,27 @@ matrix:
env:
- LARAVEL_VERSION='5.5.*'
- PUG_VERSION='^3.0.0'
- php: 7.2.0RC5
- php: 7.2.0
env:
- LARAVEL_VERSION='4.2.*'
- PUG_VERSION='^2.7.1'
- php: 7.2.0RC5
- php: 7.2.0
env:
- LARAVEL_VERSION='4.2.*'
- PUG_VERSION='^3.0.0'
- php: 7.2.0RC5
- php: 7.2.0
env:
- LARAVEL_VERSION='5.4.*'
- PUG_VERSION='^2.7.1'
- php: 7.2.0RC5
- php: 7.2.0
env:
- LARAVEL_VERSION='5.4.*'
- PUG_VERSION='^3.0.0'
- php: 7.2.0RC5
- php: 7.2.0
env:
- LARAVEL_VERSION='5.5.*'
- PUG_VERSION='2.7.1'
- php: 7.2.0RC5
- php: 7.2.0
env:
- LARAVEL_VERSION='5.5.*'
- PUG_VERSION='^3.0.0'
Expand Down Expand Up @@ -141,7 +141,7 @@ matrix:
install:
- travis_retry composer self-update
- travis_retry php tests/setDependenciesVersions.php $LARAVEL_VERSION $PUG_VERSION
- travis_retry composer update --no-interaction --prefer-stable
- travis_wait 30 travis_retry composer update --no-interaction

script:
- vendor/bin/phpunit --verbose --coverage-text --coverage-clover=coverage.xml
Expand Down
9 changes: 5 additions & 4 deletions src/PugBladeCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Illuminate\Support\Facades\Blade;
use Illuminate\View\Compilers\BladeCompiler;
use Illuminate\View\Compilers\CompilerInterface;
use Pug\Pug;

class PugBladeCompiler extends BladeCompiler implements CompilerInterface
{
Expand All @@ -15,12 +14,14 @@ class PugBladeCompiler extends BladeCompiler implements CompilerInterface
/**
* Create a new compiler instance.
*
* @param Pug $pug
* @param array $pugTarget
* @param Filesystem $files
* @param array $config
* @param string $defaultCachePath
*/
public function __construct(Pug $pug, Filesystem $files)
public function __construct(array $pugTarget, Filesystem $files, array $config, $defaultCachePath = null)
{
parent::__construct($files, $this->getCachePath($pug));
$this->construct($pugTarget, $files, $config, $defaultCachePath);
}

/**
Expand Down
9 changes: 5 additions & 4 deletions src/PugCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Illuminate\Filesystem\Filesystem;
use Illuminate\View\Compilers\Compiler;
use Illuminate\View\Compilers\CompilerInterface;
use Pug\Pug;

class PugCompiler extends Compiler implements CompilerInterface
{
Expand All @@ -14,12 +13,14 @@ class PugCompiler extends Compiler implements CompilerInterface
/**
* Create a new compiler instance.
*
* @param Pug $pug
* @param array $pugTarget
* @param Filesystem $files
* @param array $config
* @param string $defaultCachePath
*/
public function __construct(Pug $pug, Filesystem $files)
public function __construct(array $pugTarget, Filesystem $files, array $config, $defaultCachePath = null)
{
parent::__construct($files, $this->getCachePath($pug));
$this->construct($pugTarget, $files, $config, $defaultCachePath);
}

/**
Expand Down
78 changes: 65 additions & 13 deletions src/PugHandlerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,71 @@

namespace Bkwld\LaravelPug;

use Illuminate\Filesystem\Filesystem;
use InvalidArgumentException;
use Pug\Pug;

trait PugHandlerTrait
{
/**
* @var array
*/
protected $pugTarget;

/**
* @var Pug
*/
protected $pug;

/**
* Set pug instance and returns cache path.
* Common pug compiler constructor.
*
* @param Pug $pug
* @param array $pugTarget
* @param Filesystem $files
* @param array $config
*/
public function construct(array $pugTarget, Filesystem $files, array $config, $defaultCachePath = null)
{
$this->pugTarget = $pugTarget;
$cachePath = null;
foreach (array('cache_dir', 'cache', 'defaultCache') as $name) {
if (isset($config[$name])) {
$cachePath = $config[$name];
break;
}
}
if (!$cachePath) {
$cachePath = $defaultCachePath ?: $this->getCachePath();
}

parent::__construct($files, $cachePath);
}

/**
* Lazy load Pug and return the instance.
*
* @return Pug
*/
public function getPug()
{
if (!$this->pug) {
$this->pug = $this->pugTarget[0][$this->pugTarget[1]];
}

return $this->pug;
}

/**
* Returns cache path.
*
* @return string $cachePath
*/
public function getCachePath(Pug $pug)
public function getCachePath()
{
$this->pug = $pug;
if ($this->cachePath) {
return $this->cachePath;
}

$cachePath = $this->getOption('cache');

return is_string($cachePath) ? $cachePath : $this->getOption('defaultCache');
Expand All @@ -37,11 +82,17 @@ public function getCachePath(Pug $pug)
*/
public function getOption($name, $default = null)
{
if (method_exists($this->pug, 'hasOption') && !$this->pug->hasOption($name)) {
$pug = $this->getPug();

try {
if (method_exists($pug, 'hasOption') && !$pug->hasOption($name)) {
throw new \InvalidArgumentException('invalid option');
}

return $pug->getOption($name);
} catch (\InvalidArgumentException $exception) {
return $default;
}

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

/**
Expand All @@ -50,7 +101,7 @@ public function getOption($name, $default = null)
public function setCachePath($cachePath)
{
$this->cachePath = $cachePath;
$this->pug->setOption('cache', $cachePath);
$this->getPug()->setOption('cache', $cachePath);
}

/**
Expand Down Expand Up @@ -90,11 +141,11 @@ private function hasExpiredImport($path)
*/
public function isExpired($path)
{
if (!$this->getOption('cache') || parent::isExpired($path)) {
if (!$this->cachePath || parent::isExpired($path)) {
return true;
}

return $this->pug instanceof \Phug\Renderer && $this->hasExpiredImport($path);
return is_subclass_of('\Pug\Pug', '\Phug\Renderer') && $this->hasExpiredImport($path);
}

/**
Expand Down Expand Up @@ -135,15 +186,16 @@ public function compileWith($path, callable $callback = null)
{
$path = $this->extractPath($path);
if ($this->cachePath) {
$pug = $this->getPug();
$compiled = $this->getCompiledPath($path);
$contents = $this->pug->compile($this->files->get($path), $path);
$contents = $pug->compile($this->files->get($path), $path);
if ($callback) {
$contents = call_user_func($callback, $contents);
}
if ($this->pug instanceof \Phug\Renderer) {
if ($pug instanceof \Phug\Renderer) {
$this->files->put(
$compiled . '.imports.serialize.txt',
serialize($this->pug->getCompiler()->getCurrentImportPaths())
serialize($pug->getCompiler()->getCurrentImportPaths())
);
}
$this->files->put($compiled, $contents);
Expand Down
127 changes: 83 additions & 44 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@

class ServiceProvider extends \Illuminate\Support\ServiceProvider
{
/**
* @var Assets
*/
protected $assets;

protected function setDefaultOption(Pug $pug, $name, $value)
{
if (method_exists($pug, 'hasOption') && !$pug->hasOption($name)) {
Expand All @@ -26,6 +31,69 @@ protected function setDefaultOption(Pug $pug, $name, $value)
// @codeCoverageIgnoreEnd
}

protected function getDefaultCache()
{
return storage_path($this->version() >= 5 ? '/framework/views' : '/views');
}

protected function getAssetsDirectories()
{
return array_map(function ($params) {
list($function, $arg) = $params;

return function_exists($function) ? call_user_func($function, $arg) : null;
}, array(
array('resource_path', 'assets'),
array('app_path', 'views/assets'),
array('app_path', 'assets'),
array('app_path', 'views'),
array('app_path', ''),
array('base_path', ''),
));
}

protected function getPugEngine()
{
$config = $this->getConfig();
$pug = new Pug($config);
$this->assets = new Assets($pug);
$getEnv = array('App', 'environment');
$this->assets->setEnvironment(is_callable($getEnv) ? call_user_func($getEnv) : 'production');

// Determine the cache dir if not configured
$this->setDefaultOption($pug, 'defaultCache', array($this, 'getDefaultCache'));

// Determine assets input directory
$this->setDefaultOption($pug, 'assetDirectory', array($this, 'getAssetsDirectories'));

// Determine assets output directory
$this->setDefaultOption($pug, 'outputDirectory', array($this, 'getOutputDirectory'));

return $pug;
}

protected function getPugAssets()
{
return $this->app['laravel-pug.pug'] ? $this->assets : null;
}

protected function getOutputDirectory()
{
return function_exists('public_path') ? public_path() : null;
}

protected function getCompilerCreator($compilerClass)
{
return function ($app) use ($compilerClass) {
return new $compilerClass(
array($app, 'laravel-pug.pug'),
$app['files'],
$this->getConfig(),
$this->getDefaultCache()
);
};
}

/**
* Get the major Laravel version number.
*
Expand All @@ -51,56 +119,27 @@ public function register()
$this->registerLaravel5();
}

// Bind the package-configued Pug instance
// Bind the pug assets module
$this->app->singleton('laravel-pug.pug-assets', function () {
return $this->getPugAssets();
});

// Bind the package-configured Pug instance
$this->app->singleton('laravel-pug.pug', function () {
$config = $this->getConfig();
$pug = new Pug($config);
$assets = new Assets($pug);
$getEnv = array('App', 'environment');
$assets->setEnvironment(is_callable($getEnv) ? call_user_func($getEnv) : 'production');

$this->app->singleton('laravel-pug.pug-assets', function () use ($assets) {
return $assets;
});

// Determine the cache dir if not configured
$this->setDefaultOption($pug, 'defaultCache', function () {
return storage_path($this->version() >= 5 ? '/framework/views' : '/views');
});

// Determine assets input directory
$this->setDefaultOption($pug, 'assetDirectory', function () {
return array_map(function ($params) {
list($function, $arg) = $params;

return function_exists($function) ? call_user_func($function, $arg) : null;
}, array(
array('resource_path', 'assets'),
array('app_path', 'views/assets'),
array('app_path', 'assets'),
array('app_path', 'views'),
array('app_path', ''),
array('base_path', ''),
));
});

// Determine assets output directory
$this->setDefaultOption($pug, 'outputDirectory', function () {
return function_exists('public_path') ? public_path() : null;
});

return $pug;
return $this->getPugEngine();
});

// Bind the Pug compiler
$this->app->singleton('Bkwld\LaravelPug\PugCompiler', function ($app) {
return new PugCompiler($app['laravel-pug.pug'], $app['files']);
});
$this->app->singleton(
'Bkwld\LaravelPug\PugCompiler',
$this->getCompilerCreator('\Bkwld\LaravelPug\PugCompiler')
);

// Bind the Pug Blade compiler
$this->app->singleton('Bkwld\LaravelPug\PugBladeCompiler', function ($app) {
return new PugBladeCompiler($app['laravel-pug.pug'], $app['files']);
});
$this->app->singleton(
'Bkwld\LaravelPug\PugBladeCompiler',
$this->getCompilerCreator('\Bkwld\LaravelPug\PugBladeCompiler')
);
}

/**
Expand Down
Loading

0 comments on commit 3dc5db5

Please sign in to comment.