Skip to content

Commit 1f3cb75

Browse files
mbabkerfranmomu
authored andcommitted
Add an internal trait to allow cross-version compatibility for ORM SQL walkers
1 parent 4392953 commit 1f3cb75

File tree

6 files changed

+479
-82
lines changed

6 files changed

+479
-82
lines changed

phpstan.neon.dist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,5 @@ parameters:
2222
excludePaths:
2323
# Generates non-ignorable errors like " Parameter #1 $method (string) of method Gedmo\Tree\Entity\Repository\NestedTreeRepository::__call() is not contravariant with parameter #1 $method (mixed) of method Doctrine\ORM\EntityRepository::__call()."
2424
- src/Tool/ORM/Repository/EntityRepositoryCompat.php
25+
# Compat file for ORM 3, causes analysis errors with ORM 2
26+
- src/Tool/ORM/Walker/orm-3.php

src/SoftDeleteable/Query/TreeWalker/SoftDeleteableWalker.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@
1515
use Doctrine\ORM\Mapping\QuoteStrategy;
1616
use Doctrine\ORM\Query\AST\DeleteClause;
1717
use Doctrine\ORM\Query\AST\DeleteStatement;
18+
use Doctrine\ORM\Query\AST\SelectStatement;
19+
use Doctrine\ORM\Query\AST\UpdateStatement;
1820
use Doctrine\ORM\Query\Exec\AbstractSqlExecutor;
1921
use Doctrine\ORM\Query\Exec\SingleTableDeleteUpdateExecutor;
2022
use Doctrine\ORM\Query\SqlWalker;
2123
use Gedmo\Exception\RuntimeException;
2224
use Gedmo\Exception\UnexpectedValueException;
2325
use Gedmo\SoftDeleteable\Query\TreeWalker\Exec\MultiTableDeleteExecutor;
2426
use Gedmo\SoftDeleteable\SoftDeleteableListener;
27+
use Gedmo\Tool\ORM\Walker\SqlWalkerCompat;
2528

