/**
  * @dataProvider getTestsForString
  */
 public function testStringExpression($template, $expected)
 {
     $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false, 'optimizations' => 0));
     $stream = $env->tokenize($template, 'index');
     $parser = new Twig_Parser($env);
     $this->assertEquals($expected, $parser->parse($stream)->getNode('body')->getNode(0)->getNode('expr'));
 }
    /**
     * @expectedException Twig_Error_Syntax
     * @dataProvider getFailingTestsForArray
     */
    public function testArraySyntaxError($template)
    {
        $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false));
        $parser = new Twig_Parser($env);

        $parser->parse($env->tokenize($template, 'index'));
    }
 /**
  * @dataProvider getTestsForFormTheme
  */
 public function testCompile($source, $expected)
 {
     $env = new \Twig_Environment($this->getMock('Twig_LoaderInterface'), array('cache' => false, 'autoescape' => false, 'optimizations' => 0));
     $env->addTokenParser(new FormThemeTokenParser());
     $stream = $env->tokenize(new \Twig_Source($source, ''));
     $parser = new \Twig_Parser($env);
     $this->assertEquals($expected, $parser->parse($stream)->getNode('body')->getNode(0));
 }
 /**
  * @dataProvider getTestsForRender
  */
 public function testCompile($source, $expected)
 {
     $env = new \Twig_Environment(new \Twig_Loader_String(), array('cache' => false, 'autoescape' => false, 'optimizations' => 0));
     $env->addTokenParser(new RenderTokenParser());
     $stream = $env->tokenize($source);
     $parser = new \Twig_Parser($env);
     $this->assertEquals($expected, $parser->parse($stream)->getNode('body')->getNode(0));
 }
예제 #5
0
 /**
  * @return \Twig_Parser
  * @throws \Exception
  * @throws \Twig_Error_Syntax
  */
 private function getParser()
 {
     $stream = new \Twig_TokenStream(array(new \Twig_Token(\Twig_Token::TEXT_TYPE, '<!DOCTYPE html><html><body>', 1), new \Twig_Token(\Twig_Token::BLOCK_START_TYPE, '', 2), new \Twig_Token(\Twig_Token::NAME_TYPE, 'form', 2), new \Twig_Token(\Twig_Token::PUNCTUATION_TYPE, '(', 2), new \Twig_Token(\Twig_Token::NAME_TYPE, 'method', 2), new \Twig_Token(\Twig_Token::OPERATOR_TYPE, '=', 2), new \Twig_Token(\Twig_Token::STRING_TYPE, 'post', 2), new \Twig_Token(\Twig_Token::PUNCTUATION_TYPE, ',', 2), new \Twig_Token(\Twig_Token::NAME_TYPE, 'action', 2), new \Twig_Token(\Twig_Token::OPERATOR_TYPE, '=', 2), new \Twig_Token(\Twig_Token::NAME_TYPE, 'request_uri', 2), new \Twig_Token(\Twig_Token::PUNCTUATION_TYPE, '|', 2), new \Twig_Token(\Twig_Token::NAME_TYPE, 'e', 2), new \Twig_Token(\Twig_Token::PUNCTUATION_TYPE, ')', 2), new \Twig_Token(\Twig_Token::BLOCK_END_TYPE, '', 2), new \Twig_Token(\Twig_Token::TEXT_TYPE, '<input type="submit" value="Submit">', 3), new \Twig_Token(\Twig_Token::BLOCK_START_TYPE, '', 4), new \Twig_Token(\Twig_Token::NAME_TYPE, 'endform', 4), new \Twig_Token(\Twig_Token::BLOCK_END_TYPE, '', 4), new \Twig_Token(\Twig_Token::TEXT_TYPE, '</body></html>', 5), new \Twig_Token(\Twig_Token::EOF_TYPE, '', 5)));
     $env = new \Twig_Environment();
     $env->addTokenParser(new FormTokenParser());
     $parser = new \Twig_Parser($env);
     $parser->parse($stream);
     return $parser;
 }
 /**
  * @dataProvider getTestsForRender
  */
 public function testCompile($enabled, $source, $expected)
 {
     $translator = $this->getMock('Symfony\\Component\\Translation\\TranslatorInterface');
     $env = new \Twig_Environment(new \Twig_Loader_String(), array('cache' => false, 'autoescape' => false, 'optimizations' => 0));
     $env->addTokenParser(new TemplateBoxTokenParser($enabled, $translator));
     $stream = $env->tokenize($source);
     $parser = new \Twig_Parser($env);
     $this->assertEquals($expected, $parser->parse($stream)->getNode('body')->getNode(0));
 }
 /**
  * @dataProvider getCompileTestData
  *
  * @param ContentEditableNode $expectedNode
  * @param string              $source
  * @param string              $message
  */
 public function testCompile(ContentEditableNode $expectedNode, $source, $message = '')
 {
     $env = new \Twig_Environment($this->getMock('Twig_LoaderInterface'), ['cache' => false, 'autoescape' => false, 'optimizations' => 0]);
     $env->addTokenParser(new ContentEditableTokenParser());
     $parser = new \Twig_Parser($env);
     $stream = $env->tokenize($source);
     $node = $parser->parse($stream)->getNode('body')->getNode(0);
     $this->assertEquals($expectedNode, $node, $message);
 }
 public function parseTestExpression(Twig_Parser $parser, $node)
 {
     $stream = $parser->getStream();
     $name = $stream->expect(Twig_Token::NAME_TYPE);
     $arguments = null;
     if ($stream->test(Twig_Token::PUNCTUATION_TYPE, '(')) {
         $arguments = $parser->getExpressionParser()->parseArguments();
     }
     return new Twig_Node_Expression_Test($node, $name->getValue(), $arguments, $parser->getCurrentToken()->getLine());
 }
