/lib/Doctrine/ORM/Query/TreeWalkerChain.php

https://github.com/jaikdean/doctrine2 · PHP · 542 lines · 311 code · 59 blank · 172 comment · 1 complexity · 43b38710d6b2df22d76d2e19397fe229 MD5 · raw file

  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\ORM\Query;
  4. use function array_diff;
  5. use function array_keys;
  6. /**
  7. * Represents a chain of tree walkers that modify an AST and finally emit output.
  8. * Only the last walker in the chain can emit output. Any previous walkers can modify
  9. * the AST to influence the final output produced by the last walker.
  10. */
  11. class TreeWalkerChain implements TreeWalker
  12. {
  13. /**
  14. * The tree walkers.
  15. *
  16. * @var TreeWalker[]
  17. */
  18. private $walkers;
  19. /**
  20. * The query components of the original query (the "symbol table") that was produced by the Parser.
  21. *
  22. * @var mixed[][]
  23. */
  24. private $queryComponents;
  25. /**
  26. * Returns the internal queryComponents array.
  27. *
  28. * @return mixed[][]
  29. */
  30. public function getQueryComponents()
  31. {
  32. return $this->queryComponents;
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function setQueryComponent($dqlAlias, array $queryComponent)
  38. {
  39. $requiredKeys = ['metadata', 'parent', 'relation', 'map', 'nestingLevel', 'token'];
  40. if (array_diff($requiredKeys, array_keys($queryComponent))) {
  41. throw QueryException::invalidQueryComponent($dqlAlias);
  42. }
  43. $this->queryComponents[$dqlAlias] = $queryComponent;
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function __construct($query, $parserResult, array $queryComponents)
  49. {
  50. $this->queryComponents = $queryComponents;
  51. $this->walkers = new TreeWalkerChainIterator($this, $query, $parserResult);
  52. }
  53. /**
  54. * Adds a tree walker to the chain.
  55. *
  56. * @param string $walkerClass The class of the walker to instantiate.
  57. */
  58. public function addTreeWalker($walkerClass)
  59. {
  60. $this->walkers[] = $walkerClass;
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function walkSelectStatement(AST\SelectStatement $AST)
  66. {
  67. foreach ($this->walkers as $walker) {
  68. $walker->walkSelectStatement($AST);
  69. $this->queryComponents = $walker->getQueryComponents();
  70. }
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function walkSelectClause($selectClause)
  76. {
  77. foreach ($this->walkers as $walker) {
  78. $walker->walkSelectClause($selectClause);
  79. }
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. public function walkFromClause($fromClause)
  85. {
  86. foreach ($this->walkers as $walker) {
  87. $walker->walkFromClause($fromClause);
  88. }
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function walkFunction($function)
  94. {
  95. foreach ($this->walkers as $walker) {
  96. $walker->walkFunction($function);
  97. }
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. public function walkOrderByClause($orderByClause)
  103. {
  104. foreach ($this->walkers as $walker) {
  105. $walker->walkOrderByClause($orderByClause);
  106. }
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. public function walkOrderByItem($orderByItem)
  112. {
  113. foreach ($this->walkers as $walker) {
  114. $walker->walkOrderByItem($orderByItem);
  115. }
  116. }
  117. /**
  118. * {@inheritdoc}
  119. */
  120. public function walkHavingClause($havingClause)
  121. {
  122. foreach ($this->walkers as $walker) {
  123. $walker->walkHavingClause($havingClause);
  124. }
  125. }
  126. /**
  127. * {@inheritdoc}
  128. */
  129. public function walkJoin($join)
  130. {
  131. foreach ($this->walkers as $walker) {
  132. $walker->walkJoin($join);
  133. }
  134. }
  135. /**
  136. * {@inheritdoc}
  137. */
  138. public function walkSelectExpression($selectExpression)
  139. {
  140. foreach ($this->walkers as $walker) {
  141. $walker->walkSelectExpression($selectExpression);
  142. }
  143. }
  144. /**
  145. * {@inheritdoc}
  146. */
  147. public function walkQuantifiedExpression($qExpr)
  148. {
  149. foreach ($this->walkers as $walker) {
  150. $walker->walkQuantifiedExpression($qExpr);
  151. }
  152. }
  153. /**
  154. * {@inheritdoc}
  155. */
  156. public function walkSubselect($subselect)
  157. {
  158. foreach ($this->walkers as $walker) {
  159. $walker->walkSubselect($subselect);
  160. }
  161. }
  162. /**
  163. * {@inheritdoc}
  164. */
  165. public function walkSubselectFromClause($subselectFromClause)
  166. {
  167. foreach ($this->walkers as $walker) {
  168. $walker->walkSubselectFromClause($subselectFromClause);
  169. }
  170. }
  171. /**
  172. * {@inheritdoc}
  173. */
  174. public function walkSimpleSelectClause($simpleSelectClause)
  175. {
  176. foreach ($this->walkers as $walker) {
  177. $walker->walkSimpleSelectClause($simpleSelectClause);
  178. }
  179. }
  180. /**
  181. * {@inheritdoc}
  182. */
  183. public function walkSimpleSelectExpression($simpleSelectExpression)
  184. {
  185. foreach ($this->walkers as $walker) {
  186. $walker->walkSimpleSelectExpression($simpleSelectExpression);
  187. }
  188. }
  189. /**
  190. * {@inheritdoc}
  191. */
  192. public function walkAggregateExpression($aggExpression)
  193. {
  194. foreach ($this->walkers as $walker) {
  195. $walker->walkAggregateExpression($aggExpression);
  196. }
  197. }
  198. /**
  199. * {@inheritdoc}
  200. */
  201. public function walkGroupByClause($groupByClause)
  202. {
  203. foreach ($this->walkers as $walker) {
  204. $walker->walkGroupByClause($groupByClause);
  205. }
  206. }
  207. /**
  208. * {@inheritdoc}
  209. */
  210. public function walkGroupByItem($groupByItem)
  211. {
  212. foreach ($this->walkers as $walker) {
  213. $walker->walkGroupByItem($groupByItem);
  214. }
  215. }
  216. /**
  217. * {@inheritdoc}
  218. */
  219. public function walkUpdateStatement(AST\UpdateStatement $AST)
  220. {
  221. foreach ($this->walkers as $walker) {
  222. $walker->walkUpdateStatement($AST);
  223. }
  224. }
  225. /**
  226. * {@inheritdoc}
  227. */
  228. public function walkDeleteStatement(AST\DeleteStatement $AST)
  229. {
  230. foreach ($this->walkers as $walker) {
  231. $walker->walkDeleteStatement($AST);
  232. }
  233. }
  234. /**
  235. * {@inheritdoc}
  236. */
  237. public function walkDeleteClause(AST\DeleteClause $deleteClause)
  238. {
  239. foreach ($this->walkers as $walker) {
  240. $walker->walkDeleteClause($deleteClause);
  241. }
  242. }
  243. /**
  244. * {@inheritdoc}
  245. */
  246. public function walkUpdateClause($updateClause)
  247. {
  248. foreach ($this->walkers as $walker) {
  249. $walker->walkUpdateClause($updateClause);
  250. }
  251. }
  252. /**
  253. * {@inheritdoc}
  254. */
  255. public function walkUpdateItem($updateItem)
  256. {
  257. foreach ($this->walkers as $walker) {
  258. $walker->walkUpdateItem($updateItem);
  259. }
  260. }
  261. /**
  262. * {@inheritdoc}
  263. */
  264. public function walkWhereClause($whereClause)
  265. {
  266. foreach ($this->walkers as $walker) {
  267. $walker->walkWhereClause($whereClause);
  268. }
  269. }
  270. /**
  271. * {@inheritdoc}
  272. */
  273. public function walkConditionalExpression($condExpr)
  274. {
  275. foreach ($this->walkers as $walker) {
  276. $walker->walkConditionalExpression($condExpr);
  277. }
  278. }
  279. /**
  280. * {@inheritdoc}
  281. */
  282. public function walkConditionalTerm($condTerm)
  283. {
  284. foreach ($this->walkers as $walker) {
  285. $walker->walkConditionalTerm($condTerm);
  286. }
  287. }
  288. /**
  289. * {@inheritdoc}
  290. */
  291. public function walkConditionalFactor($factor)
  292. {
  293. foreach ($this->walkers as $walker) {
  294. $walker->walkConditionalFactor($factor);
  295. }
  296. }
  297. /**
  298. * {@inheritdoc}
  299. */
  300. public function walkConditionalPrimary($condPrimary)
  301. {
  302. foreach ($this->walkers as $walker) {
  303. $walker->walkConditionalPrimary($condPrimary);
  304. }
  305. }
  306. /**
  307. * {@inheritdoc}
  308. */
  309. public function walkExistsExpression($existsExpr)
  310. {
  311. foreach ($this->walkers as $walker) {
  312. $walker->walkExistsExpression($existsExpr);
  313. }
  314. }
  315. /**
  316. * {@inheritdoc}
  317. */
  318. public function walkCollectionMemberExpression($collMemberExpr)
  319. {
  320. foreach ($this->walkers as $walker) {
  321. $walker->walkCollectionMemberExpression($collMemberExpr);
  322. }
  323. }
  324. /**
  325. * {@inheritdoc}
  326. */
  327. public function walkEmptyCollectionComparisonExpression($emptyCollCompExpr)
  328. {
  329. foreach ($this->walkers as $walker) {
  330. $walker->walkEmptyCollectionComparisonExpression($emptyCollCompExpr);
  331. }
  332. }
  333. /**
  334. * {@inheritdoc}
  335. */
  336. public function walkNullComparisonExpression($nullCompExpr)
  337. {
  338. foreach ($this->walkers as $walker) {
  339. $walker->walkNullComparisonExpression($nullCompExpr);
  340. }
  341. }
  342. /**
  343. * {@inheritdoc}
  344. */
  345. public function walkInExpression($inExpr)
  346. {
  347. foreach ($this->walkers as $walker) {
  348. $walker->walkInExpression($inExpr);
  349. }
  350. }
  351. /**
  352. * {@inheritdoc}
  353. */
  354. public function walkInstanceOfExpression($instanceOfExpr)
  355. {
  356. foreach ($this->walkers as $walker) {
  357. $walker->walkInstanceOfExpression($instanceOfExpr);
  358. }
  359. }
  360. /**
  361. * {@inheritdoc}
  362. */
  363. public function walkLiteral($literal)
  364. {
  365. foreach ($this->walkers as $walker) {
  366. $walker->walkLiteral($literal);
  367. }
  368. }
  369. /**
  370. * {@inheritdoc}
  371. */
  372. public function walkBetweenExpression($betweenExpr)
  373. {
  374. foreach ($this->walkers as $walker) {
  375. $walker->walkBetweenExpression($betweenExpr);
  376. }
  377. }
  378. /**
  379. * {@inheritdoc}
  380. */
  381. public function walkLikeExpression($likeExpr)
  382. {
  383. foreach ($this->walkers as $walker) {
  384. $walker->walkLikeExpression($likeExpr);
  385. }
  386. }
  387. /**
  388. * {@inheritdoc}
  389. */
  390. public function walkStateFieldPathExpression($stateFieldPathExpression)
  391. {
  392. foreach ($this->walkers as $walker) {
  393. $walker->walkStateFieldPathExpression($stateFieldPathExpression);
  394. }
  395. }
  396. /**
  397. * {@inheritdoc}
  398. */
  399. public function walkComparisonExpression($compExpr)
  400. {
  401. foreach ($this->walkers as $walker) {
  402. $walker->walkComparisonExpression($compExpr);
  403. }
  404. }
  405. /**
  406. * {@inheritdoc}
  407. */
  408. public function walkInputParameter($inputParam)
  409. {
  410. foreach ($this->walkers as $walker) {
  411. $walker->walkInputParameter($inputParam);
  412. }
  413. }
  414. /**
  415. * {@inheritdoc}
  416. */
  417. public function walkArithmeticExpression($arithmeticExpr)
  418. {
  419. foreach ($this->walkers as $walker) {
  420. $walker->walkArithmeticExpression($arithmeticExpr);
  421. }
  422. }
  423. /**
  424. * {@inheritdoc}
  425. */
  426. public function walkArithmeticTerm($term)
  427. {
  428. foreach ($this->walkers as $walker) {
  429. $walker->walkArithmeticTerm($term);
  430. }
  431. }
  432. /**
  433. * {@inheritdoc}
  434. */
  435. public function walkStringPrimary($stringPrimary)
  436. {
  437. foreach ($this->walkers as $walker) {
  438. $walker->walkStringPrimary($stringPrimary);
  439. }
  440. }
  441. /**
  442. * {@inheritdoc}
  443. */
  444. public function walkArithmeticFactor($factor)
  445. {
  446. foreach ($this->walkers as $walker) {
  447. $walker->walkArithmeticFactor($factor);
  448. }
  449. }
  450. /**
  451. * {@inheritdoc}
  452. */
  453. public function walkSimpleArithmeticExpression($simpleArithmeticExpr)
  454. {
  455. foreach ($this->walkers as $walker) {
  456. $walker->walkSimpleArithmeticExpression($simpleArithmeticExpr);
  457. }
  458. }
  459. /**
  460. * {@inheritdoc}
  461. */
  462. public function walkPathExpression($pathExpr)
  463. {
  464. foreach ($this->walkers as $walker) {
  465. $walker->walkPathExpression($pathExpr);
  466. }
  467. }
  468. /**
  469. * {@inheritdoc}
  470. */
  471. public function walkResultVariable($resultVariable)
  472. {
  473. foreach ($this->walkers as $walker) {
  474. $walker->walkResultVariable($resultVariable);
  475. }
  476. }
  477. /**
  478. * {@inheritdoc}
  479. */
  480. public function getExecutor($AST)
  481. {
  482. }
  483. }