TreeWalkerChain.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\ORM\Query;
  20. use Doctrine\ORM\Mapping\ClassMetadata;
  21. use function array_diff;
  22. use function array_keys;
  23. /**
  24. * Represents a chain of tree walkers that modify an AST and finally emit output.
  25. * Only the last walker in the chain can emit output. Any previous walkers can modify
  26. * the AST to influence the final output produced by the last walker.
  27. */
  28. class TreeWalkerChain implements TreeWalker
  29. {
  30. /**
  31. * The tree walkers.
  32. *
  33. * @var TreeWalker[]
  34. * @psalm-var TreeWalkerChainIterator
  35. */
  36. private $_walkers;
  37. /**
  38. * The query components of the original query (the "symbol table") that was produced by the Parser.
  39. *
  40. * @var array<string, array<string, mixed>>
  41. * @psalm-var array<string, array{
  42. * metadata: ClassMetadata,
  43. * parent: string,
  44. * relation: mixed[],
  45. * map: mixed,
  46. * nestingLevel: int,
  47. * token: array
  48. * }>
  49. */
  50. private $_queryComponents;
  51. /**
  52. * Returns the internal queryComponents array.
  53. *
  54. * {@inheritDoc}
  55. */
  56. public function getQueryComponents()
  57. {
  58. return $this->_queryComponents;
  59. }
  60. /**
  61. * {@inheritdoc}
  62. *
  63. * @return void
  64. */
  65. public function setQueryComponent($dqlAlias, array $queryComponent)
  66. {
  67. $requiredKeys = ['metadata', 'parent', 'relation', 'map', 'nestingLevel', 'token'];
  68. if (array_diff($requiredKeys, array_keys($queryComponent))) {
  69. throw QueryException::invalidQueryComponent($dqlAlias);
  70. }
  71. $this->_queryComponents[$dqlAlias] = $queryComponent;
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function __construct($query, $parserResult, array $queryComponents)
  77. {
  78. $this->_queryComponents = $queryComponents;
  79. $this->_walkers = new TreeWalkerChainIterator($this, $query, $parserResult);
  80. }
  81. /**
  82. * Adds a tree walker to the chain.
  83. *
  84. * @param string $walkerClass The class of the walker to instantiate.
  85. *
  86. * @return void
  87. */
  88. public function addTreeWalker($walkerClass)
  89. {
  90. $this->_walkers[] = $walkerClass;
  91. }
  92. /**
  93. * {@inheritdoc}
  94. *
  95. * @return void
  96. */
  97. public function walkSelectStatement(AST\SelectStatement $AST)
  98. {
  99. foreach ($this->_walkers as $walker) {
  100. $walker->walkSelectStatement($AST);
  101. $this->_queryComponents = $walker->getQueryComponents();
  102. }
  103. }
  104. /**
  105. * {@inheritdoc}
  106. *
  107. * @return void
  108. */
  109. public function walkSelectClause($selectClause)
  110. {
  111. foreach ($this->_walkers as $walker) {
  112. $walker->walkSelectClause($selectClause);
  113. }
  114. }
  115. /**
  116. * {@inheritdoc}
  117. *
  118. * @return void
  119. */
  120. public function walkFromClause($fromClause)
  121. {
  122. foreach ($this->_walkers as $walker) {
  123. $walker->walkFromClause($fromClause);
  124. }
  125. }
  126. /**
  127. * {@inheritdoc}
  128. *
  129. * @return void
  130. */
  131. public function walkFunction($function)
  132. {
  133. foreach ($this->_walkers as $walker) {
  134. $walker->walkFunction($function);
  135. }
  136. }
  137. /**
  138. * {@inheritdoc}
  139. *
  140. * @return void
  141. */
  142. public function walkOrderByClause($orderByClause)
  143. {
  144. foreach ($this->_walkers as $walker) {
  145. $walker->walkOrderByClause($orderByClause);
  146. }
  147. }
  148. /**
  149. * {@inheritdoc}
  150. *
  151. * @return void
  152. */
  153. public function walkOrderByItem($orderByItem)
  154. {
  155. foreach ($this->_walkers as $walker) {
  156. $walker->walkOrderByItem($orderByItem);
  157. }
  158. }
  159. /**
  160. * {@inheritdoc}
  161. *
  162. * @return void
  163. */
  164. public function walkHavingClause($havingClause)
  165. {
  166. foreach ($this->_walkers as $walker) {
  167. $walker->walkHavingClause($havingClause);
  168. }
  169. }
  170. /**
  171. * {@inheritdoc}
  172. *
  173. * @return void
  174. */
  175. public function walkJoin($join)
  176. {
  177. foreach ($this->_walkers as $walker) {
  178. $walker->walkJoin($join);
  179. }
  180. }
  181. /**
  182. * {@inheritdoc}
  183. *
  184. * @return void
  185. */
  186. public function walkSelectExpression($selectExpression)
  187. {
  188. foreach ($this->_walkers as $walker) {
  189. $walker->walkSelectExpression($selectExpression);
  190. }
  191. }
  192. /**
  193. * {@inheritdoc}
  194. *
  195. * @return void
  196. */
  197. public function walkQuantifiedExpression($qExpr)
  198. {
  199. foreach ($this->_walkers as $walker) {
  200. $walker->walkQuantifiedExpression($qExpr);
  201. }
  202. }
  203. /**
  204. * {@inheritdoc}
  205. *
  206. * @return void
  207. */
  208. public function walkSubselect($subselect)
  209. {
  210. foreach ($this->_walkers as $walker) {
  211. $walker->walkSubselect($subselect);
  212. }
  213. }
  214. /**
  215. * {@inheritdoc}
  216. *
  217. * @return void
  218. */
  219. public function walkSubselectFromClause($subselectFromClause)
  220. {
  221. foreach ($this->_walkers as $walker) {
  222. $walker->walkSubselectFromClause($subselectFromClause);
  223. }
  224. }
  225. /**
  226. * {@inheritdoc}
  227. *
  228. * @return void
  229. */
  230. public function walkSimpleSelectClause($simpleSelectClause)
  231. {
  232. foreach ($this->_walkers as $walker) {
  233. $walker->walkSimpleSelectClause($simpleSelectClause);
  234. }
  235. }
  236. /**
  237. * {@inheritdoc}
  238. *
  239. * @return void
  240. */
  241. public function walkSimpleSelectExpression($simpleSelectExpression)
  242. {
  243. foreach ($this->_walkers as $walker) {
  244. $walker->walkSimpleSelectExpression($simpleSelectExpression);
  245. }
  246. }
  247. /**
  248. * {@inheritdoc}
  249. *
  250. * @return void
  251. */
  252. public function walkAggregateExpression($aggExpression)
  253. {
  254. foreach ($this->_walkers as $walker) {
  255. $walker->walkAggregateExpression($aggExpression);
  256. }
  257. }
  258. /**
  259. * {@inheritdoc}
  260. *
  261. * @return void
  262. */
  263. public function walkGroupByClause($groupByClause)
  264. {
  265. foreach ($this->_walkers as $walker) {
  266. $walker->walkGroupByClause($groupByClause);
  267. }
  268. }
  269. /**
  270. * {@inheritdoc}
  271. *
  272. * @return void
  273. */
  274. public function walkGroupByItem($groupByItem)
  275. {
  276. foreach ($this->_walkers as $walker) {
  277. $walker->walkGroupByItem($groupByItem);
  278. }
  279. }
  280. /**
  281. * {@inheritdoc}
  282. *
  283. * @return void
  284. */
  285. public function walkUpdateStatement(AST\UpdateStatement $AST)
  286. {
  287. foreach ($this->_walkers as $walker) {
  288. $walker->walkUpdateStatement($AST);
  289. }
  290. }
  291. /**
  292. * {@inheritdoc}
  293. *
  294. * @return void
  295. */
  296. public function walkDeleteStatement(AST\DeleteStatement $AST)
  297. {
  298. foreach ($this->_walkers as $walker) {
  299. $walker->walkDeleteStatement($AST);
  300. }
  301. }
  302. /**
  303. * {@inheritdoc}
  304. *
  305. * @return void
  306. */
  307. public function walkDeleteClause(AST\DeleteClause $deleteClause)
  308. {
  309. foreach ($this->_walkers as $walker) {
  310. $walker->walkDeleteClause($deleteClause);
  311. }
  312. }
  313. /**
  314. * {@inheritdoc}
  315. *
  316. * @return void
  317. */
  318. public function walkUpdateClause($updateClause)
  319. {
  320. foreach ($this->_walkers as $walker) {
  321. $walker->walkUpdateClause($updateClause);
  322. }
  323. }
  324. /**
  325. * {@inheritdoc}
  326. *
  327. * @return void
  328. */
  329. public function walkUpdateItem($updateItem)
  330. {
  331. foreach ($this->_walkers as $walker) {
  332. $walker->walkUpdateItem($updateItem);
  333. }
  334. }
  335. /**
  336. * {@inheritdoc}
  337. *
  338. * @return void
  339. */
  340. public function walkWhereClause($whereClause)
  341. {
  342. foreach ($this->_walkers as $walker) {
  343. $walker->walkWhereClause($whereClause);
  344. }
  345. }
  346. /**
  347. * {@inheritdoc}
  348. *
  349. * @return void
  350. */
  351. public function walkConditionalExpression($condExpr)
  352. {
  353. foreach ($this->_walkers as $walker) {
  354. $walker->walkConditionalExpression($condExpr);
  355. }
  356. }
  357. /**
  358. * {@inheritdoc}
  359. *
  360. * @return void
  361. */
  362. public function walkConditionalTerm($condTerm)
  363. {
  364. foreach ($this->_walkers as $walker) {
  365. $walker->walkConditionalTerm($condTerm);
  366. }
  367. }
  368. /**
  369. * {@inheritdoc}
  370. *
  371. * @return void
  372. */
  373. public function walkConditionalFactor($factor)
  374. {
  375. foreach ($this->_walkers as $walker) {
  376. $walker->walkConditionalFactor($factor);
  377. }
  378. }
  379. /**
  380. * {@inheritdoc}
  381. *
  382. * @return void
  383. */
  384. public function walkConditionalPrimary($condPrimary)
  385. {
  386. foreach ($this->_walkers as $walker) {
  387. $walker->walkConditionalPrimary($condPrimary);
  388. }
  389. }
  390. /**
  391. * {@inheritdoc}
  392. *
  393. * @return void
  394. */
  395. public function walkExistsExpression($existsExpr)
  396. {
  397. foreach ($this->_walkers as $walker) {
  398. $walker->walkExistsExpression($existsExpr);
  399. }
  400. }
  401. /**
  402. * {@inheritdoc}
  403. *
  404. * @return void
  405. */
  406. public function walkCollectionMemberExpression($collMemberExpr)
  407. {
  408. foreach ($this->_walkers as $walker) {
  409. $walker->walkCollectionMemberExpression($collMemberExpr);
  410. }
  411. }
  412. /**
  413. * {@inheritdoc}
  414. *
  415. * @return void
  416. */
  417. public function walkEmptyCollectionComparisonExpression($emptyCollCompExpr)
  418. {
  419. foreach ($this->_walkers as $walker) {
  420. $walker->walkEmptyCollectionComparisonExpression($emptyCollCompExpr);
  421. }
  422. }
  423. /**
  424. * {@inheritdoc}
  425. *
  426. * @return void
  427. */
  428. public function walkNullComparisonExpression($nullCompExpr)
  429. {
  430. foreach ($this->_walkers as $walker) {
  431. $walker->walkNullComparisonExpression($nullCompExpr);
  432. }
  433. }
  434. /**
  435. * {@inheritdoc}
  436. *
  437. * @return void
  438. */
  439. public function walkInExpression($inExpr)
  440. {
  441. foreach ($this->_walkers as $walker) {
  442. $walker->walkInExpression($inExpr);
  443. }
  444. }
  445. /**
  446. * {@inheritdoc}
  447. *
  448. * @return void
  449. */
  450. public function walkInstanceOfExpression($instanceOfExpr)
  451. {
  452. foreach ($this->_walkers as $walker) {
  453. $walker->walkInstanceOfExpression($instanceOfExpr);
  454. }
  455. }
  456. /**
  457. * {@inheritdoc}
  458. *
  459. * @return void
  460. */
  461. public function walkLiteral($literal)
  462. {
  463. foreach ($this->_walkers as $walker) {
  464. $walker->walkLiteral($literal);
  465. }
  466. }
  467. /**
  468. * {@inheritdoc}
  469. *
  470. * @return void
  471. */
  472. public function walkBetweenExpression($betweenExpr)
  473. {
  474. foreach ($this->_walkers as $walker) {
  475. $walker->walkBetweenExpression($betweenExpr);
  476. }
  477. }
  478. /**
  479. * {@inheritdoc}
  480. *
  481. * @return void
  482. */
  483. public function walkLikeExpression($likeExpr)
  484. {
  485. foreach ($this->_walkers as $walker) {
  486. $walker->walkLikeExpression($likeExpr);
  487. }
  488. }
  489. /**
  490. * {@inheritdoc}
  491. *
  492. * @return void
  493. */
  494. public function walkStateFieldPathExpression($stateFieldPathExpression)
  495. {
  496. foreach ($this->_walkers as $walker) {
  497. $walker->walkStateFieldPathExpression($stateFieldPathExpression);
  498. }
  499. }
  500. /**
  501. * {@inheritdoc}
  502. *
  503. * @return void
  504. */
  505. public function walkComparisonExpression($compExpr)
  506. {
  507. foreach ($this->_walkers as $walker) {
  508. $walker->walkComparisonExpression($compExpr);
  509. }
  510. }
  511. /**
  512. * {@inheritdoc}
  513. *
  514. * @return void
  515. */
  516. public function walkInputParameter($inputParam)
  517. {
  518. foreach ($this->_walkers as $walker) {
  519. $walker->walkInputParameter($inputParam);
  520. }
  521. }
  522. /**
  523. * {@inheritdoc}
  524. *
  525. * @return void
  526. */
  527. public function walkArithmeticExpression($arithmeticExpr)
  528. {
  529. foreach ($this->_walkers as $walker) {
  530. $walker->walkArithmeticExpression($arithmeticExpr);
  531. }
  532. }
  533. /**
  534. * {@inheritdoc}
  535. *
  536. * @return void
  537. */
  538. public function walkArithmeticTerm($term)
  539. {
  540. foreach ($this->_walkers as $walker) {
  541. $walker->walkArithmeticTerm($term);
  542. }
  543. }
  544. /**
  545. * {@inheritdoc}
  546. *
  547. * @return void
  548. */
  549. public function walkStringPrimary($stringPrimary)
  550. {
  551. foreach ($this->_walkers as $walker) {
  552. $walker->walkStringPrimary($stringPrimary);
  553. }
  554. }
  555. /**
  556. * {@inheritdoc}
  557. *
  558. * @return void
  559. */
  560. public function walkArithmeticFactor($factor)
  561. {
  562. foreach ($this->_walkers as $walker) {
  563. $walker->walkArithmeticFactor($factor);
  564. }
  565. }
  566. /**
  567. * {@inheritdoc}
  568. *
  569. * @return void
  570. */
  571. public function walkSimpleArithmeticExpression($simpleArithmeticExpr)
  572. {
  573. foreach ($this->_walkers as $walker) {
  574. $walker->walkSimpleArithmeticExpression($simpleArithmeticExpr);
  575. }
  576. }
  577. /**
  578. * {@inheritdoc}
  579. *
  580. * @return void
  581. */
  582. public function walkPathExpression($pathExpr)
  583. {
  584. foreach ($this->_walkers as $walker) {
  585. $walker->walkPathExpression($pathExpr);
  586. }
  587. }
  588. /**
  589. * {@inheritdoc}
  590. *
  591. * @return void
  592. */
  593. public function walkResultVariable($resultVariable)
  594. {
  595. foreach ($this->_walkers as $walker) {
  596. $walker->walkResultVariable($resultVariable);
  597. }
  598. }
  599. /**
  600. * {@inheritdoc}
  601. *
  602. * @return void
  603. */
  604. public function getExecutor($AST)
  605. {
  606. }
  607. }