예제 #9
0
 public function parseTestExpression(Twig_Parser $parser, $node)
 {
     $stream = $parser->getStream();
     $name = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
     $arguments = null;
     if ($stream->test(Twig_Token::PUNCTUATION_TYPE, '(')) {
         $arguments = $parser->getExpressionParser()->parseArguments();
     }
     $class = $this->getTestNodeClass($parser->getEnvironment(), $name);
     return new $class($node, $name, $arguments, $parser->getCurrentToken()->getLine());
 }
예제 #10
0
 protected function getParserForFilterBodyNodes()
 {
     $invoker = new ReflectionMethod('Twig_Parser', 'filterBodyNodes');
     $invoker->setAccessible(true);
     $p = new ReflectionProperty('Twig_Parser', 'stream');
     $p->setAccessible(true);
     $parser = new Twig_Parser(new Twig_Environment());
     $parser->setParent(new Twig_Node());
     $p->setValue($parser, $this->getMockBuilder('Twig_TokenStream')->disableOriginalConstructor()->getMock());
     return array($parser, $invoker);
 }
 public function __construct(Twig_Parser $parser, Twig_Environment $env = null)
 {
     $this->parser = $parser;
     if ($env instanceof Twig_Environment) {
         $this->env = $env;
         $this->unaryOperators = $env->getUnaryOperators();
         $this->binaryOperators = $env->getBinaryOperators();
     } else {
         @trigger_error('Passing the operators as constructor arguments to ' . __METHOD__ . ' is deprecated since version 1.27. Pass the environment instead.', E_USER_DEPRECATED);
         $this->env = $parser->getEnvironment();
         $this->unaryOperators = func_get_arg(1);
         $this->binaryOperators = func_get_arg(2);
     }
 }
예제 #12
0
 public function subparse($test, $dropNeedle = false)
 {
     if (!is_object($this->handlers)) {
         $this->handlers = new Zwig_FallbackArray($this->handlers);
     }
     return parent::subparse($test, $dropNeedle);
 }
 private function parseInline(\Twig_TokenStream $stream, $lineno)
 {
     $stream->expect(\Twig_Token::BLOCK_END_TYPE);
     $this->parser->subparse(function ($token) {
         return $token->test(['end' . $this->getTag()]);
     }, true);
     $stream->expect(\Twig_Token::BLOCK_END_TYPE);
     $file = $this->parser->getEnvironment()->getLoader()->getCacheKey($stream->getFilename());
     if (!isset($this->inline_blocks[$file])) {
         $this->inline_blocks[$file] = 0;
     }
     $file_name = md5($file . $this->inline_blocks[$file]) . ".js";
     $assets = $this->extension->webpackAsset('cache.' . $file_name);
     $this->inline_blocks[$file]++;
     return new WebpackInlineNode(['js_file' => $assets['js'], 'css_file' => $assets['css']], $lineno, $this->getTag());
 }
