From 549a1e907f8da1dc05dd80532e5f1166db1619f0 Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Thu, 11 May 2017 10:21:04 +0200 Subject: [PATCH] Adapt cache system --- src/PugBladeCompiler.php | 102 +++++++------ src/PugCompiler.php | 75 +++++---- src/ServiceProvider.php | 321 ++++++++++++++++++++------------------- 3 files changed, 271 insertions(+), 227 deletions(-) diff --git a/src/PugBladeCompiler.php b/src/PugBladeCompiler.php index 6c551dd..6511912 100644 --- a/src/PugBladeCompiler.php +++ b/src/PugBladeCompiler.php @@ -6,48 +6,64 @@ use Illuminate\Filesystem\Filesystem; use Pug\Pug; -class PugBladeCompiler extends BladeCompiler implements CompilerInterface { - - /** - * The MtHaml instance. - * - * @var Pug - */ - protected $pug; - - /** - * Create a new compiler instance. - * - * @param Pug $pug - * @param \Illuminate\Filesystem\Filesystem $files - * @param string $cachePath - * @return void - */ - public function __construct(Pug $pug, Filesystem $files, $cachePath) - { - $this->pug = $pug; - parent::__construct($files, $cachePath); - } - - /** - * Compile the view at the given path. - * - * @param string $path - * @return void - */ - public function compile($path) { - $this->footer = array(); - - if (is_null($this->cachePath)) return; - - // 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); - } +class PugBladeCompiler extends BladeCompiler implements CompilerInterface +{ + /** + * The MtHaml instance. + * + * @var Pug + */ + protected $pug; + + /** + * Create a new compiler instance. + * + * @param Pug $pug + * @param \Illuminate\Filesystem\Filesystem $files + * @param string $cachePath + * @return void + */ + public function __construct(Pug $pug, Filesystem $files) + { + $this->pug = $pug; + $this->files = $files; + $this->cachePath = $pug->getOption('cache'); + if (!is_string($this->cachePath)) { + $this->cachePath = $pug->getOption('defaultCache'); + } + } + + /** + * Determine if the view at the given path is expired. + * + * @param string $path + * @return bool + */ + public function isExpired($path) + { + return !$this->pug->getOption('cache') || parent::isExpired($path); + } + + /** + * Compile the view at the given path. + * + * @param string $path + * @return void + */ + public function compile($path) + { + $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); + } + } } diff --git a/src/PugCompiler.php b/src/PugCompiler.php index 7e45027..b790921 100644 --- a/src/PugCompiler.php +++ b/src/PugCompiler.php @@ -6,37 +6,54 @@ use Illuminate\Filesystem\Filesystem; use Pug\Pug; -class PugCompiler extends Compiler implements CompilerInterface { +class PugCompiler extends Compiler implements CompilerInterface +{ + /** + * @var Pug + */ + protected $pug; - /** - * @var Pug - */ - protected $pug; + /** + * Create a new compiler instance. + * + * @param Pug $pug + * @param Illuminate\Filesystem\Filesystem $files + * @param string $cachePath + * @return void + */ + public function __construct(Pug $pug, Filesystem $files) + { + $this->pug = $pug; + $this->files = $files; + $this->cachePath = $pug->getOption('cache'); + if (!is_string($this->cachePath)) { + $this->cachePath = $pug->getOption('defaultCache'); + } + } - /** - * Create a new compiler instance. - * - * @param Pug $pug - * @param Illuminate\Filesystem\Filesystem $files - * @param string $cachePath - * @return void - */ - public function __construct(Pug $pug, Filesystem $files, $cachePath) - { - $this->pug = $pug; - parent::__construct($files, $cachePath); - } + /** + * Determine if the view at the given path is expired. + * + * @param string $path + * @return bool + */ + public function isExpired($path) + { + return !$this->pug->getOption('cache') || parent::isExpired($path); + } - /** - * Compile the view at the given path. - * - * @param string $path - * @return void - */ - public function compile($path) { - if (is_null($this->cachePath)) return; - $contents = $this->pug->compile($this->files->get($path), $path); - $this->files->put($this->getCompiledPath($path), $contents); - } + /** + * Compile the view at the given path. + * + * @param string $path + * @return void + */ + public function compile($path) + { + if ($this->cachePath) { + $contents = $this->pug->compile($this->files->get($path), $path); + $this->files->put($this->getCompiledPath($path), $contents); + } + } } diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index fc88b40..e052456 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -4,160 +4,171 @@ use Pug\Pug; use Illuminate\View\Engines\CompilerEngine; -class ServiceProvider extends \Illuminate\Support\ServiceProvider { - - /** - * Get the major Laravel version number - * - * @return integer - */ - public function version() { - $app = $this->app; - return intval($app::VERSION); - } - - /** - * Register the service provider. - * - * @return void - */ - public function register() { - - // Version specific registering - if ($this->version() == 5) $this->registerLaravel5(); - - // Determine the cache dir - $cache_dir = storage_path($this->version() == 5 ? '/framework/views' : '/views'); - - // Bind the package-configued Pug instance - $this->app->singleton('laravel-pug.pug', function($app) { - $config = $this->getConfig(); - return new Pug($config); - }); - - // Bind the Pug compiler - $this->app->singleton('Bkwld\LaravelPug\PugCompiler', function($app) use ($cache_dir) { - return new PugCompiler($app['laravel-pug.pug'], $app['files'], $cache_dir); - }); - - // Bind the Pug Blade compiler - $this->app->singleton('Bkwld\LaravelPug\PugBladeCompiler', function($app) use ($cache_dir) { - return new PugBladeCompiler($app['laravel-pug.pug'], $app['files'], $cache_dir); - }); - - } - - /** - * Register specific logic for Laravel 5. Merges package config with user config - * - * @return void - */ - public function registerLaravel5() { - $this->mergeConfigFrom(__DIR__.'/../config/config.php', 'laravel-pug'); - } - - /** - * Bootstrap the application events. - * - * @return void - */ - public function boot() { - - // Version specific booting - switch($this->version()) { - case 4: $this->bootLaravel4(); break; - case 5: $this->bootLaravel5(); break; - default: throw new Exception('Unsupported Laravel version'); - } - - // Register compilers - $this->registerPugCompiler(); - $this->registerPugBladeCompiler(); - } - - /** - * Boot specific logic for Laravel 4. Tells Laravel about the package for auto - * namespacing of config files - * - * @return void - */ - public function bootLaravel4() { - $this->package('bkwld/laravel-pug'); - } - - /** - * Boot specific logic for Laravel 5. Registers the config file for publishing - * to app directory - * - * @return void - */ - public function bootLaravel5() { - $this->publishes([ - __DIR__.'/../config/config.php' => config_path('laravel-pug.php') - ], 'laravel-pug'); - } - - /** - * Register the regular Pug compiler - * - * @return void - */ - public function registerPugCompiler() { - - // Add resolver - $this->app['view.engine.resolver']->register('pug', function() { - return new CompilerEngine($this->app['Bkwld\LaravelPug\PugCompiler']); - }); - - // 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'); - } - - /** - * Register the blade compiler compiler - * - * @return void - */ - public function registerPugBladeCompiler() { - - // Add resolver - $this->app['view.engine.resolver']->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'); - } - - /** - * Get the configuration, which is keyed differently in L5 vs l4 - * - * @return array - */ - public function getConfig() { - $key = $this->version() == 5 ? 'laravel-pug' : 'laravel-pug::config'; - return $this->app->make('config')->get($key); - } - - - - /** - * Get the services provided by the provider. - * - * @return array - */ - public function provides() { - return array( - 'Bkwld\LaravelPug\PugCompiler', - 'Bkwld\LaravelPug\PugBladeCompiler', - 'laravel-pug.pug', - ); - } +class ServiceProvider extends \Illuminate\Support\ServiceProvider +{ + /** + * Get the major Laravel version number + * + * @return integer + */ + public function version() + { + $app = $this->app; + + return intval($app::VERSION); + } + + /** + * Register the service provider. + * + * @return void + */ + public function register() + { + // Version specific registering + if ($this->version() == 5) { + $this->registerLaravel5(); + } + + // Bind the package-configued Pug instance + $this->app->singleton('laravel-pug.pug', function($app) { + $config = $this->getConfig(); + $pug = new Pug($config); + // Determine the cache dir if not configured + $pug->setCustomOption( + 'defaultCache', + storage_path($this->version() == 5 ? '/framework/views' : '/views') + ); + + return $pug; + }); + + // Bind the Pug compiler + $this->app->singleton('Bkwld\LaravelPug\PugCompiler', function($app) { + return new PugCompiler($app['laravel-pug.pug'], $app['files']); + }); + + // Bind the Pug Blade compiler + $this->app->singleton('Bkwld\LaravelPug\PugBladeCompiler', function($app) { + return new PugBladeCompiler($app['laravel-pug.pug'], $app['files']); + }); + } + + /** + * Register specific logic for Laravel 5. Merges package config with user config + * + * @return void + */ + public function registerLaravel5() + { + $this->mergeConfigFrom(__DIR__.'/../config/config.php', 'laravel-pug'); + } + + /** + * Bootstrap the application events. + * + * @return void + */ + public function boot() + { + // Version specific booting + switch($this->version()) { + case 4: $this->bootLaravel4(); break; + case 5: $this->bootLaravel5(); break; + default: throw new Exception('Unsupported Laravel version'); + } + + // Register compilers + $this->registerPugCompiler(); + $this->registerPugBladeCompiler(); + } + + /** + * Boot specific logic for Laravel 4. Tells Laravel about the package for auto + * namespacing of config files + * + * @return void + */ + public function bootLaravel4() + { + $this->package('bkwld/laravel-pug'); + } + + /** + * Boot specific logic for Laravel 5. Registers the config file for publishing + * to app directory + * + * @return void + */ + public function bootLaravel5() + { + $this->publishes([ + __DIR__.'/../config/config.php' => config_path('laravel-pug.php') + ], 'laravel-pug'); + } + + /** + * Register the regular Pug compiler + * + * @return void + */ + public function registerPugCompiler() + { + // Add resolver + $this->app['view.engine.resolver']->register('pug', function() { + return new CompilerEngine($this->app['Bkwld\LaravelPug\PugCompiler']); + }); + + // 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'); + } + + /** + * Register the blade compiler compiler + * + * @return void + */ + public function registerPugBladeCompiler() + { + // Add resolver + $this->app['view.engine.resolver']->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'); + } + + /** + * Get the configuration, which is keyed differently in L5 vs l4 + * + * @return array + */ + public function getConfig() + { + $key = $this->version() == 5 ? 'laravel-pug' : 'laravel-pug::config'; + + return $this->app->make('config')->get($key); + } + + /** + * Get the services provided by the provider. + * + * @return array + */ + public function provides() + { + return array( + 'Bkwld\LaravelPug\PugCompiler', + 'Bkwld\LaravelPug\PugBladeCompiler', + 'laravel-pug.pug', + ); + } }