2629
/**
2730
* This SqlWalker is needed when you need to use a DELETE DQL query.
@@ -35,6 +38,8 @@
3538
*/
3639
class SoftDeleteableWalker extends SqlWalker
3740
{
41+
use SqlWalkerCompat;
42+
3843
/**
3944
* @var Connection
4045
*
@@ -94,30 +99,30 @@ public function __construct($query, $parserResult, array $queryComponents)
9499
}
95100

96101
/**
97-
* @return AbstractSqlExecutor
102+
* @param SelectStatement|UpdateStatement|DeleteStatement $statement
103+
*
104+
* @throws UnexpectedValueException when an unsupported AST statement is given
98105
*/
99-
public function getExecutor($AST)
106+
protected function doGetExecutorWithCompat($statement): AbstractSqlExecutor
100107
{
101108
switch (true) {
102-
case $AST instanceof DeleteStatement:
103-
assert(class_exists($AST->deleteClause->abstractSchemaName));
109+
case $statement instanceof DeleteStatement:
110+
assert(class_exists($statement->deleteClause->abstractSchemaName));
104111

105-
$primaryClass = $this->getEntityManager()->getClassMetadata($AST->deleteClause->abstractSchemaName);
112+
$primaryClass = $this->getEntityManager()->getClassMetadata($statement->deleteClause->abstractSchemaName);
106113

107114
return $primaryClass->isInheritanceTypeJoined()
108-
? new MultiTableDeleteExecutor($AST, $this, $this->meta, $this->getConnection()->getDatabasePlatform(), $this->configuration)
109-
: new SingleTableDeleteUpdateExecutor($AST, $this);
115+
? new MultiTableDeleteExecutor($statement, $this, $this->meta, $this->getConnection()->getDatabasePlatform(), $this->configuration)
116+
: new SingleTableDeleteUpdateExecutor($statement, $this);
110117
default:
111118
throw new UnexpectedValueException('SoftDeleteable walker should be used only on delete statement');
112119
}
113120
}
114121

115122
/**
116-
* Change a DELETE clause for an UPDATE clause
117-
*
118-
* @return string the SQL
123+
* Changes a DELETE clause into an UPDATE clause for a soft-deleteable entity.
119124
*/
120-
public function walkDeleteClause(DeleteClause $deleteClause)
125+
protected function doWalkDeleteClauseWithCompat(DeleteClause $deleteClause): string
121126
{
122127
$em = $this->getEntityManager();
123128

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Doctrine Behavioral Extensions package.
5+
* (c) Gediminas Morkevicius <[email protected]> http://www.gediminasm.org
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace Gedmo\Tool\ORM\Walker;
11+
12+
use Doctrine\ORM\Query\SqlWalker;
13+
14+
if ((new \ReflectionClass(SqlWalker::class))->getMethod('getExecutor')->hasReturnType()) {
15+
// ORM 3.x
16+
require_once __DIR__.'/orm-3.php';
17+
} else {
18+
// ORM 2.x
19+
require_once __DIR__.'/orm-2.php';
20+
}

src/Tool/ORM/Walker/orm-2.php

Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Doctrine Behavioral Extensions package.
5+
* (c) Gediminas Morkevicius <[email protected]> http://www.gediminasm.org
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace Gedmo\Tool\ORM\Walker;
11+
12+
use Doctrine\ORM\Query\AST;
13+
use Doctrine\ORM\Query\AST\DeleteClause;
14+
use Doctrine\ORM\Query\AST\FromClause;
15+
use Doctrine\ORM\Query\AST\GroupByClause;
16+
use Doctrine\ORM\Query\AST\HavingClause;
17+
use Doctrine\ORM\Query\AST\OrderByClause;
18+
use Doctrine\ORM\Query\AST\SelectClause;
19+
use Doctrine\ORM\Query\AST\SelectStatement;
20+
use Doctrine\ORM\Query\AST\SimpleSelectClause;
21+
use Doctrine\ORM\Query\AST\SubselectFromClause;
22+
use Doctrine\ORM\Query\AST\WhereClause;
23+
use Doctrine\ORM\Query\Exec\AbstractSqlExecutor;
24+
use Doctrine\ORM\Query\SqlWalker;
25+
26+
/**
27+
* Helper trait to address compatibility issues between ORM 2.x and 3.x.
28+
*
29+
* @mixin SqlWalker
30+
*
31+
* @internal
32+
*/
33+
trait SqlWalkerCompat
34+
{
35+
/**
36+
* Gets an executor that can be used to execute the result of this walker.
37+
*
38+
* @param SelectStatement|AST\UpdateStatement|AST\DeleteStatement $statement
39+
*
40+
* @return AbstractSqlExecutor
41+
*/
42+
public function getExecutor($statement)
43+
{
44+
return $this->doGetExecutorWithCompat($statement);
45+
}
46+
47+
/**
48+
* Walks down a SelectStatement AST node, thereby generating the appropriate SQL.
49+
*
50+
* @param SelectStatement $selectStatement
51+
*
52+
* @return string
53+
*/
54+
public function walkSelectStatement($selectStatement)
55+
{
56+
return $this->doWalkSelectStatementWithCompat($selectStatement);
57+
}
58+
59+
/**
60+
* Walks down a SelectClause AST node, thereby generating the appropriate SQL.
61+
*
62+
* @param SelectClause $selectClause
63+
*
64+
* @return string
65+
*/
66+
public function walkSelectClause($selectClause)
67+
{
68+
return $this->doWalkSelectClauseWithCompat($selectClause);
69+
}
70+
71+
/**
72+
* Walks down a FromClause AST node, thereby generating the appropriate SQL.
73+
*
74+
* @param FromClause $fromClause
75+
*
76+
* @return string
77+
*/
78+
public function walkFromClause($fromClause)
79+
{
80+
return $this->doWalkFromClauseWithCompat($fromClause);
81+
}
82+
83+
/**
84+
* Walks down a OrderByClause AST node, thereby generating the appropriate SQL.
85+
*
86+
* @param OrderByClause $orderByClause
87+
*
88+
* @return string
89+
*/
90+
public function walkOrderByClause($orderByClause)
91+
{
92+
return $this->doWalkOrderByClauseWithCompat($orderByClause);
93+
}
94+
95+
/**
96+
* Walks down a HavingClause AST node, thereby generating the appropriate SQL.
97+
*
98+
* @param HavingClause $havingClause
99+
*
100+
* @return string
101+
*/
102+
public function walkHavingClause($havingClause)
103+
{
104+
return $this->doWalkHavingClauseWithCompat($havingClause);
105+
}
106+
107+
/**
108+
* Walks down a SubselectFromClause AST node, thereby generating the appropriate SQL.
109+
*
110+
* @param SubselectFromClause $subselectFromClause
111+
*
112+
* @return string
113+
*/
114+
public function walkSubselectFromClause($subselectFromClause)
115+
{
116+
return $this->doWalkSubselectFromClauseWithCompat($subselectFromClause);
117+
}
118+
119+
/**
120+
* Walks down a SimpleSelectClause AST node, thereby generating the appropriate SQL.
121+
*
122+
* @param SimpleSelectClause $simpleSelectClause
123+
*
124+
* @return string
125+
*/
126+
public function walkSimpleSelectClause($simpleSelectClause)
127+
{
128+
return $this->doWalkSimpleSelectClauseWithCompat($simpleSelectClause);
129+
}
130+
131+
/**
132+
* Walks down a GroupByClause AST node, thereby generating the appropriate SQL.
133+
*
134+
* @param GroupByClause $groupByClause
135+
*
136+
* @return string
137+
*/
138+
public function walkGroupByClause($groupByClause)
139+
{
140+
return $this->doWalkGroupByClauseWithCompat($groupByClause);
141+
}
142+
143+
/**
144+
* Walks down a DeleteClause AST node, thereby generating the appropriate SQL.
145+
*
146+
* @param DeleteClause $deleteClause
147+
*
148+
* @return string
149+
*/
150+
public function walkDeleteClause($deleteClause)
151+
{
152+
return $this->doWalkDeleteClauseWithCompat($deleteClause);
153+
}
154+
155+
/**
156+
* Walks down a WhereClause AST node, thereby generating the appropriate SQL.
157+
*
158+
* WhereClause or not, the appropriate discriminator sql is added.
159+
*
160+
* @param WhereClause|null $whereClause
161+
*
162+
* @return string
163+
*/
164+
public function walkWhereClause($whereClause)
165+
{
166+
return $this->doWalkWhereClauseWithCompat($whereClause);
167+
}
168+
169+
/**
170+
* Gets an executor that can be used to execute the result of this walker.
171+
*
172+
* @param SelectStatement|AST\UpdateStatement|AST\DeleteStatement $statement
173+
*/
174+
protected function doGetExecutorWithCompat($statement): AbstractSqlExecutor
175+
{
176+
return parent::getExecutor($statement);
177+
}
178+
179+
protected function doWalkSelectStatementWithCompat(SelectStatement $selectStatement): string
180+
{
181+
return parent::walkSelectStatement($selectStatement);
182+
}
183+
184+
protected function doWalkSelectClauseWithCompat(SelectClause $selectClause): string
185+
{
186+
return parent::walkSelectClause($selectClause);
187+
}
188+
189+
protected function doWalkFromClauseWithCompat(FromClause $fromClause): string
190+
{
191+
return parent::walkFromClause($fromClause);
192+
}
193+
194+
protected function doWalkOrderByClauseWithCompat(OrderByClause $orderByClause): string
195+
{
196+
return parent::walkOrderByClause($orderByClause);
197+
}
198+
199+
protected function doWalkHavingClauseWithCompat(HavingClause $havingClause): string
200+
{
201+
return parent::walkHavingClause($havingClause);
202+
}
203+
204+
protected function doWalkSubselectFromClauseWithCompat(SubselectFromClause $subselectFromClause): string
205+
{
206+
return parent::walkSubselectFromClause($subselectFromClause);
207+
}
208+
209+
protected function doWalkSimpleSelectClauseWithCompat(SimpleSelectClause $simpleSelectClause): string
210+
{
211+
return parent::walkSimpleSelectClause($simpleSelectClause);
212+
}
213+
214+
protected function doWalkGroupByClauseWithCompat(GroupByClause $groupByClause): string
215+
{
216+
return parent::walkGroupByClause($groupByClause);
217+
}
218+
219+
protected function doWalkDeleteClauseWithCompat(DeleteClause $deleteClause): string
220+
{
221+
return parent::walkDeleteClause($deleteClause);
222+
}
223+
224+
protected function doWalkWhereClauseWithCompat(?WhereClause $whereClause): string
225+
{
226+
return parent::walkWhereClause($whereClause);
227+
}
228+
}

0 commit comments

Comments
 (0)