blob: c7a9d6984e70fb1622408d76865b324b8e3606c3 [file] [log] [blame]
Matthias Andreas Benkard12a57352021-12-28 18:02:04 +01001<?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\Node;
13
14use Twig\Compiler;
15use Twig\Node\Expression\AbstractExpression;
16
17/**
18 * Checks if casting an expression to __toString() is allowed by the sandbox.
19 *
20 * For instance, when there is a simple Print statement, like {{ article }},
21 * and if the sandbox is enabled, we need to check that the __toString()
22 * method is allowed if 'article' is an object. The same goes for {{ article|upper }}
23 * or {{ random(article) }}
24 *
25 * @author Fabien Potencier <fabien@symfony.com>
26 */
27class CheckToStringNode extends AbstractExpression
28{
29 public function __construct(AbstractExpression $expr)
30 {
31 parent::__construct(['expr' => $expr], [], $expr->getTemplateLine(), $expr->getNodeTag());
32 }
33
34 public function compile(Compiler $compiler): void
35 {
36 $expr = $this->getNode('expr');
37 $compiler
38 ->raw('$this->sandbox->ensureToStringAllowed(')
39 ->subcompile($expr)
40 ->raw(', ')
41 ->repr($expr->getTemplateLine())
42 ->raw(', $this->source)')
43 ;
44 }
45}