summaryrefslogtreecommitdiff
path: root/vendor/doctrine/orm/src/Query/TreeWalkerChain.php
blob: 7bb30511de0fb3fad5bad3b76ad6da6fa3a21ee0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php

declare(strict_types=1);

namespace Doctrine\ORM\Query;

use Doctrine\ORM\AbstractQuery;
use Generator;

/**
 * Represents a chain of tree walkers that modify an AST and finally emit output.
 * Only the last walker in the chain can emit output. Any previous walkers can modify
 * the AST to influence the final output produced by the last walker.
 *
 * @psalm-import-type QueryComponent from Parser
 */
class TreeWalkerChain implements TreeWalker
{
    /**
     * The tree walkers.
     *
     * @var string[]
     * @psalm-var list<class-string<TreeWalker>>
     */
    private array $walkers = [];

    /**
     * {@inheritDoc}
     */
    public function __construct(
        private readonly AbstractQuery $query,
        private readonly ParserResult $parserResult,
        private array $queryComponents,
    ) {
    }

    /**
     * Returns the internal queryComponents array.
     *
     * {@inheritDoc}
     */
    public function getQueryComponents(): array
    {
        return $this->queryComponents;
    }

    /**
     * Adds a tree walker to the chain.
     *
     * @param string $walkerClass The class of the walker to instantiate.
     * @psalm-param class-string<TreeWalker> $walkerClass
     */
    public function addTreeWalker(string $walkerClass): void
    {
        $this->walkers[] = $walkerClass;
    }

    public function walkSelectStatement(AST\SelectStatement $selectStatement): void
    {
        foreach ($this->getWalkers() as $walker) {
            $walker->walkSelectStatement($selectStatement);

            $this->queryComponents = $walker->getQueryComponents();
        }
    }

    public function walkUpdateStatement(AST\UpdateStatement $updateStatement): void
    {
        foreach ($this->getWalkers() as $walker) {
            $walker->walkUpdateStatement($updateStatement);
        }
    }

    public function walkDeleteStatement(AST\DeleteStatement $deleteStatement): void
    {
        foreach ($this->getWalkers() as $walker) {
            $walker->walkDeleteStatement($deleteStatement);
        }
    }

    /** @psalm-return Generator<int, TreeWalker> */
    private function getWalkers(): Generator
    {
        foreach ($this->walkers as $walkerClass) {
            yield new $walkerClass($this->query, $this->parserResult, $this->queryComponents);
        }
    }
}