예제 #1
0
 private function escapePrintNode(Apishka_Templater_Node_Print $node, Apishka_Templater_Environment $env, $type)
 {
     if (false === $type) {
         return $node;
     }
     $expression = $node->getNode('expr');
     if ($this->isSafeFor($type, $expression, $env)) {
         return $node;
     }
     $class = get_class($node);
     return new $class($this->getEscaperFilter($type, $expression), $node->getLine());
 }
예제 #2
0
파일: Block.php 프로젝트: apishka/templater
 public function parse(Apishka_Templater_Token $token)
 {
     $lineno = $token->getLine();
     $stream = $this->parser->getStream();
     $name = $stream->expect(Apishka_Templater_Token::NAME_TYPE)->getValue();
     if ($this->parser->hasBlock($name)) {
         throw new Apishka_Templater_Error_Syntax(sprintf("The block '%s' has already been defined line %d.", $name, $this->parser->getBlock($name)->getLine()), $stream->getCurrent()->getLine(), $stream->getFilename());
     }
     $this->parser->setBlock($name, $block = Apishka_Templater_Node_Block::apishka($name, Apishka_Templater_Node::apishka(array()), $lineno));
     $this->parser->pushLocalScope();
     $this->parser->pushBlockStack($name);
     if ($stream->nextIf(Apishka_Templater_Token::BLOCK_END_TYPE)) {
         $body = $this->parser->subparse(array($this, 'decideBlockEnd'), true);
         if ($token = $stream->nextIf(Apishka_Templater_Token::NAME_TYPE)) {
             $value = $token->getValue();
             if ($value != $name) {
                 throw new Apishka_Templater_Error_Syntax(sprintf('Expected endblock for block "%s" (but "%s" given).', $name, $value), $stream->getCurrent()->getLine(), $stream->getFilename());
             }
         }
     } else {
         $body = Apishka_Templater_Node::apishka(array(Apishka_Templater_Node_Print::apishka($this->parser->getExpressionParser()->parseExpression(), $lineno)));
     }
     $stream->expect(Apishka_Templater_Token::BLOCK_END_TYPE);
     $block->setNode('body', $body);
     $this->parser->popBlockStack();
     $this->parser->popLocalScope();
     return Apishka_Templater_Node_BlockReference::apishka($name, $lineno, $this->getTag());
 }
예제 #3
0
 public function parse(Apishka_Templater_Token $token)
 {
     $name = $this->parser->getVarName();
     $ref = Apishka_Templater_Node_Expression_BlockReference::apishka(Apishka_Templater_Node_Expression_Constant::apishka($name, $token->getLine()), Apishka_Templater_Node::apishka(), true, $token->getLine(), $this->getTag());
     $filter = $this->parser->getExpressionParser()->parseFilterExpressionRaw($ref, $this->getTag());
     $this->parser->getStream()->expect(Apishka_Templater_Token::BLOCK_END_TYPE);
     $body = $this->parser->subparse(array($this, 'decideBlockEnd'), true);
     $this->parser->getStream()->expect(Apishka_Templater_Token::BLOCK_END_TYPE);
     $block = Apishka_Templater_Node_Block::apishka($name, $body, $token->getLine());
     $this->parser->setBlock($name, $block);
     return Apishka_Templater_Node_Print::apishka($filter, $token->getLine(), $this->getTag());
 }
예제 #4
0
    public function getTests()
    {
        $tests = array();
        $names = Apishka_Templater_Node::apishka(array(Apishka_Templater_Node_Expression_AssignName::apishka('foo', 1)), array(), 1);
        $values = Apishka_Templater_Node::apishka(array(Apishka_Templater_Node_Expression_Constant::apishka('foo', 1)), array(), 1);
        $node = Apishka_Templater_Node_Set::apishka(false, $names, $values, 1);
        $tests[] = array($node, <<<EOF
// line 1
\$context["foo"] = "foo";
EOF
);
        $names = Apishka_Templater_Node::apishka(array(Apishka_Templater_Node_Expression_AssignName::apishka('foo', 1)), array(), 1);
        $values = Apishka_Templater_Node::apishka(array(Apishka_Templater_Node_Print::apishka(Apishka_Templater_Node_Expression_Constant::apishka('foo', 1), 1)), array(), 1);
        $node = Apishka_Templater_Node_Set::apishka(true, $names, $values, 1);
        $tests[] = array($node, <<<EOF
// line 1
ob_start();
echo "foo";
\$context["foo"] = ('' === \$tmp = ob_get_clean()) ? '' : new Apishka_Templater_Markup(\$tmp, \$this->env->getCharset());
EOF
);
        $names = Apishka_Templater_Node::apishka(array(Apishka_Templater_Node_Expression_AssignName::apishka('foo', 1)), array(), 1);
        $values = Apishka_Templater_Node_Text::apishka('foo', 1);
        $node = Apishka_Templater_Node_Set::apishka(true, $names, $values, 1);
        $tests[] = array($node, <<<EOF
// line 1
\$context["foo"] = ('' === \$tmp = "foo") ? '' : new Apishka_Templater_Markup(\$tmp, \$this->env->getCharset());
EOF
);
        $names = Apishka_Templater_Node::apishka(array(Apishka_Templater_Node_Expression_AssignName::apishka('foo', 1), Apishka_Templater_Node_Expression_AssignName::apishka('bar', 1)), array(), 1);
        $values = Apishka_Templater_Node::apishka(array(Apishka_Templater_Node_Expression_Constant::apishka('foo', 1), Apishka_Templater_Node_Expression_Name::apishka('bar', 1)), array(), 1);
        $node = Apishka_Templater_Node_Set::apishka(false, $names, $values, 1);
        $tests[] = array($node, <<<EOF
// line 1
list(\$context["foo"], \$context["bar"]) = array("foo", {$this->getVariableGetter('bar')});
EOF
);
        return $tests;
    }
