Esempio n. 1
0
 protected function getEnvironment()
 {
     $env = new Apishka_Templater_Environment(new Apishka_Templater_Loader_Array(array()));
     $env->addTest(Apishka_Templater_Test::apishka('anonymous', function () {
     }));
     return $env;
 }
Esempio n. 2
0
 /**
  * @dataProvider getTestsForForOptimizer
  */
 public function testForOptimizer($template, $expected)
 {
     $env = new Apishka_Templater_Environment($this->createMock('Apishka_Templater_LoaderInterface'), array('cache' => false));
     $stream = $env->parse($env->tokenize($template, 'index'));
     foreach ($expected as $target => $withLoop) {
         $this->assertTrue($this->checkForConfiguration($stream, $target, $withLoop), sprintf('variable %s is %soptimized', $target, $withLoop ? 'not ' : ''));
     }
 }
Esempio n. 3
0
 public function testCustomEscaper()
 {
     $twig = new Apishka_Templater_Environment($this->createMock('Apishka_Templater_LoaderInterface'));
     $twig->getExtension('core')->setEscaper('foo', 'foo_escaper_for_test');
     $this->assertEquals('fooUTF-8', twig_escape_filter($twig, 'foo', 'foo'));
     $this->assertEquals('UTF-8', twig_escape_filter($twig, null, 'foo'));
     $this->assertEquals('42UTF-8', twig_escape_filter($twig, 42, 'foo'));
 }
Esempio n. 4
0
 public function testParseIsReentrant()
 {
     $twig = new Apishka_Templater_Environment($this->createMock('Apishka_Templater_LoaderInterface'), array('autoescape' => false, 'optimizations' => 0));
     $twig->addTokenParser(new TestTokenParser());
     $parser = new Apishka_Templater_Parser($twig);
     $parser->parse(new Apishka_Templater_TokenStream(array(new Apishka_Templater_Token(Apishka_Templater_Token::BLOCK_START_TYPE, '', 1), new Apishka_Templater_Token(Apishka_Templater_Token::NAME_TYPE, 'test', 1), new Apishka_Templater_Token(Apishka_Templater_Token::BLOCK_END_TYPE, '', 1), new Apishka_Templater_Token(Apishka_Templater_Token::VAR_START_TYPE, '', 1), new Apishka_Templater_Token(Apishka_Templater_Token::NAME_TYPE, 'foo', 1), new Apishka_Templater_Token(Apishka_Templater_Token::VAR_END_TYPE, '', 1), new Apishka_Templater_Token(Apishka_Templater_Token::EOF_TYPE, '', 1))));
     $this->assertNull($parser->getParent());
 }
Esempio n. 5
0
 /**
  * {@inheritdoc}
  */
 protected function doLeaveNode(Apishka_Templater_NodeAbstract $node, Apishka_Templater_Environment $env)
 {
     if ($this->doLeaveNodeIsSafeAll($node)) {
         // constants are marked safe for all
         $this->setSafe($node, array('all'));
     } elseif ($node instanceof Apishka_Templater_Node_Expression_Conditional) {
         // intersect safeness of both operands
         $safe = $this->intersectSafe($this->getSafe($node->getNode('expr2')), $this->getSafe($node->getNode('expr3')));
         $this->setSafe($node, $safe);
     } elseif ($node instanceof Apishka_Templater_Node_Expression_Filter) {
         // filter expression is safe when the filter is safe
         $name = $node->getNode('filter')->getAttribute('value');
         $args = $node->getNode('arguments');
         if (false !== ($filter = $env->getFilter($name))) {
             $safe = $filter->getSafe($args);
             if (null === $safe) {
                 $safe = $this->intersectSafe($this->getSafe($node->getNode('node')), $filter->getPreservesSafety());
             }
             $this->setSafe($node, $safe);
         } else {
             $this->setSafe($node, array());
         }
     } elseif ($node instanceof Apishka_Templater_Node_Expression_Function) {
         // function expression is safe when the function is safe
         $name = $node->getAttribute('name');
         $args = $node->getNode('arguments');
         $function = $env->getFunction($name);
         if (false !== $function) {
             $this->setSafe($node, $function->getSafe($args));
         } else {
             $this->setSafe($node, array());
         }
     } elseif ($node instanceof Apishka_Templater_Node_Expression_MethodCall) {
         if ($node->getAttribute('safe')) {
             $this->setSafe($node, array('all'));
         } else {
             $this->setSafe($node, array());
         }
     } elseif ($node instanceof Apishka_Templater_Node_Expression_GetAttr && $node->getNode('node') instanceof Apishka_Templater_Node_Expression_Name) {
         $name = $node->getNode('node')->getAttribute('name');
         if (in_array($name, $this->safeVars)) {
             $this->setSafe($node, array('all'));
         } else {
             $this->setSafe($node, array());
         }
     } else {
         $this->setSafe($node, array());
     }
     return $node;
 }
