-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathDcCompat.php
More file actions
246 lines (208 loc) · 8.29 KB
/
Copy pathDcCompat.php
File metadata and controls
246 lines (208 loc) · 8.29 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<?php
/**
* This file is part of contao-community-alliance/dc-general.
*
* (c) 2013-2025 Contao Community Alliance.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* This project is provided in good faith and hope to be usable by anyone.
*
* @package contao-community-alliance/dc-general
* @author Christian Schiffler <c.schiffler@cyberspectrum.de>
* @author Tristan Lins <tristan.lins@bit3.de>
* @author Sven Baumann <baumann.sv@gmail.com>
* @author Ingolf Steinhardt <info@e-spin.de>
* @copyright 2013-2025 Contao Community Alliance.
* @license https://github.com/contao-community-alliance/dc-general/blob/master/LICENSE LGPL-3.0-or-later
* @filesource
*/
namespace ContaoCommunityAlliance\DcGeneral\Contao\Compatibility;
use ContaoCommunityAlliance\DcGeneral\Data\DataProviderInterface;
use ContaoCommunityAlliance\DcGeneral\Data\ModelId;
use ContaoCommunityAlliance\DcGeneral\Data\ModelInterface;
use ContaoCommunityAlliance\DcGeneral\DataDefinition\ContainerInterface;
use ContaoCommunityAlliance\DcGeneral\DC\General;
use ContaoCommunityAlliance\DcGeneral\EnvironmentInterface;
use ContaoCommunityAlliance\DcGeneral\Exception\DcGeneralException;
use ContaoCommunityAlliance\DcGeneral\Exception\DcGeneralRuntimeException;
use ContaoCommunityAlliance\DcGeneral\Factory\Event\PopulateEnvironmentEvent;
use ContaoCommunityAlliance\DcGeneral\InputProviderInterface;
/**
* Small compatibility layer for callbacks, that expect a "full-featured" DC instance.
*
* @psalm-suppress PropertyNotSetInConstructor
*/
class DcCompat extends General
{
/**
* The current model.
*
* @var ModelInterface|null
*/
protected $model;
/**
* Name of the property currently working on.
*
* @var string|null
*/
protected $propertyName;
/**
* Create a new instance.
*
* @param EnvironmentInterface $environment The Dc instance to use for delegating.
* @param ModelInterface|null $model The model within scope (optional).
* @param string|null $propertyName The name of the property within scope (optional).
*/
public function __construct(EnvironmentInterface $environment, ?ModelInterface $model = null, ?string $propertyName = null)
{
// Prevent "Recoverable error: Argument X passed to SomClass::someMethod() must be an instance of DataContainer,
// instance of ContaoCommunityAlliance\DcGeneral\Contao\Compatibility\DcCompat given" in callbacks.
if (!\class_exists('\DataContainer', false)) {
\class_alias('\Contao\DataContainer', '\DataContainer');
}
$this->objEnvironment = $environment;
$this->model = $model;
$this->propertyName = $propertyName;
}
/**
* Retrieve the current model.
*
* @return ModelInterface|null
*/
public function getModel()
{
return $this->model;
}
/**
* Retrieve the current property.
*
* @return string|null
*/
public function getPropertyName()
{
return $this->propertyName;
}
/**
* Internal use only.
*
* @throws DcGeneralException This method is for internal use only.
*
* @return never
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function handlePopulateEnvironment(PopulateEnvironmentEvent $event)
{
throw new DcGeneralException(
__CLASS__ . '::handlePopulateEnvironment() is internal use only and must not be called'
);
}
/**
* {@inheritdoc}
*
* @throws DcGeneralException This method is for internal use only.
*/
protected function getTablenameCallback($tableName)
{
throw new DcGeneralException(
__CLASS__ . '::getTablenameCallback() is internal use only and must not be called'
);
}
/**
* {@inheritdoc}
*
* @throws DcGeneralRuntimeException The magic setter is unsupported and has been deactivated.
*/
public function __set($strKey, $varValue)
{
throw new DcGeneralRuntimeException('The magic setter is not supported anymore!');
}
/**
* {@inheritdoc}
*
* @throws DcGeneralRuntimeException When an unknown key is encountered.
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function __get($name)
{
switch ($name) {
case 'id':
if (null !== $this->getModel()) {
$model = $this->getModel();
assert($model instanceof ModelInterface);
return $model->getId();
}
$environment = $this->getEnvironment();
$dataDefinition = $environment->getDataDefinition();
assert($dataDefinition instanceof ContainerInterface);
$inputProvider = $environment->getInputProvider();
assert($inputProvider instanceof InputProviderInterface);
$idParameter = $inputProvider->hasParameter('id') ? 'id' : 'pid';
if (!$inputProvider->hasParameter($idParameter)) {
return null;
}
$modelId = ModelId::fromSerialized($inputProvider->getParameter($idParameter));
if ($modelId->getDataProviderName() === $dataDefinition->getName()) {
return $modelId->getId();
}
if (
('pid' === $idParameter)
|| !$inputProvider->hasParameter('pid')
|| !$inputProvider->getParameter('pid')
) {
return null;
}
$parentModelId = ModelId::fromSerialized($inputProvider->getParameter('pid'));
if ($dataDefinition->getName() !== $parentModelId->getDataProviderName()) {
return null;
}
return $parentModelId->getId();
case 'parentTable':
if ($this->getEnvironment()->getParentDataDefinition()) {
$container = $this->getEnvironment()->getParentDataDefinition();
assert($container instanceof ContainerInterface);
return $container->getName();
}
return null;
case 'childTable':
throw new DcGeneralRuntimeException('The magic property $dc->childTable is not supported yet!');
case 'rootIds':
throw new DcGeneralRuntimeException('The magic property $dc->rootIds is not supported yet!');
case 'createNewVersion':
throw new DcGeneralRuntimeException('The magic property $dc->createNewVersion is not supported yet!');
case 'table':
$dataProvider = $this->getEnvironment()->getDataProvider();
assert($dataProvider instanceof DataProviderInterface);
return $dataProvider->getEmptyModel()->getProviderName();
case 'value':
if (null !== $this->propertyName && null !== $this->getModel()) {
$model = $this->getModel();
assert($model instanceof ModelInterface);
return $model->getProperty($this->propertyName);
}
return null;
case 'field':
return $this->propertyName;
case 'inputName':
return $this->propertyName;
case 'palette':
throw new DcGeneralRuntimeException('The magic property $dc->palette is not supported yet!');
case 'activeRecord':
assert($this->model instanceof ModelInterface);
return new ActiveRecord($this->model);
default:
}
throw new DcGeneralRuntimeException('The magic property ' . $name . ' is not supported (yet)!');
}
public function getCurrentRecord(int|string|null $id = null, string|null $table = null): array|null
{
// FIXME: we can not implement this properly with dc caching as in DataContainer due to static functions there.
if ((null === $id && null === $table) && $this->model instanceof ModelInterface) {
return $this->model->getPropertiesAsArray();
}
return null;
}
}