Merge branch 'MDL-62945-master' of https://github.com/HuongNV13/moodle
[moodle.git] / lib / lessphp / Tree / Call.php
blob1560351ef7aec439e521e7ae3439fa5b3bcc31ee
1 <?php
4 /**
5 * Call
7 * @package Less
8 * @subpackage tree
9 */
10 class Less_Tree_Call extends Less_Tree{
11 public $value;
13 protected $name;
14 protected $args;
15 protected $index;
16 protected $currentFileInfo;
17 public $type = 'Call';
19 public function __construct($name, $args, $index, $currentFileInfo = null ){
20 $this->name = $name;
21 $this->args = $args;
22 $this->index = $index;
23 $this->currentFileInfo = $currentFileInfo;
26 public function accept( $visitor ){
27 $this->args = $visitor->visitArray( $this->args );
31 // When evaluating a function call,
32 // we either find the function in `tree.functions` [1],
33 // in which case we call it, passing the evaluated arguments,
34 // or we simply print it out as it appeared originally [2].
36 // The *functions.js* file contains the built-in functions.
38 // The reason why we evaluate the arguments, is in the case where
39 // we try to pass a variable to a function, like: `saturate(@color)`.
40 // The function should receive the value, not the variable.
42 public function compile($env=null){
43 $args = array();
44 foreach($this->args as $a){
45 $args[] = $a->compile($env);
48 $nameLC = strtolower($this->name);
49 switch($nameLC){
50 case '%':
51 $nameLC = '_percent';
52 break;
54 case 'get-unit':
55 $nameLC = 'getunit';
56 break;
58 case 'data-uri':
59 $nameLC = 'datauri';
60 break;
62 case 'svg-gradient':
63 $nameLC = 'svggradient';
64 break;
67 $result = null;
68 if( $nameLC === 'default' ){
69 $result = Less_Tree_DefaultFunc::compile();
71 }else{
73 if( method_exists('Less_Functions',$nameLC) ){ // 1.
74 try {
76 $func = new Less_Functions($env, $this->currentFileInfo);
77 $result = call_user_func_array( array($func,$nameLC),$args);
79 } catch (Exception $e) {
80 throw new Less_Exception_Compiler('error evaluating function `' . $this->name . '` '.$e->getMessage().' index: '. $this->index);
82 } elseif( isset( $env->functions[$nameLC] ) && is_callable( $env->functions[$nameLC] ) ) {
83 try {
84 $result = call_user_func_array( $env->functions[$nameLC], $args );
85 } catch (Exception $e) {
86 throw new Less_Exception_Compiler('error evaluating function `' . $this->name . '` '.$e->getMessage().' index: '. $this->index);
91 if( $result !== null ){
92 return $result;
96 return new Less_Tree_Call( $this->name, $args, $this->index, $this->currentFileInfo );
99 /**
100 * @see Less_Tree::genCSS
102 public function genCSS( $output ){
104 $output->add( $this->name . '(', $this->currentFileInfo, $this->index );
105 $args_len = count($this->args);
106 for($i = 0; $i < $args_len; $i++ ){
107 $this->args[$i]->genCSS( $output );
108 if( $i + 1 < $args_len ){
109 $output->add( ', ' );
113 $output->add( ')' );
117 //public function toCSS(){
118 // return $this->compile()->toCSS();