Esempio n. 6
0
 private function preEscapeFilterNode(Apishka_Templater_Node_Expression_Filter $filter, Apishka_Templater_Environment $env)
 {
     $name = $filter->getNode('filter')->getAttribute('value');
     $type = $env->getFilter($name)->getPreEscape();
     if (null === $type) {
         return $filter;
     }
     $node = $filter->getNode('node');
     if ($this->isSafeFor($type, $node, $env)) {
         return $filter;
     }
     $filter->setNode('node', $this->getEscaperFilter($type, $node));
     return $filter;
 }
Esempio n. 7
0
function twig_var_dump(Apishka_Templater_Environment $env, $context)
{
    if (!$env->isDebug()) {
        return;
    }
    ob_start();
    $count = func_num_args();
    if (2 === $count) {
        $vars = array();
        foreach ($context as $key => $value) {
            if (!$value instanceof Apishka_Templater_TemplateAbstract) {
                $vars[$key] = $value;
            }
        }
        var_dump($vars);
    } else {
        for ($i = 2; $i < $count; ++$i) {
            var_dump(func_get_arg($i));
        }
    }
    return ob_get_clean();
}
Esempio n. 8
0
 /**
  * @expectedException LogicException
  * @expectedExceptionMessage Unable to register extension "environment_test" as it is already registered.
  */
 public function testOverrideExtension()
 {
     $twig = new Apishka_Templater_Environment($this->createMock('Apishka_Templater_LoaderInterface'));
     $twig->addExtension(new Apishka_Tests_Templater_EnvironmentTest_Extension());
     $twig->addExtension(new Apishka_Tests_Templater_EnvironmentTest_Extension());
 }
Esempio n. 9
0
/**
 * Loads a template from a string.
 *
 * <pre>
 * {{ include(template_from_string("Hello {{ name }}")) }}
 * </pre>
 *
 * @param Apishka_Templater_Environment $env      A Apishka_Templater_Environment instance
 * @param string                        $template A template as a string or object implementing __toString()
 *
 * @return Apishka_Templater_TemplateAbstract A Apishka_Templater_TemplateAbstract instance
 */
function twig_template_from_string(Apishka_Templater_Environment $env, $template)
{
    return $env->createTemplate((string) $template);
}
Esempio n. 10
0
/**
 * Returns a template content without rendering it.
 *
 * @param Apishka_Templater_Environment $env
 * @param string                        $name          The template name
 * @param bool                          $ignoreMissing Whether to ignore missing templates or not
 *
 * @return string The template source
 */
function twig_source(Apishka_Templater_Environment $env, $name, $ignoreMissing = false)
{
    try {
        return $env->getLoader()->getSource($name);
    } catch (Apishka_Templater_Error_Loader $e) {
        if (!$ignoreMissing) {
            throw $e;
        }
    }
}
Esempio n. 11
0
 /**
  * @expectedException        Apishka_Templater_Error_Syntax
  * @expectedExceptionMessage The "block" function takes at least one argument (the variable and the attributes) in "index" at line 1.
  */
 public function testBlockNoArgs()
 {
     $env = new Apishka_Templater_Environment($this->createMock('Apishka_Templater_LoaderInterface'), array('cache' => false, 'autoescape' => false));
     $parser = new Apishka_Templater_Parser($env);
     $parser->parse($env->tokenize('{{ block() }}', 'index'));
 }