1<?php 2 3/* 4 * This file is part of Twig. 5 * 6 * (c) Fabien Potencier 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12namespace Twig\TokenParser; 13 14use Twig\Node\WithNode; 15use Twig\Token; 16 17/** 18 * Creates a nested scope. 19 * 20 * @author Fabien Potencier <fabien@symfony.com> 21 * 22 * @final 23 */ 24class WithTokenParser extends AbstractTokenParser 25{ 26 public function parse(Token $token) 27 { 28 $stream = $this->parser->getStream(); 29 30 $variables = null; 31 $only = false; 32 if (!$stream->test(Token::BLOCK_END_TYPE)) { 33 $variables = $this->parser->getExpressionParser()->parseExpression(); 34 $only = $stream->nextIf(Token::NAME_TYPE, 'only'); 35 } 36 37 $stream->expect(Token::BLOCK_END_TYPE); 38 39 $body = $this->parser->subparse([$this, 'decideWithEnd'], true); 40 41 $stream->expect(Token::BLOCK_END_TYPE); 42 43 return new WithNode($body, $variables, $only, $token->getLine(), $this->getTag()); 44 } 45 46 public function decideWithEnd(Token $token) 47 { 48 return $token->test('endwith'); 49 } 50 51 public function getTag() 52 { 53 return 'with'; 54 } 55} 56 57class_alias('Twig\TokenParser\WithTokenParser', 'Twig_TokenParser_With'); 58