예제 #14
0
 protected function filterBodyNodes(\Twig_NodeInterface $node)
 {
     // Bypass "include" nodes as they "capture" the output
     if ($node instanceof IncludeResource_Node) {
         return $node;
     }
     return parent::filterBodyNodes($node);
 }
예제 #15
0
파일: Core.php 프로젝트: TiGR/Twig
 protected function getTestNodeClass(Twig_Parser $parser, $name, $line)
 {
     $env = $parser->getEnvironment();
     $testMap = $env->getTests();
     $testName = null;
     if (isset($testMap[$name])) {
         $testName = $name;
     } elseif ($parser->getStream()->test(Twig_Token::NAME_TYPE)) {
         // try 2-words tests
         $name = $name . ' ' . $parser->getCurrentToken()->getValue();
         if (isset($testMap[$name])) {
             $parser->getStream()->next();
             $testName = $name;
         }
     }
     if (null === $testName) {
         $message = sprintf('The test "%s" does not exist', $name);
         if ($alternatives = $env->computeAlternatives($name, array_keys($env->getTests()))) {
             $message = sprintf('%s. Did you mean "%s"', $message, implode('", "', $alternatives));
         }
         throw new Twig_Error_Syntax($message, $line, $parser->getFilename());
     }
     if ($testMap[$name] instanceof Twig_SimpleTest) {
         return $testMap[$name]->getNodeClass();
     }
     return $testMap[$name] instanceof Twig_Test_Node ? $testMap[$name]->getClass() : 'Twig_Node_Expression_Test';
 }
예제 #16
0
 protected function getTestNodeClass(Twig_Parser $parser, $name, $line)
 {
     $env = $parser->getEnvironment();
     $testMap = $env->getTests();
     if (!isset($testMap[$name])) {
         $message = sprintf('The test "%s" does not exist', $name);
         if ($alternatives = $env->computeAlternatives($name, array_keys($env->getTests()))) {
             $message = sprintf('%s. Did you mean "%s"', $message, implode('", "', $alternatives));
         }
         throw new Twig_Error_Syntax($message, $line, $parser->getFilename());
     }
     if ($testMap[$name] instanceof Twig_SimpleTest) {
         return $testMap[$name]->getNodeClass();
     }
     return $testMap[$name] instanceof Twig_Test_Node ? $testMap[$name]->getClass() : 'Twig_Node_Expression_Test';
 }
예제 #17
0
 public function filterBodyNodes(Twig_NodeInterface $node)
 {
     return parent::filterBodyNodes($node);
 }
예제 #18
0
파일: parser.php 프로젝트: betsyzhang/chyrp
/**
 * Twig::Parser
 * ~~~~~~~~~~~~
 *
 * This module implements the Twig parser.
 *
 * :copyright: 2008 by Armin Ronacher.
 * :license: BSD.
 */
function twig_parse($source, $filename = NULL)
{
    $stream = twig_tokenize($source, $filename);
    $parser = new Twig_Parser($stream);
    return $parser->parse();
}
 public function moveToNextNode(\Twig_Parser $parser)
 {
     $parser->getStream()->next();
     $parser->getStream()->expect(\Twig_Token::BLOCK_START_TYPE);
 }
예제 #20
0
 /**
  * @expectedException        Twig_Error_Syntax
  * @expectedExceptionMessage The test "nul" does not exist. Did you mean "null" in "index" at line 1
  */
 public function testUnknownTest()
 {
     $env = new Twig_Environment($this->getMock('Twig_LoaderInterface'), array('cache' => false, 'autoescape' => false));
     $parser = new Twig_Parser($env);
     $parser->parse($env->tokenize('{{ 1 is nul }}', 'index'));
 }
 /**
  * @expectedException Twig_Error_Syntax
  */
 public function testSetMacroThrowsExceptionOnReservedMethods()
 {
     $parser = new Twig_Parser(new Twig_Environment());
     $parser->setMacro('display', $this->getMock('Twig_Node_Macro', null, array(), '', null));
 }
