Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Smarter LESS compilation (following @import file updates) #13

Merged
merged 3 commits into from
Oct 10, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 53 additions & 23 deletions lib/Compiler.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
65 changes: 55 additions & 10 deletions lib/Configuration.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

/**
Expand Down
File renamed without changes.
14 changes: 10 additions & 4 deletions lib/Plugin.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
{
Expand Down