Matthias Andreas Benkard | 12a5735 | 2021-12-28 18:02:04 +0100 | [diff] [blame^] | 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 | |
| 12 | namespace Twig\TokenParser; |
| 13 | |
| 14 | use Twig\Node\Node; |
| 15 | use Twig\Node\WithNode; |
| 16 | use Twig\Token; |
| 17 | |
| 18 | /** |
| 19 | * Creates a nested scope. |
| 20 | * |
| 21 | * @author Fabien Potencier <fabien@symfony.com> |
| 22 | * |
| 23 | * @internal |
| 24 | */ |
| 25 | final class WithTokenParser extends AbstractTokenParser |
| 26 | { |
| 27 | public function parse(Token $token): Node |
| 28 | { |
| 29 | $stream = $this->parser->getStream(); |
| 30 | |
| 31 | $variables = null; |
| 32 | $only = false; |
| 33 | if (!$stream->test(/* Token::BLOCK_END_TYPE */ 3)) { |
| 34 | $variables = $this->parser->getExpressionParser()->parseExpression(); |
| 35 | $only = (bool) $stream->nextIf(/* Token::NAME_TYPE */ 5, 'only'); |
| 36 | } |
| 37 | |
| 38 | $stream->expect(/* Token::BLOCK_END_TYPE */ 3); |
| 39 | |
| 40 | $body = $this->parser->subparse([$this, 'decideWithEnd'], true); |
| 41 | |
| 42 | $stream->expect(/* Token::BLOCK_END_TYPE */ 3); |
| 43 | |
| 44 | return new WithNode($body, $variables, $only, $token->getLine(), $this->getTag()); |
| 45 | } |
| 46 | |
| 47 | public function decideWithEnd(Token $token): bool |
| 48 | { |
| 49 | return $token->test('endwith'); |
| 50 | } |
| 51 | |
| 52 | public function getTag(): string |
| 53 | { |
| 54 | return 'with'; |
| 55 | } |
| 56 | } |