예제 #5
0
    public function getTests()
    {
        $tests = array();
        $t = Apishka_Templater_Node::apishka(array(Apishka_Templater_Node_Expression_Constant::apishka(true, 1), Apishka_Templater_Node_Print::apishka(Apishka_Templater_Node_Expression_Name::apishka('foo', 1), 1)), array(), 1);
        $else = null;
        $node = Apishka_Templater_Node_If::apishka($t, $else, 1);
        $tests[] = array($node, <<<EOF
// line 1
if (true) {
    echo {$this->getVariableGetter('foo')};
}
EOF
);
        $t = Apishka_Templater_Node::apishka(array(Apishka_Templater_Node_Expression_Constant::apishka(true, 1), Apishka_Templater_Node_Print::apishka(Apishka_Templater_Node_Expression_Name::apishka('foo', 1), 1), Apishka_Templater_Node_Expression_Constant::apishka(false, 1), Apishka_Templater_Node_Print::apishka(Apishka_Templater_Node_Expression_Name::apishka('bar', 1), 1)), array(), 1);
        $else = null;
        $node = Apishka_Templater_Node_If::apishka($t, $else, 1);
        $tests[] = array($node, <<<EOF
// line 1
if (true) {
    echo {$this->getVariableGetter('foo')};
} elseif (false) {
    echo {$this->getVariableGetter('bar')};
}
EOF
);
        $t = Apishka_Templater_Node::apishka(array(Apishka_Templater_Node_Expression_Constant::apishka(true, 1), Apishka_Templater_Node_Print::apishka(Apishka_Templater_Node_Expression_Name::apishka('foo', 1), 1)), array(), 1);
        $else = Apishka_Templater_Node_Print::apishka(Apishka_Templater_Node_Expression_Name::apishka('bar', 1), 1);
        $node = Apishka_Templater_Node_If::apishka($t, $else, 1);
        $tests[] = array($node, <<<EOF
// line 1
if (true) {
    echo {$this->getVariableGetter('foo')};
} else {
    echo {$this->getVariableGetter('bar')};
}
EOF
);
        return $tests;
    }