예제 #22
0
 protected function getParser()
 {
     $parser = new Twig_Parser(new Twig_Environment($this->getMock('Twig_LoaderInterface')));
     $parser->setParent(new Twig_Node());
     $p = new ReflectionProperty($parser, 'stream');
     $p->setAccessible(true);
     $p->setValue($parser, $this->getMockBuilder('Twig_TokenStream')->disableOriginalConstructor()->getMock());
     return $parser;
 }
예제 #23
0
 /**
  * Replace Twig_Node_Module with Curry_Twig_Node_Module
  *
  * @param Twig_TokenStream $stream
  * @return Curry_Twig_Node_Module
  */
 public function parse(Twig_TokenStream $stream)
 {
     $node = parent::parse($stream);
     // Convert Twig_Node_Module to Curry_Twig_Node_Module
     return new Curry_Twig_Node_Module($node->getNode('body'), $node->getNode('parent'), $node->getNode('blocks'), $node->getNode('macros'), $node->getNode('traits'), $node->getAttribute('embedded_templates'), $node->getAttribute('filename'));
 }
예제 #24
0
 /**
  * @expectedException        Twig_Error_Syntax
  * @expectedExceptionMessage The test "nul" does not exist. Did you mean "null" in "index" at line 1
  */
 public function testUnknownTest()
 {
     $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false));
     $parser = new Twig_Parser($env);
     $parser->parse($env->tokenize('{{ 1 is nul }}', 'index'));
 }
예제 #25
0
파일: Core.php 프로젝트: stler/NMFrame
 protected function getTestNodeClass(Twig_Parser $parser, $name)
 {
     $env = $parser->getEnvironment();
     $testMap = $env->getTests();
     if ($testMap[$name] instanceof Twig_SimpleTest) {
         return $testMap[$name]->getNodeClass();
     }
     return $testMap[$name] instanceof Twig_Test_Node ? $testMap[$name]->getClass() : 'Twig_Node_Expression_Test';
 }
예제 #26
0
파일: Core.php 프로젝트: hoque92/hoquezilla
 protected function getTest(Twig_Parser $parser, $line)
 {
     $stream = $parser->getStream();
     $name = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
     $env = $parser->getEnvironment();
     if ($test = $env->getTest($name)) {
         return array($name, $test);
     }
     if ($stream->test(Twig_Token::NAME_TYPE)) {
         // try 2-words tests
         $name = $name . ' ' . $parser->getCurrentToken()->getValue();
         if ($test = $env->getTest($name)) {
             $parser->getStream()->next();
             return array($name, $test);
         }
     }
     $e = new Twig_Error_Syntax(sprintf('Unknown "%s" test.', $name), $line, $parser->getFilename());
     $e->addSuggestions($name, array_keys($env->getTests()));
     throw $e;
 }
예제 #27
0
 protected function getTestName(Twig_Parser $parser, $line)
 {
     $stream = $parser->getStream();
     $name = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
     $env = $parser->getEnvironment();
     $testMap = $env->getTests();
     if (isset($testMap[$name])) {
         return $name;
     }
     if ($stream->test(Twig_Token::NAME_TYPE)) {
         // try 2-words tests
         $name = $name . ' ' . $parser->getCurrentToken()->getValue();
         if (isset($testMap[$name])) {
             $parser->getStream()->next();
             return $name;
         }
     }
     $message = sprintf('The test "%s" does not exist', $name);
     if ($alternatives = $env->computeAlternatives($name, array_keys($testMap))) {
         $message = sprintf('%s. Did you mean "%s"', $message, implode('", "', $alternatives));
     }
     throw new Twig_Error_Syntax($message, $line, $parser->getFilename());
 }
예제 #28
0
 /**
  * @expectedException        Twig_Error_Syntax
  * @expectedExceptionMessage Unknown "foobar" test in "index" at line 1.
  */
 public function testUnknownTestWithoutSuggestions()
 {
     $env = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock(), array('cache' => false, 'autoescape' => false));
     $parser = new Twig_Parser($env);
     $parser->parse($env->tokenize(new Twig_Source('{{ 1 is foobar }}', 'index')));
 }