コード例 #1
0
ファイル: SetTest.php プロジェクト: apishka/templater
    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;
    }
コード例 #2
0
ファイル: For.php プロジェクト: apishka/templater
 public function parse(Apishka_Templater_Token $token)
 {
     $lineno = $token->getLine();
     $stream = $this->parser->getStream();
     $targets = $this->parser->getExpressionParser()->parseAssignmentExpression();
     $stream->expect(Apishka_Templater_Token::OPERATOR_TYPE, 'in');
     $seq = $this->parser->getExpressionParser()->parseExpression();
     $ifexpr = null;
     if ($stream->nextIf(Apishka_Templater_Token::NAME_TYPE, 'if')) {
         $ifexpr = $this->parser->getExpressionParser()->parseExpression();
     }
     $stream->expect(Apishka_Templater_Token::BLOCK_END_TYPE);
     $body = $this->parser->subparse(array($this, 'decideForFork'));
     if ($stream->next()->getValue() == 'else') {
         $stream->expect(Apishka_Templater_Token::BLOCK_END_TYPE);
         $else = $this->parser->subparse(array($this, 'decideForEnd'), true);
     } else {
         $else = null;
     }
     $stream->expect(Apishka_Templater_Token::BLOCK_END_TYPE);
     if (count($targets) > 1) {
         $keyTarget = $targets->getNode(0);
         $keyTarget = Apishka_Templater_Node_Expression_AssignName::apishka($keyTarget->getAttribute('name'), $keyTarget->getLine());
         $valueTarget = $targets->getNode(1);
         $valueTarget = Apishka_Templater_Node_Expression_AssignName::apishka($valueTarget->getAttribute('name'), $valueTarget->getLine());
     } else {
         $keyTarget = Apishka_Templater_Node_Expression_AssignName::apishka('_key', $lineno);
         $valueTarget = $targets->getNode(0);
         $valueTarget = Apishka_Templater_Node_Expression_AssignName::apishka($valueTarget->getAttribute('name'), $valueTarget->getLine());
     }
     if ($ifexpr) {
         $this->checkLoopUsageCondition($stream, $ifexpr);
         $this->checkLoopUsageBody($stream, $body);
     }
     return Apishka_Templater_Node_For::apishka($keyTarget, $valueTarget, $seq, $ifexpr, $body, $else, $lineno, $this->getTag());
 }
コード例 #3
0
 public function parseAssignmentExpression()
 {
     $targets = array();
     while (true) {
         $token = $this->parser->getStream()->expect(Apishka_Templater_Token::NAME_TYPE, null, 'Only variables can be assigned to');
         if (in_array($token->getValue(), array('true', 'false', 'none'))) {
             throw new Apishka_Templater_Error_Syntax(sprintf('You cannot assign a value to "%s".', $token->getValue()), $token->getLine(), $this->parser->getFilename());
         }
         $targets[] = Apishka_Templater_Node_Expression_AssignName::apishka($token->getValue(), $token->getLine());
         if (!$this->parser->getStream()->nextIf(Apishka_Templater_Token::PUNCTUATION_TYPE, ',')) {
             break;
         }
     }
     return Apishka_Templater_Node::apishka($targets);
 }
コード例 #4
0
ファイル: ForTest.php プロジェクト: apishka/templater
    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;
    }
