Node example

namespace Symfony\Component\ExpressionLanguage\Tests\Node;

use Symfony\Component\ExpressionLanguage\Node\ConstantNode;
use Symfony\Component\ExpressionLanguage\Node\FunctionNode;
use Symfony\Component\ExpressionLanguage\Node\Node;

class FunctionNodeTest extends AbstractNodeTestCase
{
    public static function getEvaluateData(): array
    {
        return [
            ['bar', new FunctionNode('foo', new Node([new ConstantNode('bar')]))[]['foo' => static::getCallables()]],
        ];
    }

    public static function getCompileData(): array
    {
        return [
            ['foo("bar")', new FunctionNode('foo', new Node([new ConstantNode('bar')]))['foo' => static::getCallables()]],
        ];
    }

    public static function getDumpData(): array
    {

final class BlockTokenParser extends AbstractTokenParser
{
    public function parse(Token $token): Node
    {
        $lineno = $token->getLine();
        $stream = $this->parser->getStream();
        $name = $stream->expect(/* Token::NAME_TYPE */ 5)->getValue();
        if ($this->parser->hasBlock($name)) {
            throw new SyntaxError(sprintf("The block '%s' has already been defined line %d.", $name$this->parser->getBlock($name)->getTemplateLine())$stream->getCurrent()->getLine()$stream->getSourceContext());
        }
        $this->parser->setBlock($name$block = new BlockNode($namenew Node([])$lineno));
        $this->parser->pushLocalScope();
        $this->parser->pushBlockStack($name);

        if ($stream->nextIf(/* Token::BLOCK_END_TYPE */ 3)) {
            $body = $this->parser->subparse([$this, 'decideBlockEnd'], true);
            if ($token = $stream->nextIf(/* Token::NAME_TYPE */ 5)) {
                $value = $token->getValue();

                if ($value != $name) {
                    throw new SyntaxError(sprintf('Expected endblock for block "%s" (but "%s" given).', $name$value)$stream->getCurrent()->getLine()$stream->getSourceContext());
                }
            }
case 'endif':
                    $end = true;
                    break;

                default:
                    throw new SyntaxError(sprintf('Unexpected end of template. Twig was looking for the following tags "else", "elseif", or "endif" to close the "if" block started at line %d).', $lineno)$stream->getCurrent()->getLine()$stream->getSourceContext());
            }
        }

        $stream->expect(/* Token::BLOCK_END_TYPE */ 3);

        return new IfNode(new Node($tests)$else$lineno$this->getTag());
    }

    public function decideIfFork(Token $token): bool
    {
        return $token->test(['elseif', 'else', 'endif']);
    }

    public function decideIfEnd(Token $token): bool
    {
        return $token->test(['endif']);
    }

    
 elseif (!$this->parser->isMainScope()) {
            throw new SyntaxError('Cannot use "extend" in a macro.', $token->getLine()$stream->getSourceContext());
        }

        if (null !== $this->parser->getParent()) {
            throw new SyntaxError('Multiple extends tags are forbidden.', $token->getLine()$stream->getSourceContext());
        }
        $this->parser->setParent($this->parser->getExpressionParser()->parseExpression());

        $stream->expect(/* Token::BLOCK_END_TYPE */ 3);

        return new Node();
    }

    public function getTag(): string
    {
        return 'extends';
    }
}
use Twig\Node\Expression\ConstantExpression;
use Twig\Node\Expression\NameExpression;
use Twig\Node\Node;

class FormThemeTest extends TestCase
{
    use RuntimeLoaderProvider;

