Skip to content

Commit

Permalink
Merge pull request #12 from oncletom/feature-12
Browse files Browse the repository at this point in the history
Match lessphp variable API
  • Loading branch information
Oncle Tom committed Oct 9, 2012
2 parents fb6fc1c + 2aa872f commit dcf621b
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 142 deletions.
52 changes: 27 additions & 25 deletions lib/Compiler.class.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php
require dirname(__FILE__).'/vendor/lessphp/lessc.inc.php';

/**
* LESS compiler
*
*
* @author oncletom
* @extends lessc
* @package wp-less
Expand All @@ -13,8 +15,8 @@ class WPLessCompiler extends lessc
{
/**
* Instantiate a compiler
*
* @api
*
* @api
* @see lessc::__construct
* @param $file string [optional] Additional file to parse
*/
Expand All @@ -24,26 +26,9 @@ public function __construct($file = null)
parent::__construct(apply_filters('wp-less_compiler_construct', $file));
}

/**
* Parse a LESS file
*
* @api
* @see lessc::parse
* @throws Exception
* @param string $text [optional] Custom CSS to parse
* @param array $variables [optional] Variables to inject in the stylesheet
* @return string CSS output
*/
public function parse($text = null, $variables = null)
{
do_action('wp-less_compiler_parse_pre', $this, $text, $variables);
return apply_filters('wp-less_compiler_parse', parent::parse($text, $variables));
}

/**
* Registers a set of functions
* Originally stored in WPLessConfiguration instance
*
*
* @param array $functions
*/
public function registerFunctions(array $functions = array())
Expand All @@ -54,11 +39,27 @@ public function registerFunctions(array $functions = array())
}
}

/**
* Returns available variables
*
* @since 1.5
* @return array Already defined variables
*/
public function getVariables()
{
return $this->registeredVars;
}

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)
Expand All @@ -67,9 +68,10 @@ public function saveStylesheet(WPLessStylesheet $stylesheet)

try
{
do_action('wp-less_stylesheet_save_pre', $stylesheet, $stylesheet->getVariables());
do_action('wp-less_stylesheet_save_pre', $stylesheet, $this->getVariables());

$this->compileFile($stylesheet->getSourcePath(), $stylesheet->getTargetPath());

file_put_contents($stylesheet->getTargetPath(), apply_filters('wp-less_stylesheet_save', $this->parse(null, $stylesheet->getVariables()), $stylesheet));
chmod($stylesheet->getTargetPath(), 0666);

$stylesheet->save();
Expand Down
83 changes: 5 additions & 78 deletions lib/Configuration.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ class WPLessConfiguration extends WPPluginToolkitConfiguration
*/
const VERSION = '1.5-dev';

/**
* @protected
*/
protected $variables = array();

/**
* @protected
* @see http://leafo.net/lessphp/docs/index.html#custom_functions
Expand All @@ -26,79 +21,11 @@ class WPLessConfiguration extends WPPluginToolkitConfiguration

protected function configure()
{
$this->configureOptions();
}

protected function configureOptions()
{
$this->setVariables(array());
}

/**
* Set global Less variables
*
* @since 1.4
*/
public function addVariable($name, $value)
{
$this->variables[$name] = $value;
}

/**
* Returns the registered variables
*
* @since 1.4
* @return array
*/
public function getVariables()
{
return $this->variables;
}

/**
* Set global Less variables
*
* @since 1.4
*/
public function setVariables(array $variables)
{
$this->variables = $variables;
}

/**
* Return LESS functions
*
* @since 1.4.2
* @return array
*/
public function getFunctions()
{
return $this->functions;
$this->configureOptions();
}

/**
* Registers a new LESS function
*
* @param string $name
* @param Closure|function $callback
* @param array $scope CSS handles to limit callback registration to (if empty, applies to every stylesheet) – not used yet
* @see http://leafo.net/lessphp/docs/index.html#custom_functions
*/
public function registerFunction($name, $callback, $scope = array())
{
$this->functions[$name] = array(
'callback' => $callback,
'scope' => $scope,
);
}

/**
* Unregisters a LESS function
*
* @see http://leafo.net/lessphp/docs/index.html#custom_functions
*/
public function unregisterFunction($name)
{
unset($this->functions[$name]);
}
protected function configureOptions()
{
//
}
}
44 changes: 26 additions & 18 deletions lib/Plugin.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,23 @@ class WPLessPlugin extends WPPluginToolkitPlugin
{
protected $is_filters_registered = false;
protected $is_hooks_registered = false;
protected $compiler = null;

/**
* @static
* @var Pattern used to match stylesheet files to process them as pure CSS
*/
public static $match_pattern = '/\.less$/U';

public function __construct(WPPluginToolkitConfiguration $configuration)
{
parent::__construct($configuration);

$this->compiler = new WPLessCompiler;
$this->compiler->setVariable('stylesheet_directory_uri', "'".get_stylesheet_directory_uri()."'");
$this->compiler->setVariable('template_directory_uri', "'".get_template_directory_uri()."'");
}

/**
* Dispatches all events of the plugin
*
Expand Down Expand Up @@ -118,13 +128,11 @@ public function getStyles()
public function processStylesheet($handle, $force = false)
{
$wp_styles = $this->getStyles();
$stylesheet = new WPLessStylesheet($wp_styles->registered[$handle], $this->getConfiguration()->getVariables());
$stylesheet = new WPLessStylesheet($wp_styles->registered[$handle], $this->compiler->getVariables());

if ((is_bool($force) && $force) || $stylesheet->hasToCompile())
{
$compiler = new WPLessCompiler($stylesheet->getSourcePath());
$compiler->registerFunctions($this->getConfiguration()->getFunctions());
$compiler->saveStylesheet($stylesheet);
$this->compiler->saveStylesheet($stylesheet);
}

$wp_styles->registered[$handle]->src = $stylesheet->getTargetUri();
Expand All @@ -145,7 +153,7 @@ public function processStylesheets($force = false)
$styles = $this->getQueuedStylesToProcess();
$wp_styles = $this->getStyles();
$force = is_bool($force) && $force ? !!$force : false;

WPLessStylesheet::$upload_dir = $this->configuration->getUploadDir();
WPLessStylesheet::$upload_uri = $this->configuration->getUploadUrl();

Expand Down Expand Up @@ -186,7 +194,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 All @@ -198,45 +206,45 @@ protected function registerHooks()

/**
* Proxy method
*
* @see WPLessConfiguration::setVariables()
*
* @see http://leafo.net/lessphp/docs/#setting_variables_from_php
* @since 1.4
*/
public function addVariable($name, $value)
{
$this->getConfiguration()->addVariable($name, $value);
$this->compiler->setVariables(array( $name => $value ));
}

/**
* Proxy method
*
* @see WPLessConfiguration::setVariables()
*
* @see http://leafo.net/lessphp/docs/#setting_variables_from_php
* @since 1.4
*/
public function setVariables(array $variables)
{
$this->getConfiguration()->setVariables($variables);
$this->compiler->setVariables($variables);
}

/**
* Proxy method
*
* @see WPLessConfiguration::registerFunction()
*
* @see http://leafo.net/lessphp/docs/#custom_functions
* @since 1.4.2
*/
public function registerFunction($name, $callback, $scope = array())
public function registerFunction($name, $callback)
{
$this->getConfiguration()->registerFunction($name, $callback, $scope);
$this->compiler->registerFunction($name, $callback);
}

/**
* Proxy method
*
*
* @see WPLessConfiguration::unregisterFunction()
* @since 1.4.2
*/
public function unregisterFunction($name)
{
$this->getConfiguration()->unregisterFunction($name);
$this->compiler->unregisterFunction($name);
}
}
27 changes: 6 additions & 21 deletions lib/Stylesheet.class.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<?php
require dirname(__FILE__).'/vendor/lessphp/lessc.inc.php';