コード例 #5
0
ファイル: ModuleTest.php プロジェクト: apishka/templater
    public function getTests()
    {
        $twig = new Apishka_Templater_Environment($this->createMock('Apishka_Templater_LoaderInterface'));
        $tests = array();
        $body = Apishka_Templater_Node_Text::apishka('foo', 1);
        $extends = null;
        $blocks = Apishka_Templater_Node::apishka();
        $macros = Apishka_Templater_Node::apishka();
        $traits = Apishka_Templater_Node::apishka();
        $filename = 'foo.twig';
        $node = Apishka_Templater_Node_Module::apishka($body, $extends, $blocks, $macros, $traits, Apishka_Templater_Node::apishka(array()), $filename);
        $tests[] = array($node, <<<EOF
<?php

/* foo.twig */
class __TwigTemplate_%x extends Apishka_Templater_TemplateAbstract
{
    public function __construct(Apishka_Templater_Environment \$env)
    {
        parent::__construct(\$env);

        \$this->parent = false;

        \$this->blocks = array(
        );
    }

    public function getSupportedNames()
    {
        return array(
            'foo.twig',
        );
    }

    protected function doDisplay(array \$context, array \$blocks = array())
    {
        // line 1
        echo "foo";
    }

    public function getTemplateName()
    {
        return "foo.twig";
    }

    public function getDebugInfo()
    {
        return array (  26 => 1,);
    }
}
EOF
, $twig, true);
        $body = Apishka_Templater_Node::apishka(array());
        $extends = Apishka_Templater_Node_Expression_Constant::apishka('layout.twig', 1);
        $node = Apishka_Templater_Node_Module::apishka($body, $extends, $blocks, $macros, $traits, Apishka_Templater_Node::apishka(array()), $filename);
        $tests[] = array($node, <<<EOF
<?php

/* foo.twig */
class __TwigTemplate_%x extends Apishka_Templater_TemplateAbstract
{
    public function __construct(Apishka_Templater_Environment \$env)
    {
        parent::__construct(\$env);

        // line 1
        \$this->parent = \$this->loadTemplate("layout.twig", "foo.twig", 1);
        \$this->blocks = array(
        );
    }

    public function getSupportedNames()
    {
        return array(
            'foo.twig',
        );
    }

    protected function doGetParent(array \$context)
    {
        return "layout.twig";
    }

    protected function doDisplay(array \$context, array \$blocks = array())
    {
        \$this->parent->display(\$context, array_merge(\$this->blocks, \$blocks));
    }

    public function getTemplateName()
    {
        return "foo.twig";
    }

    public function isTraitable()
    {
        return false;
    }

    public function getDebugInfo()
    {
        return array (  11 => 1,);
    }
}
EOF
, $twig, true);
        $set = Apishka_Templater_Node_Set::apishka(false, Apishka_Templater_Node::apishka(array(Apishka_Templater_Node_Expression_AssignName::apishka('foo', 4))), Apishka_Templater_Node::apishka(array(Apishka_Templater_Node_Expression_Constant::apishka('foo', 4))), 4);
        $body = Apishka_Templater_Node::apishka(array($set));
        $extends = Apishka_Templater_Node_Expression_Conditional::apishka(Apishka_Templater_Node_Expression_Constant::apishka(true, 2), Apishka_Templater_Node_Expression_Constant::apishka('foo', 2), Apishka_Templater_Node_Expression_Constant::apishka('foo', 2), 2);
        $node = Apishka_Templater_Node_Module::apishka($body, $extends, $blocks, $macros, $traits, Apishka_Templater_Node::apishka(array()), $filename);
        $tests[] = array($node, <<<EOF
<?php

/* foo.twig */
class __TwigTemplate_%x extends Apishka_Templater_TemplateAbstract
{
    public function getSupportedNames()
    {
        return array(
            'foo.twig',
        );
    }

    protected function doGetParent(array \$context)
    {
        // line 2
        return \$this->loadTemplate(((true) ? ("foo") : ("foo")), "foo.twig", 2);
    }

    protected function doDisplay(array \$context, array \$blocks = array())
    {
        // line 4
        \$context["foo"] = "foo";
        // line 2
        \$this->getParent(\$context)->display(\$context, array_merge(\$this->blocks, \$blocks));
    }

    public function getTemplateName()
    {
        return "foo.twig";
    }

    public function isTraitable()
    {
        return false;
    }

    public function getDebugInfo()
    {
        return array (  24 => 2,  22 => 4,  16 => 2,);
    }
}
EOF
, $twig, true);
        return $tests;
    }
コード例 #6
0
ファイル: AssignNameTest.php プロジェクト: apishka/templater
 public function getTests()
 {
     $node = Apishka_Templater_Node_Expression_AssignName::apishka('foo', 1);
     return array(array($node, '$context["foo"]'));
 }