예제 #6
0
    public function getTests()
    {
        $tests = array();
        $keyTarget = Apishka_Templater_Node_Expression_AssignName::apishka('key', 1);
        $valueTarget = Apishka_Templater_Node_Expression_AssignName::apishka('item', 1);
        $seq = Apishka_Templater_Node_Expression_Name::apishka('items', 1);
        $ifexpr = null;
        $body = Apishka_Templater_Node::apishka(array(Apishka_Templater_Node_Print::apishka(Apishka_Templater_Node_Expression_Name::apishka('foo', 1), 1)), array(), 1);
        $else = null;
        $node = Apishka_Templater_Node_For::apishka($keyTarget, $valueTarget, $seq, $ifexpr, $body, $else, 1);
        $node->setAttribute('with_loop', false);
        $tests[] = array($node, <<<EOF
// line 1
\$context['_parent'] = \$context;
\$context['_seq'] = twig_ensure_traversable({$this->getVariableGetter('items')});
foreach (\$context['_seq'] as \$context["key"] => \$context["item"]) {
    echo {$this->getVariableGetter('foo')};
}
\$_parent = \$context['_parent'];
unset(\$context['_seq'], \$context['_iterated'], \$context['key'], \$context['item'], \$context['_parent'], \$context['loop']);
\$context = array_intersect_key(\$context, \$_parent) + \$_parent;
EOF
);
        $keyTarget = Apishka_Templater_Node_Expression_AssignName::apishka('k', 1);
        $valueTarget = Apishka_Templater_Node_Expression_AssignName::apishka('v', 1);
        $seq = Apishka_Templater_Node_Expression_Name::apishka('values', 1);
        $ifexpr = null;
        $body = Apishka_Templater_Node::apishka(array(Apishka_Templater_Node_Print::apishka(Apishka_Templater_Node_Expression_Name::apishka('foo', 1), 1)), array(), 1);
        $else = null;
        $node = Apishka_Templater_Node_For::apishka($keyTarget, $valueTarget, $seq, $ifexpr, $body, $else, 1);
        $node->setAttribute('with_loop', true);
        $tests[] = array($node, <<<EOF
// line 1
\$context['_parent'] = \$context;
\$context['_seq'] = twig_ensure_traversable({$this->getVariableGetter('values')});
\$context['loop'] = array(
  'parent' => \$context['_parent'],
  'index0' => 0,
  'index'  => 1,
  'first'  => true,
);
if (is_array(\$context['_seq']) || (is_object(\$context['_seq']) && \$context['_seq'] instanceof Countable)) {
    \$length = count(\$context['_seq']);
    \$context['loop']['revindex0'] = \$length - 1;
    \$context['loop']['revindex'] = \$length;
    \$context['loop']['length'] = \$length;
    \$context['loop']['last'] = 1 === \$length;
}
foreach (\$context['_seq'] as \$context["k"] => \$context["v"]) {
    echo {$this->getVariableGetter('foo')};
    ++\$context['loop']['index0'];
    ++\$context['loop']['index'];
    \$context['loop']['first'] = false;
    if (isset(\$context['loop']['length'])) {
        --\$context['loop']['revindex0'];
        --\$context['loop']['revindex'];
        \$context['loop']['last'] = 0 === \$context['loop']['revindex0'];
    }
}
\$_parent = \$context['_parent'];
unset(\$context['_seq'], \$context['_iterated'], \$context['k'], \$context['v'], \$context['_parent'], \$context['loop']);
\$context = array_intersect_key(\$context, \$_parent) + \$_parent;
EOF
);
        $keyTarget = Apishka_Templater_Node_Expression_AssignName::apishka('k', 1);
        $valueTarget = Apishka_Templater_Node_Expression_AssignName::apishka('v', 1);
        $seq = Apishka_Templater_Node_Expression_Name::apishka('values', 1);
        $ifexpr = Apishka_Templater_Node_Expression_Constant::apishka(true, 1);
        $body = Apishka_Templater_Node::apishka(array(Apishka_Templater_Node_Print::apishka(Apishka_Templater_Node_Expression_Name::apishka('foo', 1), 1)), array(), 1);
        $else = null;
        $node = Apishka_Templater_Node_For::apishka($keyTarget, $valueTarget, $seq, $ifexpr, $body, $else, 1);
        $node->setAttribute('with_loop', true);
        $tests[] = array($node, <<<EOF
// line 1
\$context['_parent'] = \$context;
\$context['_seq'] = twig_ensure_traversable({$this->getVariableGetter('values')});
\$context['loop'] = array(
  'parent' => \$context['_parent'],
  'index0' => 0,
  'index'  => 1,
  'first'  => true,
);
foreach (\$context['_seq'] as \$context["k"] => \$context["v"]) {
    if (true) {
        echo {$this->getVariableGetter('foo')};
        ++\$context['loop']['index0'];
        ++\$context['loop']['index'];
        \$context['loop']['first'] = false;
    }
}
\$_parent = \$context['_parent'];
unset(\$context['_seq'], \$context['_iterated'], \$context['k'], \$context['v'], \$context['_parent'], \$context['loop']);
\$context = array_intersect_key(\$context, \$_parent) + \$_parent;
EOF
);
        $keyTarget = Apishka_Templater_Node_Expression_AssignName::apishka('k', 1);
        $valueTarget = Apishka_Templater_Node_Expression_AssignName::apishka('v', 1);
        $seq = Apishka_Templater_Node_Expression_Name::apishka('values', 1);
        $ifexpr = null;
        $body = Apishka_Templater_Node::apishka(array(Apishka_Templater_Node_Print::apishka(Apishka_Templater_Node_Expression_Name::apishka('foo', 1), 1)), array(), 1);
        $else = Apishka_Templater_Node_Print::apishka(Apishka_Templater_Node_Expression_Name::apishka('foo', 1), 1);
        $node = Apishka_Templater_Node_For::apishka($keyTarget, $valueTarget, $seq, $ifexpr, $body, $else, 1);
        $node->setAttribute('with_loop', true);
        $tests[] = array($node, <<<EOF
// line 1
\$context['_parent'] = \$context;
\$context['_seq'] = twig_ensure_traversable({$this->getVariableGetter('values')});
\$context['_iterated'] = false;
\$context['loop'] = array(
  'parent' => \$context['_parent'],
  'index0' => 0,
  'index'  => 1,
  'first'  => true,
);
if (is_array(\$context['_seq']) || (is_object(\$context['_seq']) && \$context['_seq'] instanceof Countable)) {
    \$length = count(\$context['_seq']);
    \$context['loop']['revindex0'] = \$length - 1;
    \$context['loop']['revindex'] = \$length;
    \$context['loop']['length'] = \$length;
    \$context['loop']['last'] = 1 === \$length;
}
foreach (\$context['_seq'] as \$context["k"] => \$context["v"]) {
    echo {$this->getVariableGetter('foo')};
    \$context['_iterated'] = true;
    ++\$context['loop']['index0'];
    ++\$context['loop']['index'];
    \$context['loop']['first'] = false;
    if (isset(\$context['loop']['length'])) {
        --\$context['loop']['revindex0'];
        --\$context['loop']['revindex'];
        \$context['loop']['last'] = 0 === \$context['loop']['revindex0'];
    }
}
if (!\$context['_iterated']) {
    echo {$this->getVariableGetter('foo')};
}
\$_parent = \$context['_parent'];
unset(\$context['_seq'], \$context['_iterated'], \$context['k'], \$context['v'], \$context['_parent'], \$context['loop']);
\$context = array_intersect_key(\$context, \$_parent) + \$_parent;
EOF
);
        return $tests;
    }