/**
* Stylesheet management
*
Expand All @@ -11,8 +9,7 @@
class WPLessStylesheet
{
protected $compiler,
$stylesheet,
$variables = array();
$stylesheet;

protected $is_new = true,
$signature,
Expand All @@ -38,7 +35,6 @@ class WPLessStylesheet
public function __construct(_WP_Dependency $stylesheet, array $variables = array())
{
$this->stylesheet = $stylesheet;
$this->variables = $variables;

if (!self::$upload_dir || !self::$upload_uri)
{
Expand All @@ -47,7 +43,7 @@ public function __construct(_WP_Dependency $stylesheet, array $variables = array

$this->stylesheet->ver = null;
$this->configurePath();
$this->configureSignature();
$this->configureSignature($variables);

if (file_exists($this->getTargetPath()))
{
Expand Down Expand Up @@ -103,12 +99,13 @@ protected function configurePath()
* It should be called each time stylesheet variables are updated.
*
* @author oncletom
* @param array $variables List of variables used for signature
* @since 1.4.2
* @version 1.0
* @version 1.1
*/
protected function configureSignature()
protected function configureSignature(array $variables = array())
{
$this->signature = substr(sha1(serialize($this->variables) . $this->source_timestamp), 0, 10);
$this->signature = substr(sha1(serialize($variables) . $this->source_timestamp), 0, 10);
}

/**
Expand Down Expand Up @@ -178,18 +175,6 @@ public function getTargetUri()
return sprintf($this->target_uri, $this->signature);
}

/**
* Returns stylesheet variables
*
* @author oncletom
* @since 1.4.2
* @return array
*/
public function getVariables()
{
return $this->variables;
}

/**
* Tells if compilation is needed
*
Expand Down
7 changes: 7 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ Theme developers can even bundle the plugin without worrying about conflicts: ju
The plugin lets you concentrate on what you need: coding CSS. Everything else is handled automatically, from cache management to user delivery.
Seriously.

= Documentation =

Advanced topics on how to use the plugin API are [available on the Github project wiki](https://github.com/oncletom/wp-less/wiki).

= Requirements =

The sole requirement is to use WordPress API and LESS convention: the `.less` extension.
Expand Down Expand Up @@ -52,6 +56,9 @@ The sole requirement is to use WordPress API and LESS convention: the `.less` ex

Mostly issues related to `lessphp` 0.3.8 features.

* [dev documentation available online](https://github.com/oncletom/wp-less/wiki)
* feature: providing stylesheet and template directory uri variables (`@stylesheet_directory_uri` & `@template_directory_uri`) following WordPress convention
* feature: Pruning old compiled files [#15](https://github.com/oncletom/wp-less/issues/15)
* feature: Smarter LESS compilation (following @import file updates) [#13](https://github.com/oncletom/wp-less/issues/13)
* feature: Systematic LESS rebuild through configuration [#14](https://github.com/oncletom/wp-less/issues/14)
* improvement: Match lessphp variable API [#12](https://github.com/oncletom/wp-less/issues/12)
Expand Down

0 comments on commit dcf621b

Please sign in to comment.