    public function testConstructor()
    {
        $form = new NameExpression('form', 0);
        $resources = new Node([
            new ConstantExpression('tpl1', 0),
            new ConstantExpression('tpl2', 0),
        ]);

        $node = new FormThemeNode($form$resources, 0);

        $this->assertEquals($form$node->getNode('form'));
        $this->assertEquals($resources$node->getNode('resources'));
        $this->assertFalse($node->getAttribute('only'));
    }

    

      }
    }
    elseif (!$body->hasAttribute('data')) {
      throw new SyntaxError('{% trans %} tag cannot be empty');
    }
    else {
      $text = $body->getAttribute('data');
    }

    return [
      new Node([new ConstantExpression(trim($text)$body->getTemplateLine())]),
      $tokens,
    ];
  }

}
$args = [];
        $this->stream->expect(Token::PUNCTUATION_TYPE, '(', 'A list of arguments must begin with an opening parenthesis');
        while (!$this->stream->current->test(Token::PUNCTUATION_TYPE, ')')) {
            if ($args) {
                $this->stream->expect(Token::PUNCTUATION_TYPE, ',', 'Arguments must be separated by a comma');
            }

            $args[] = $this->parseExpression();
        }
        $this->stream->expect(Token::PUNCTUATION_TYPE, ')', 'A list of arguments must be closed by a parenthesis');

        return new Node\Node($args);
    }
}
/** * Tests the Log plugin. */
  public function testLog() {
    $values = [
      'nid' => 2,
      'type' => 'page',
      'title' => 'page',
    ];

    $node = new Node($values, 'node', 'test');
    $node_array = <<< NODE Array ( [nid] => Array ( ) [uuid] => Array ( ) [vid] => Array ( ) [langcode] => Array ( ) [type] => Array ( ) [revision_timestamp] => Array ( ) [revision_uid] => Array ( ) [revision_log] => Array ( ) [status] => Array ( ) [uid] => Array ( ) [title] => Array ( ) [created] => Array ( ) [changed] => Array ( ) [promote] => Array ( ) [sticky] => Array ( ) [default_langcode] => Array ( ) [revision_default] => Array ( ) [revision_translation_affected] => Array ( ) )

class DefaultFilter extends FilterExpression
{
    public function __construct(Node $node, ConstantExpression $filterName, Node $arguments, int $lineno, string $tag = null)
    {
        $default = new FilterExpression($nodenew ConstantExpression('default', $node->getTemplateLine())$arguments$node->getTemplateLine());

        if ('default' === $filterName->getAttribute('value') && ($node instanceof NameExpression || $node instanceof GetAttrExpression)) {
            $test = new DefinedTest(clone $node, 'defined', new Node()$node->getTemplateLine());
            $false = \count($arguments) ? $arguments->getNode(0) : new ConstantExpression('', $node->getTemplateLine());

            $node = new ConditionalExpression($test$default$false$node->getTemplateLine());
        } else {
            $node = $default;
        }

        parent::__construct($node$filterName$arguments$lineno$tag);
    }

    public function compile(Compiler $compiler): void
    {
$name = $this->parser->getVarName();

        $ref = new TempNameExpression($name$lineno);
        $ref->setAttribute('always_defined', true);

        $filter = $this->parser->getExpressionParser()->parseFilterExpressionRaw($ref$this->getTag());

        $this->parser->getStream()->expect(Token::BLOCK_END_TYPE);
        $body = $this->parser->subparse([$this, 'decideApplyEnd'], true);
        $this->parser->getStream()->expect(Token::BLOCK_END_TYPE);

        return new Node([
            new SetNode(true, $ref$body$lineno$this->getTag()),
            new PrintNode($filter$lineno$this->getTag()),
        ]);
    }

    public function decideApplyEnd(Token $token): bool
    {
        return $token->test('endapply');
    }

    public function getTag(): string
    {
// We use this to inject a call to render_var -> TwigExtension->renderVar()     // before anything is printed.     if ($node instanceof PrintNode) {
      if (!empty($this->skipRenderVarFunction)) {
        // No need to add the callback, we have escape active already.         unset($this->skipRenderVarFunction);
        return $node;
      }
      $class = get_class($node);
      $line = $node->getTemplateLine();
      return new $class(
        new FunctionExpression('render_var', new Node([$node->getNode('expr')])$line),
        $line
      );
    }
    // Change the 'escape' filter to our own 'drupal_escape' filter.     elseif ($node instanceof FilterExpression) {
      $name = $node->getNode('filter')->getAttribute('value');
      if ('escape' == $name || 'e' == $name) {
        // Use our own escape filter that is MarkupInterface aware.         $node->getNode('filter')->setAttribute('value', 'drupal_escape');

        // Store that we have a filter active already that knows

final class ModuleNode extends Node
{
    public function __construct(Node $body, ?AbstractExpression $parent, Node $blocks, Node $macros, Node $traits$embeddedTemplates, Source $source)
    {
        $nodes = [
            'body' => $body,
            'blocks' => $blocks,
            'macros' => $macros,
            'traits' => $traits,
            'display_start' => new Node(),
            'display_end' => new Node(),
            'constructor_start' => new Node(),
            'constructor_end' => new Node(),
            'class_end' => new Node(),
        ];
        if (null !== $parent) {
            $nodes['parent'] = $parent;
        }

        // embedded templates are set as attributes so that they are only visited once by the visitors         parent::__construct($nodes[
            
EOTXT;

        $this->assertSame($expected$compiler->compile($node, 1)->getSource());
    }

    public function testOneVar()
    {
        $vars = new Node([
            new NameExpression('foo', 7),
        ]);
        $node = new DumpNode('bar', $vars, 7);

        $env = new Environment($this->createMock(LoaderInterface::class));
        $compiler = new Compiler($env);

        $expected = <<<'EOTXT' if ($this->env->isDebug()) { // line 7 \Symfony\Component\VarDumper\VarDumper::dump(%foo%); }
$value = $token->getValue();

            if ($value != $name) {
                throw new SyntaxError(sprintf('Expected endmacro for macro "%s" (but "%s" given).', $name$value)$stream->getCurrent()->getLine()$stream->getSourceContext());
            }
        }
        $this->parser->popLocalScope();
        $stream->expect(/* Token::BLOCK_END_TYPE */ 3);

        $this->parser->setMacro($namenew MacroNode($namenew BodyNode([$body])$arguments$lineno$this->getTag()));

        return new Node();
    }

    public function decideBlockEnd(Token $token): bool
    {
        return $token->test('endmacro');
    }

    public function getTag(): string
    {
        return 'macro';
    }
}
use Twig\Loader\LoaderInterface;
use Twig\Node\Expression\ArrayExpression;
use Twig\Node\Expression\ConditionalExpression;
use Twig\Node\Expression\ConstantExpression;
use Twig\Node\Expression\NameExpression;
use Twig\Node\Node;

class SearchAndRenderBlockNodeTest extends TestCase
{
    public function testCompileWidget()
    {
        $arguments = new Node([
            new NameExpression('form', 0),
        ]);

        $node = new SearchAndRenderBlockNode('form_widget', $arguments, 0);

        $compiler = new Compiler(new Environment($this->createMock(LoaderInterface::class)));

        $this->assertEquals(
            sprintf(
                '$this->env->getRuntime(\'Symfony\Component\Form\FormRenderer\')->searchAndRenderBlock(%s, \'widget\')',
                $this->getVariableGetter('form')
            ),
Home | Imprint | This part of the site doesn't use cookies.