예제 #7
0
 public function subparse($test, $dropNeedle = false)
 {
     $lineno = $this->getCurrentToken()->getLine();
     $rv = array();
     while (!$this->stream->isEOF()) {
         switch ($this->getCurrentToken()->getType()) {
             case Apishka_Templater_Token::TEXT_TYPE:
                 $token = $this->stream->next();
                 $rv[] = Apishka_Templater_Node_Text::apishka($token->getValue(), $token->getLine());
                 break;
             case Apishka_Templater_Token::VAR_START_TYPE:
                 $token = $this->stream->next();
                 $expr = $this->expressionParser->parseExpression();
                 $this->stream->expect(Apishka_Templater_Token::VAR_END_TYPE);
                 $rv[] = Apishka_Templater_Node_Print::apishka($expr, $token->getLine());
                 break;
             case Apishka_Templater_Token::BLOCK_START_TYPE:
                 $this->stream->next();
                 $token = $this->getCurrentToken();
                 if ($token->getType() !== Apishka_Templater_Token::NAME_TYPE) {
                     throw new Apishka_Templater_Error_Syntax('A block must start with a tag name.', $token->getLine(), $this->getFilename());
                 }
                 if (null !== $test && $test($token)) {
                     if ($dropNeedle) {
                         $this->stream->next();
                     }
                     if (1 === count($rv)) {
                         return $rv[0];
                     }
                     return Apishka_Templater_Node::apishka($rv, array(), $lineno);
                 }
                 if (!isset($this->handlers[$token->getValue()])) {
                     if (null !== $test) {
                         $e = new Apishka_Templater_Error_Syntax(sprintf('Unexpected "%s" tag', $token->getValue()), $token->getLine(), $this->getFilename());
                         if (is_array($test) && isset($test[0]) && $test[0] instanceof Apishka_Templater_TokenParserInterface) {
                             $e->appendMessage(sprintf(' (expecting closing tag for the "%s" tag defined near line %s).', $test[0]->getTag(), $lineno));
                         }
                     } else {
                         $e = new Apishka_Templater_Error_Syntax(sprintf('Unknown "%s" tag.', $token->getValue()), $token->getLine(), $this->getFilename());
                         $e->addSuggestions($token->getValue(), array_keys($this->env->getTags()));
                     }
                     throw $e;
                 }
                 $this->stream->next();
                 $subparser = $this->handlers[$token->getValue()];
                 $node = $subparser->parse($token);
                 if (null !== $node) {
                     $rv[] = $node;
                 }
                 break;
             default:
                 throw new Apishka_Templater_Error_Syntax('Lexer or parser ended up in unsupported state.', 0, $this->getFilename());
         }
     }
     if (1 === count($rv)) {
         return $rv[0];
     }
     return Apishka_Templater_Node::apishka($rv, array(), $lineno);
 }
예제 #8
0
 public function getTests()
 {
     $tests = array();
     $tests[] = array(Apishka_Templater_Node_Print::apishka(Apishka_Templater_Node_Expression_Constant::apishka('foo', 1), 1), "// line 1\necho \"foo\";");
     return $tests;
 }