Call.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * Call
  4. *
  5. * @package Less
  6. * @subpackage tree
  7. */
  8. class Less_Tree_Call extends Less_Tree {
  9. public $value;
  10. public $name;
  11. public $args;
  12. public $index;
  13. public $currentFileInfo;
  14. public $type = 'Call';
  15. public function __construct( $name, $args, $index, $currentFileInfo = null ) {
  16. $this->name = $name;
  17. $this->args = $args;
  18. $this->index = $index;
  19. $this->currentFileInfo = $currentFileInfo;
  20. }
  21. public function accept( $visitor ) {
  22. $this->args = $visitor->visitArray( $this->args );
  23. }
  24. //
  25. // When evaluating a function call,
  26. // we either find the function in `tree.functions` [1],
  27. // in which case we call it, passing the evaluated arguments,
  28. // or we simply print it out as it appeared originally [2].
  29. //
  30. // The *functions.js* file contains the built-in functions.
  31. //
  32. // The reason why we evaluate the arguments, is in the case where
  33. // we try to pass a variable to a function, like: `saturate(@color)`.
  34. // The function should receive the value, not the variable.
  35. //
  36. public function compile( $env = null ) {
  37. $args = array();
  38. foreach ( $this->args as $a ) {
  39. $args[] = $a->compile( $env );
  40. }
  41. $nameLC = strtolower( $this->name );
  42. switch ( $nameLC ) {
  43. case '%':
  44. $nameLC = '_percent';
  45. break;
  46. case 'get-unit':
  47. $nameLC = 'getunit';
  48. break;
  49. case 'data-uri':
  50. $nameLC = 'datauri';
  51. break;
  52. case 'svg-gradient':
  53. $nameLC = 'svggradient';
  54. break;
  55. }
  56. $result = null;
  57. if ( $nameLC === 'default' ) {
  58. $result = Less_Tree_DefaultFunc::compile();
  59. } else {
  60. if ( method_exists( 'Less_Functions', $nameLC ) ) { // 1.
  61. try {
  62. $func = new Less_Functions( $env, $this->currentFileInfo );
  63. $result = call_user_func_array( array( $func,$nameLC ), $args );
  64. } catch ( Exception $e ) {
  65. throw new Less_Exception_Compiler( 'error evaluating function `' . $this->name . '` '.$e->getMessage().' index: '. $this->index );
  66. }
  67. } elseif ( isset( $env->functions[$nameLC] ) && is_callable( $env->functions[$nameLC] ) ) {
  68. try {
  69. $result = call_user_func_array( $env->functions[$nameLC], $args );
  70. } catch ( Exception $e ) {
  71. throw new Less_Exception_Compiler( 'error evaluating function `' . $this->name . '` '.$e->getMessage().' index: '. $this->index );
  72. }
  73. }
  74. }
  75. if ( $result !== null ) {
  76. return $result;
  77. }
  78. return new Less_Tree_Call( $this->name, $args, $this->index, $this->currentFileInfo );
  79. }
  80. /**
  81. * @see Less_Tree::genCSS
  82. */
  83. public function genCSS( $output ) {
  84. $output->add( $this->name . '(', $this->currentFileInfo, $this->index );
  85. $args_len = count( $this->args );
  86. for ( $i = 0; $i < $args_len; $i++ ) {
  87. $this->args[$i]->genCSS( $output );
  88. if ( $i + 1 < $args_len ) {
  89. $output->add( ', ' );
  90. }
  91. }
  92. $output->add( ')' );
  93. }
  94. // public function toCSS(){
  95. // return $this->compile()->toCSS();
  96. //}
  97. }