Skip to content
This repository was archived by the owner on Dec 5, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Implement custom function support
  • Loading branch information
nyordanov committed Apr 13, 2014
commit 86d9895c9c4b17d92d2ddd231bd1ea2a9e51b2ee
5 changes: 5 additions & 0 deletions lib/Less/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ class Less_Environment{

public static $mixin_stack = 0;

/**
* @var array
*/
public $functions = array();


public function Init(){

Expand Down
18 changes: 18 additions & 0 deletions lib/Less/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,25 @@ public function SetOption($option,$value){
Less_Parser::$options[$option] = $value;
}

/**
* Registers a new custom function
*
* @param string $name function name
* @param callable $callback callback
*/
public function registerFunction($name, $callback) {
$this->env->functions[$name] = $callback;
}

/**
* Removed an already registered function
*
* @param string $name function name
*/
public function unregisterFunction($name) {
if( isset($this->env->functions[$name]) )
unset($this->env->functions[$name]);
}


/**
Expand Down
6 changes: 6 additions & 0 deletions lib/Less/Tree/Call.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ public function compile($env=null){
} catch (Exception $e) {
throw new Less_Exception_Compiler('error evaluating function `' . $this->name . '` '.$e->getMessage().' index: '. $this->index);
}
} elseif( isset( $env->functions[$nameLC] ) && is_callable( $env->functions[$nameLC] ) ) {
try {
$result = call_user_func_array( $env->functions[$nameLC], $args );
} catch (Exception $e) {
throw new Less_Exception_Compiler('error evaluating function `' . $this->name . '` '.$e->getMessage().' index: '. $this->index);
}
}
}

Expand Down