From ccd9110fa6360e67a6f5cd14b8073616849ecb67 Mon Sep 17 00:00:00 2001 From: oncletom Date: Tue, 9 Oct 2012 18:41:44 +0200 Subject: [PATCH 1/3] Fixed filename case --- lib/{GarbageCollector.class.php => Garbagecollector.class.php} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename lib/{GarbageCollector.class.php => Garbagecollector.class.php} (100%) diff --git a/lib/GarbageCollector.class.php b/lib/Garbagecollector.class.php similarity index 100% rename from lib/GarbageCollector.class.php rename to lib/Garbagecollector.class.php From 5724a3f8ed6454d0b4754f81572ec1f7a637749c Mon Sep 17 00:00:00 2001 From: oncletom Date: Tue, 9 Oct 2012 19:10:05 +0200 Subject: [PATCH 2/3] Upgraded the rebuild strategy --- lib/Configuration.class.php | 65 +++++++++++++++++++++++++++++++------ 1 file changed, 55 insertions(+), 10 deletions(-) diff --git a/lib/Configuration.class.php b/lib/Configuration.class.php index 7fe0c84..f66fada 100644 --- a/lib/Configuration.class.php +++ b/lib/Configuration.class.php @@ -12,12 +12,24 @@ class WPLessConfiguration extends WPPluginToolkitConfiguration */ const VERSION = '1.5-dev'; + + /** + * Current compilation strategy + * + * @since 1.5 * @protected - * @deprecated - * @var bool + * @var string + */ + protected $compilation_strategy = 'deep'; + + /** + * Available compilation strategies + * + * @since 1.5 + * @var array */ - protected $alwaysRecompile = false; + protected $compilation_strategies = array('legacy', 'always', 'deep'); /** * Time to live before pruning CSS cache @@ -34,23 +46,56 @@ protected function configure() protected function configureOptions() { - $this->alwaysRecompile((defined('WP_DEBUG') && WP_DEBUG) || (defined('WP_LESS_ALWAYS_RECOMPILE') && WP_LESS_ALWAYS_RECOMPILE)); + if (defined('WP_LESS_COMPILATION') && WP_LESS_COMPILATION) + { + $this->setCompilationStrategy(WP_LESS_COMPILATION); + } + + //previous setting can be overridden for special reasons (dev/prod for example) + if ((defined('WP_DEBUG') && WP_DEBUG) || (defined('WP_LESS_ALWAYS_RECOMPILE') && WP_LESS_ALWAYS_RECOMPILE)) + { + $this->setCompilationStrategy('always'); + } } + /** + * Current compilation strategy + * + * @api + * @since 1.5 + * @return string Active compilation strategy + */ + public function getCompilationStrategy() + { + return $this->compilation_strategy; + } + + /** + * Always recompile + * + * @since 1.5 + * @return bool + */ + public function alwaysRecompile() + { + return $this->compilation_strategy === 'always'; + } + /** * Set compilation strategy * - * @param $bFlag bool - * @return bool Actual compilation "strategy" + * @api + * @since 1.5 + * @param $strategy string Actual compilation "strategy" */ - public function alwaysRecompile($bFlag = null) + public function setCompilationStrategy($strategy) { - if (!is_null($bFlag)) + if (!in_array($strategy, $this->compilation_strategies)) { - $this->alwaysRecompile = !!$bFlag; + throw new WPLessException('Unknown compile strategy: ['.$strategy.'] provided.'); } - return $this->alwaysRecompile; + $this->compilation_strategy = $strategy; } /** From d99eae0115024121016a50e3340c79639f004cb6 Mon Sep 17 00:00:00 2001 From: oncletom Date: Wed, 10 Oct 2012 12:07:50 +0200 Subject: [PATCH 3/3] Cache compiler --- lib/Compiler.class.php | 76 +++++++++++++++++++++++++++++------------- lib/Plugin.class.php | 14 +++++--- 2 files changed, 63 insertions(+), 27 deletions(-) diff --git a/lib/Compiler.class.php b/lib/Compiler.class.php index 5d27eab..1bcda13 100644 --- a/lib/Compiler.class.php +++ b/lib/Compiler.class.php @@ -55,31 +55,61 @@ public function setVariable($name, $value) $this->registeredVars[ $name ] = $value; } - /** - * Process a WPLessStylesheet - * - * This logic was previously held in WPLessStylesheet::save() - * - * @since 1.4.2 - */ - public function saveStylesheet(WPLessStylesheet $stylesheet) - { - wp_mkdir_p(dirname($stylesheet->getTargetPath())); + /** + * Smart caching and retrieval of a tree of @import LESS stylesheets + * + * @since 1.5 + * @param WPLessStylesheet $stylesheet + * @param bool $force + */ + public function cacheStylesheet(WPLessStylesheet $stylesheet, $force = false) + { + $cache_name = 'wp_less_compiled_'.md5($stylesheet->getSourcePath()); + $compiled_cache = get_transient($cache_name); - try - { - do_action('wp-less_stylesheet_save_pre', $stylesheet, $this->getVariables()); + $compiled_cache = $this->cachedCompile($compiled_cache ? $compiled_cache : $stylesheet->getSourcePath(), $force); - $this->compileFile($stylesheet->getSourcePath(), $stylesheet->getTargetPath()); + // saving compiled stuff + if (isset($compiled_cache['compiled']) && $compiled_cache['compiled']) + { + $this->saveStylesheet($stylesheet, $compiled_cache['compiled']); - chmod($stylesheet->getTargetPath(), 0666); + $compiled_cache['compiled'] = NULL; + set_transient($cache_name, $compiled_cache); + } + } - $stylesheet->save(); - do_action('wp-less_stylesheet_save_post', $stylesheet); - } - catch(Exception $e) - { - wp_die($e->getMessage()); - } - } + /** + * Process a WPLessStylesheet + * + * This logic was previously held in WPLessStylesheet::save() + * + * @since 1.4.2 + * @param WPLessStylesheet $stylesheet + * @param null $css + */ + public function saveStylesheet(WPLessStylesheet $stylesheet, $css = null) + { + wp_mkdir_p(dirname($stylesheet->getTargetPath())); + + try + { + do_action('wp-less_stylesheet_save_pre', $stylesheet, $this->getVariables()); + + if ($css === null) + { + $css = $this->compileFile($stylesheet->getSourcePath()); + } + + file_put_contents($stylesheet->getTargetPath(), apply_filters('wp-less_stylesheet_save', $css, $stylesheet)); + chmod($stylesheet->getTargetPath(), 0666); + + $stylesheet->save(); + do_action('wp-less_stylesheet_save_post', $stylesheet); + } + catch(Exception $e) + { + wp_die($e->getMessage()); + } + } } diff --git a/lib/Plugin.class.php b/lib/Plugin.class.php index df01de5..ea6e48f 100644 --- a/lib/Plugin.class.php +++ b/lib/Plugin.class.php @@ -23,7 +23,7 @@ class WPLessPlugin extends WPPluginToolkitPlugin */ public static $match_pattern = '/\.less$/U'; - public function __construct(WPPluginToolkitConfiguration $configuration) + public function __construct(WPLessConfiguration $configuration) { parent::__construct($configuration); @@ -161,12 +161,18 @@ public function getStyles() */ public function processStylesheet($handle, $force = false) { + $force = !!$force ? $force : $this->configuration->alwaysRecompile(); + $wp_styles = $this->getStyles(); $stylesheet = new WPLessStylesheet($wp_styles->registered[$handle], $this->compiler->getVariables()); - if ((is_bool($force) && $force) || $this->configuration->alwaysRecompile() || $stylesheet->hasToCompile()) + if ($this->configuration->getCompilationStrategy() === 'legacy' && $stylesheet->hasToCompile()) + { + $this->compiler->saveStylesheet($stylesheet); + } + elseif ($this->configuration->getCompilationStrategy() !== 'legacy') { - $this->compiler->saveStylesheet($stylesheet); + $this->compiler->cacheStylesheet($stylesheet, $force); } $wp_styles->registered[$handle]->src = $stylesheet->getTargetUri(); @@ -228,7 +234,7 @@ protected function registerHooks() { do_action('wp-less_init', $this); add_action('wp', array($this, 'processStylesheets'), 999, 0); - //add_filter('wp-less_stylesheet_save', array($this, 'filterStylesheetUri'), 10, 2); + add_filter('wp-less_stylesheet_save', array($this, 'filterStylesheetUri'), 10, 2); } else {