示例#1
0
 /**
  * @covers compile
  */
 public function testCompile()
 {
     $env = new Context();
     $d = new ExpressionNode([new AnonymousNode('foobar')]);
     $result = $d->compile($env);
     $this->assertInstanceOf('ILess\\Node\\AnonymousNode', $result);
     $this->assertEquals('foobar', $result->toCSS($env));
     // FIXME: more tests!
 }
示例#2
0
 /**
  * Compile parameters.
  *
  * @param Context $context The context
  * @param Context $mixinEnv The mixin environment
  * @param array $arguments Array of arguments
  * @param array $compiledArguments The compiled arguments
  *
  * @throws
  *
  * @return mixed
  */
 public function compileParams(Context $context, Context $mixinEnv, $arguments = [], array &$compiledArguments = [])
 {
     $frame = new RulesetNode([], []);
     $params = $this->params;
     $argsCount = 0;
     if (isset($mixinEnv->frames[0]) && $mixinEnv->frames[0]->functionRegistry) {
         $frame->functionRegistry = $mixinEnv->frames[0]->functionRegistry->inherit();
     }
     // create a copy of mixin environment
     $mixinEnv = Context::createCopyForCompilation($mixinEnv, array_merge([$frame], $mixinEnv->frames));
     if ($arguments) {
         $argsCount = count($arguments);
         for ($i = 0; $i < $argsCount; ++$i) {
             if (!isset($arguments[$i])) {
                 continue;
             }
             $arg = $arguments[$i];
             if (isset($arg['name']) && ($name = $arg['name'])) {
                 $isNamedFound = false;
                 foreach ($params as $j => $param) {
                     if (!isset($compiledArguments[$j]) && $name === $params[$j]['name']) {
                         $compiledArguments[$j] = $arg['value']->compile($context);
                         array_unshift($frame->rules, new RuleNode($name, $arg['value']->compile($context)));
                         $isNamedFound = true;
                         break;
                     }
                 }
                 if ($isNamedFound) {
                     array_splice($arguments, $i, 1);
                     --$i;
                     continue;
                 } else {
                     throw new CompilerException(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']) && $param['variadic']) {
                 $varArgs = [];
                 for ($j = $argIndex; $j < $argsCount; ++$j) {
                     $varArgs[] = $arguments[$j]['value']->compile($context);
                 }
                 $expression = new ExpressionNode($varArgs);
                 array_unshift($frame->rules, new RuleNode($name, $expression->compile($context)));
             } else {
                 $val = $arg && $arg['value'] ? $arg['value'] : false;
                 if ($val) {
                     $val = $val->compile($context);
                 } elseif (isset($param['value'])) {
                     $val = $param['value']->compile($mixinEnv);
                     $frame->resetCache();
                 } else {
                     throw new CompilerException(sprintf('Wrong number of arguments for `%s` (%s for %s)', $this->name, count($arguments), $this->arity));
                 }
                 array_unshift($frame->rules, new RuleNode($name, $val));
                 $compiledArguments[$i] = $val;
             }
         }
         if (isset($param['variadic']) && $param['variadic'] && $arguments) {
             for ($j = $argIndex; $j < $argsCount; ++$j) {
                 $compiledArguments[$j] = $arguments[$j]['value']->compile($context);
             }
         }
         ++$argIndex;
     }
     ksort($compiledArguments);
     return $frame;
 }