本文整理汇总了PHP中Twig_Parser类的典型用法代码示例。如果您正苦于以下问题:PHP Twig_Parser类的具体用法?PHP Twig_Parser怎么用?PHP Twig_Parser使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Twig_Parser类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testStringExpression
/**
* @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'));
}
示例2: testArraySyntaxError
/**
* @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'));
}
示例3: testCompile
/**
* @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));
}
示例4: testCompile
/**
* @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: getParser
/**
* @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;
}
示例6: testCompile
/**
* @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));
}
示例7: testCompile
/**
* @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);
}
示例8: parseTestExpression
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: parseTestExpression
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: getParserForFilterBodyNodes
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);
}
示例11: __construct
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: getTestNodeClass
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';
}
示例13: twig_parse
/**
* 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();
}
示例14: moveToNextNode
public function moveToNextNode(\Twig_Parser $parser)
{
$parser->getStream()->next();
$parser->getStream()->expect(\Twig_Token::BLOCK_START_TYPE);
}
示例15: testUnknownTest
/**
* @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'));
}