|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
namespace TYPO3\CMS\Core\Database\Query\Restriction; |
|
5
|
|
|
|
|
6
|
|
|
/* |
|
7
|
|
|
* This file is part of the TYPO3 CMS project. |
|
8
|
|
|
* |
|
9
|
|
|
* It is free software; you can redistribute it and/or modify it under |
|
10
|
|
|
* the terms of the GNU General Public License, either version 2 |
|
11
|
|
|
* of the License, or any later version. |
|
12
|
|
|
* |
|
13
|
|
|
* For the full copyright and license information, please read the |
|
14
|
|
|
* LICENSE.txt file that was distributed with this source code. |
|
15
|
|
|
* |
|
16
|
|
|
* The TYPO3 project - inspiring people to share! |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
use TYPO3\CMS\Core\Database\Query\Expression\CompositeExpression; |
|
20
|
|
|
use TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Restriction container that applies added restrictions only to the given table aliases. |
|
24
|
|
|
* Enforced restrictions are treated equally to all other restrictions. |
|
25
|
|
|
*/ |
|
26
|
|
|
class LimitToTablesRestrictionContainer implements QueryRestrictionContainerInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var QueryRestrictionInterface[] |
|
30
|
|
|
*/ |
|
31
|
|
|
private $restrictions = []; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var QueryRestrictionContainerInterface[] |
|
35
|
|
|
*/ |
|
36
|
|
|
private $restrictionContainer = []; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var array |
|
40
|
|
|
*/ |
|
41
|
|
|
private $applicableTableAliases; |
|
42
|
|
|
|
|
43
|
|
|
public function removeAll(): QueryRestrictionContainerInterface |
|
44
|
|
|
{ |
|
45
|
|
|
$this->applicableTableAliases = $this->restrictions = $this->restrictionContainer = []; |
|
46
|
|
|
return $this; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function removeByType(string $restrictionType): QueryRestrictionContainerInterface |
|
50
|
|
|
{ |
|
51
|
|
|
unset($this->applicableTableAliases[$restrictionType], $this->restrictions[$restrictionType]); |
|
52
|
|
|
foreach ($this->restrictionContainer as $restrictionContainer) { |
|
53
|
|
|
$restrictionContainer->removeByType($restrictionType); |
|
54
|
|
|
} |
|
55
|
|
|
return $this; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function add(QueryRestrictionInterface $restriction): QueryRestrictionContainerInterface |
|
59
|
|
|
{ |
|
60
|
|
|
$this->restrictions[get_class($restriction)] = $restriction; |
|
61
|
|
|
if ($restriction instanceof QueryRestrictionContainerInterface) { |
|
62
|
|
|
$this->restrictionContainer[get_class($restriction)] = $restriction; |
|
63
|
|
|
} |
|
64
|
|
|
return $this; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Adds the restriction, but also remembers which table aliases it should be applied to |
|
69
|
|
|
* |
|
70
|
|
|
* @param QueryRestrictionInterface $restriction |
|
71
|
|
|
* @param array $tableAliases flat array of table aliases, not table names |
|
72
|
|
|
* @return QueryRestrictionContainerInterface |
|
73
|
|
|
*/ |
|
74
|
|
|
public function addForTables(QueryRestrictionInterface $restriction, array $tableAliases): QueryRestrictionContainerInterface |
|
75
|
|
|
{ |
|
76
|
|
|
$this->applicableTableAliases[get_class($restriction)] = $tableAliases; |
|
77
|
|
|
return $this->add($restriction); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Main method to build expressions for given tables, but respecting configured filters. |
|
82
|
|
|
* |
|
83
|
|
|
* @param array $queriedTables Array of tables, where array key is table alias and value is a table name |
|
84
|
|
|
* @param ExpressionBuilder $expressionBuilder Expression builder instance to add restrictions with |
|
85
|
|
|
* @return CompositeExpression The result of query builder expression(s) |
|
86
|
|
|
*/ |
|
87
|
|
|
public function buildExpression(array $queriedTables, ExpressionBuilder $expressionBuilder): CompositeExpression |
|
88
|
|
|
{ |
|
89
|
|
|
$constraints = []; |
|
90
|
|
|
foreach ($this->restrictions as $name => $restriction) { |
|
91
|
|
|
$constraints[] = $restriction->buildExpression( |
|
92
|
|
|
$this->filterApplicableTableAliases($queriedTables, $name), |
|
93
|
|
|
$expressionBuilder |
|
94
|
|
|
); |
|
95
|
|
|
} |
|
96
|
|
|
return $expressionBuilder->andX(...$constraints); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
private function filterApplicableTableAliases(array $queriedTables, string $name): array |
|
100
|
|
|
{ |
|
101
|
|
|
if (!isset($this->applicableTableAliases[$name])) { |
|
102
|
|
|
return $queriedTables; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
$filteredTables = []; |
|
106
|
|
|
foreach ($this->applicableTableAliases[$name] as $tableAlias) { |
|
107
|
|
|
if (!isset($queriedTables[$tableAlias])) { |
|
108
|
|
|
throw new \LogicException(sprintf('Applicable table alias "%s" is not in queried tables', $tableAlias), 1558354033); |
|
109
|
|
|
} |
|
110
|
|
|
$filteredTables[$tableAlias] = $queriedTables[$tableAlias]; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return $filteredTables; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|