Beispiel #1
0
 /**
  * @covers compile
  */
 public function testCompile()
 {
     $env = new ILess_Environment();
     $d = new ILess_Node_Expression(array(new ILess_Node_Anonymous('foobar')));
     $result = $d->compile($env);
     $this->assertInstanceOf('ILess_Node_Anonymous', $result);
     $this->assertEquals('foobar', $result->toCSS($env));
     // FIXME: more tests!
 }
Beispiel #2
0
 /**
  * Compile parameters
  *
  * @param ILess_Environment $env The environment
  * @param ILess_Environment $mixinEnv The mixin environment
  * @param array $arguments Array of arguments
  * @param array $compiledArguments The compiled arguments
  */
 public function compileParams(ILess_Environment $env, ILess_Environment $mixinEnv, $arguments = array(), array &$compiledArguments = array())
 {
     $frame = new ILess_Node_Ruleset(array(), array());
     $params = $this->params;
     // create a copy of mixin environment
     $mixinEnv = ILess_Environment::createCopy($mixinEnv, array_merge(array($frame), $mixinEnv->frames));
     for ($i = 0; $i < count($arguments); $i++) {
         $arg = $arguments[$i];
         if ($name = $arg['name']) {
             $isNamedFound = false;
             foreach ($params as $j => $param) {
                 if (!isset($compiledArguments[$j]) && $name === $params[$j]['name']) {
                     $compiledArguments[$j] = $arg['value']->compile($env);
                     array_unshift($frame->rules, new ILess_Node_Rule($name, $arg['value']->compile($env)));
                     $isNamedFound = true;
                     break;
                 }
             }
             if ($isNamedFound) {
                 array_splice($arguments, $i, 1);
                 $i--;
                 continue;
             } else {
                 throw new ILess_Exception_Compiler(sprintf('The named argument for `%s` %s was not found.', $this->name, $arguments[$i]['name']));
             }
         }
     }
     $argIndex = 0;
     foreach ($params as $i => $param) {
         if (array_key_exists($i, $compiledArguments)) {
             continue;
         }
         $arg = null;
         if (array_key_exists($argIndex, $arguments)) {
             $arg = $arguments[$argIndex];
         }
         if (isset($param['name']) && ($name = $param['name'])) {
             if (isset($param['variadic']) && $arguments) {
                 $varArgs = array();
                 for ($j = $argIndex, $count = count($arguments); $j < $count; $j++) {
                     $varArgs[] = $arguments[$j]['value']->compile($env);
                 }
                 $expression = new ILess_Node_Expression($varArgs);
                 array_unshift($frame->rules, new ILess_Node_Rule($name, $expression->compile($env)));
             } else {
                 $val = $arg && $arg['value'] ? $arg['value'] : false;
                 if ($val) {
                     $val = $val->compile($env);
                 } elseif (isset($param['value'])) {
                     $val = $param['value']->compile($mixinEnv);
                     $frame->resetCache();
                 } else {
                     throw new ILess_Exception_Compiler(sprintf('Wrong number of arguments for `%s` (%s for %s)', $this->name, count($arguments), $this->arity));
                 }
                 array_unshift($frame->rules, new ILess_Node_Rule($name, $val));
                 $compiledArguments[$i] = $val;
             }
         }
         if (isset($param['variadic']) && $arguments) {
             for ($j = $argIndex, $count = count($arguments); $j < $count; $j++) {
                 $compiledArguments[$j] = $arguments[$j]['value']->compile($env);
             }
         }
         $argIndex++;
     }
     ksort($compiledArguments);
     return $frame;
 }