Exemple #1
0
 private function parseToken(Token $token, Stream $stream, RootNode $root)
 {
     $value = $token->getValue();
     switch ($token->getType()) {
         case Token::TEXT:
             $root->addChild(new PrintNode($value));
             break;
         case Token::TAG_START:
             try {
                 $node = $this->environment->getTag($value)->parse($this, $stream);
                 if ($node instanceof Node) {
                     $node->addData('line', $token->getLine());
                     $root->addChild($node);
                 }
             } catch (\OutOfBoundsException $e) {
                 throw new ParseException("Unknown {$value} tag", $token->getLine(), $e);
             }
             break;
         default:
             $type = $token->getTypeString();
             $line = $token->getLine();
             throw new ParseException("Unexpected {$type} ({$value}) token", $line);
     }
     return $stream->next();
 }
 public function __construct(Environment $environment)
 {
     if (self::$environment !== $environment) {
         self::$environment = $environment;
         self::$operators = array_combine($environment->getOperatorSymbols(), $environment->getOperatorSymbols());
         self::$expressionPartsPattern = $this->getExpressionPartsPattern();
     }
 }
 public function testThatDefaultErrorTemplateRespectsCustomDelimiters()
 {
     $this->loader->addTemplate('template', '{*');
     ob_start();
     try {
         $this->env->render('template');
     } catch (TemplatingException $e) {
         $this->fail();
     }
     ob_end_clean();
 }
Exemple #4
0
 public function __construct(Environment $env, $templateName)
 {
     $this->templateName = $templateName;
     $this->baseClass = $env->getOption('template_base_class');
     $className = $env->getTemplateClassName($templateName);
     $classNameOffset = strrpos($className, '\\');
     if ($classNameOffset === false) {
         $this->namespace = '';
         $this->className = $className;
     } else {
         $this->namespace = substr($className, 0, $classNameOffset);
         $this->className = substr($className, $classNameOffset + 1);
     }
 }
Exemple #5
0
 /**
  * @param Node $node
  * @param      $for
  */
 private function addFilterNode(Node $node, $for)
 {
     if (!$this->environment->hasFunction('filter_' . $for)) {
         $for = $this->defaultAutofilterStrategy;
     }
     $node->addChild(new FunctionNode('filter_' . $for, [$node->getChild('expression')]), 'expression');
 }
 /**
  * @test
  * @dataProvider getTests
  */
 public function runIntegrationTests($file, $description, $templates, $data, $expectation, $exception, $exceptionMessage)
 {
     //global counter to provide random namespaces to avoid class name collision
     $options = $this->optionsProperty->getValue($this->environment);
     $options['cache_namespace'] = 'test_' . ++self::$counter;
     $this->optionsProperty->setValue($this->environment, $options);
     foreach ($templates as $name => $template) {
         $this->stringLoader->addTemplate($name, $template);
     }
     if ($data) {
         eval('$data = ' . $data . ';');
     } else {
         $data = [];
     }
     if ($exception) {
         $this->setExpectedException($exception, $exceptionMessage);
     }
     $obLevel = ob_get_level();
     try {
         ob_start();
         $this->environment->render('index', $data);
         $output = rtrim(ob_get_clean(), "\n");
         if ($expectation) {
             $this->assertEquals($expectation, $output, $description . ' (' . $file . ')');
         }
     } catch (\Exception $e) {
         if (ob_get_level() !== $obLevel) {
             ob_end_clean();
         }
         throw $e;
     }
 }
 private function functionNeedsEnvironment(IdentifierNode $node)
 {
     $function = $this->environment->getFunction($node->getData('name'));
     if ($function->getOption('needs_environment')) {
         return true;
     }
     return !is_string($function->getCallback());
 }
Exemple #8
0
 /**
  * @param Template $parent
  * @param          $blockName
  * @param Context  $context
  *
  * @throws \RuntimeException
  */
 private function renderParentBlock(Template $parent, $blockName, Context $context)
 {
     while ($parent->parentTemplate) {
         $parent = $this->environment->load($parent->parentTemplate);
         if (isset($parent->blocks[$blockName])) {
             $parent->blocks[$blockName]($context);
             return;
         }
     }
     throw new \RuntimeException("Block {$blockName} was not found.");
 }
 public function getEnvironment(StringLoader $loader)
 {
     $env = new Environment($loader, ['fallback_tag' => 'print', 'global_variables' => ['global' => 'global variable']]);
     $env->addFunction(new TemplateFunction('html_safe', function ($data) {
         return $data;
     }, ['is_safe' => ['html', 'xml']]));
     $env->addFunction(new TemplateFunction('json_safe', function ($data) {
         return $data;
     }, ['is_safe' => 'json']));
     $env->addFunction(new TemplateFunction('dump', function ($data) {
         return print_r($data, 1);
     }, ['is_safe' => true]));
     $env->addExtension(new Core());
     return $env;
 }
Exemple #10
0
 private function processTag($tag)
 {
     //Try to find the tag name
     preg_match('/(\\S*)(?:\\s*(.*|))$/ADs', $tag, $parts);
     list(, $tagName, $expression) = $parts;
     //If the tag name is unknown, try to use the fallback
     if (isset(self::$closingTags[$tagName])) {
         $tagName = self::$closingTags[$tagName];
     } elseif (!self::$environment->hasTag($tagName)) {
         $tagName = self::$fallbackTagName;
         $expression = $tag;
     }
     $this->pushToken(Token::TAG_START, $tagName);
     self::$expressionTokenizer->setLine($this->line);
     $stream = self::$expressionTokenizer->tokenize($expression);
     while (!$stream->next()->test(Token::EOF)) {
         $this->tokenBuffer[] = $stream->current();
     }
     $this->line = $stream->current()->getLine();
     $this->pushToken(Token::TAG_END, $tagName);
 }
Exemple #11
0
function template_function_source(Environment $environment, $template)
{
    return $environment->getSource($environment->findFirstExistingTemplate($template));
}
 public function getEnvironment(StringLoader $loader)
 {
     $env = new Environment($loader, ['fallback_tag' => 'print', 'error_template' => false]);
     $env->addExtension(new Core());
     return $env;
 }
Exemple #13
0
 /**
  * @dataProvider returnValueProvider
  */
 public function testFunctionReturnValues($function, $arguments, $expected)
 {
     $actual = call_user_func_array($this->env->getFunction($function)->getCallback(), $arguments);
     $this->assertEquals($expected, $actual);
 }
Exemple #14
0
 /**
  * @inheritdoc
  */
 public function isCacheFresh($template)
 {
     $cacheTime = $this->environment->getTemplateCache()->getCreatedTime($this->getCacheKey($template));
     return filemtime($this->getPath($template)) < $cacheTime;
 }
Exemple #15
0
 /**
  * @inheritdoc
  */
 public function isCacheFresh($template)
 {
     return $this->environment->getTemplateCache()->exists($this->getCacheKey($template));
 }
Exemple #16
0
 public function __construct(Environment $environment)
 {
     $this->binaryOperators = $environment->getBinaryOperators();
     $this->unaryPrefixOperators = $environment->getUnaryPrefixOperators();
     $this->unaryPostfixOperators = $environment->getUnaryPostfixOperators();
 }