1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Propel package - modified by Spryker Systems GmbH. |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with the source code of the extended class. |
7
|
|
|
* |
8
|
|
|
* @license MIT License |
9
|
|
|
* @see https://github.com/propelorm/Propel2 |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Spryker\Zed\PropelOrm\Business\Builder; |
13
|
|
|
|
14
|
|
|
use Propel\Generator\Builder\Om\ClassTools; |
15
|
|
|
use Propel\Generator\Builder\Om\ObjectBuilder as PropelObjectBuilder; |
16
|
|
|
use Propel\Generator\Model\Column; |
17
|
|
|
use Propel\Generator\Model\Table; |
18
|
|
|
use Spryker\Shared\Config\Application\Environment; |
19
|
|
|
use Spryker\Shared\ErrorHandler\ErrorHandlerEnvironment; |
20
|
|
|
use Spryker\Zed\Kernel\Business\FactoryResolverAwareTrait as BusinessFactoryResolverAwareTrait; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @method \Spryker\Zed\PropelOrm\Business\PropelOrmBusinessFactory getFactory() |
24
|
|
|
*/ |
25
|
|
|
class ObjectBuilder extends PropelObjectBuilder |
26
|
|
|
{ |
27
|
|
|
use BusinessFactoryResolverAwareTrait; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param \Propel\Generator\Model\Table $table |
31
|
|
|
*/ |
32
|
|
|
public function __construct(Table $table) |
33
|
|
|
{ |
34
|
|
|
parent::__construct($table); |
35
|
|
|
|
36
|
|
|
Environment::initialize(); |
37
|
|
|
|
38
|
|
|
$errorHandlerEnvironment = new ErrorHandlerEnvironment(); |
39
|
|
|
$errorHandlerEnvironment->initialize(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Changes default Propel behavior. |
44
|
|
|
* |
45
|
|
|
* Adds setter method for boolean columns. |
46
|
|
|
* |
47
|
|
|
* @see \Propel\Generator\Builder\Om\ObjectBuilder::addColumnMutators() |
48
|
|
|
* |
49
|
|
|
* @param string $script The script will be modified in this method. |
50
|
|
|
* @param \Propel\Generator\Model\Column $col The current column. |
51
|
|
|
* |
52
|
|
|
* @return void |
53
|
|
|
*/ |
54
|
|
|
protected function addBooleanMutator(string &$script, Column $col): void |
55
|
|
|
{ |
56
|
|
|
$clo = $col->getLowercasedName(); |
57
|
|
|
|
58
|
|
|
$this->addBooleanMutatorComment($script, $col); |
59
|
|
|
$this->addMutatorOpenOpen($script, $col); |
60
|
|
|
$this->addMutatorOpenBody($script, $col); |
61
|
|
|
|
62
|
|
|
$allowNullValues = ($col->getAttribute('required', 'true') === 'true') ? 'false' : 'true'; |
63
|
|
|
|
64
|
|
|
$hasDefaultValue = $col->getDefaultValue() === null ? 'false' : 'true'; |
65
|
|
|
|
66
|
|
|
$script .= " |
67
|
|
|
if (\$v !== null) { |
68
|
|
|
if (is_string(\$v)) { |
69
|
|
|
\$v = in_array(strtolower(\$v), array('false', 'off', '-', 'no', 'n', '0', '')) ? false : true; |
70
|
|
|
} else { |
71
|
|
|
\$v = (bool) \$v; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
\$allowNullValues = $allowNullValues; |
76
|
|
|
|
77
|
|
|
if (\$v === null && !\$allowNullValues) { |
78
|
|
|
return \$this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// When this is true we will not check for value equality as we need to be able to set a value for this field |
82
|
|
|
// to its initial value and have the column marked as modified. This is relevant for update cases when |
83
|
|
|
// we create an instance of an entity manually. |
84
|
|
|
// @see \Spryker\Zed\Kernel\Persistence\EntityManager\TransferToEntityMapper::mapEntity() |
85
|
|
|
\$hasDefaultValue = $hasDefaultValue; |
86
|
|
|
|
87
|
|
|
if ((\$this->isNew() && \$hasDefaultValue) || \$this->$clo !== \$v) { |
88
|
|
|
\$this->$clo = \$v; |
89
|
|
|
\$this->modifiedColumns[" . $this->getColumnConstant($col) . "] = true; |
90
|
|
|
} |
91
|
|
|
"; |
92
|
|
|
$this->addMutatorClose($script, $col); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Adds setter method for "normal" columns. |
97
|
|
|
* |
98
|
|
|
* @see parent::addColumnMutators() |
99
|
|
|
* |
100
|
|
|
* @param string $script The script will be modified in this method. |
101
|
|
|
* @param \Propel\Generator\Model\Column $col The current column. |
102
|
|
|
* |
103
|
|
|
* @return void |
104
|
|
|
*/ |
105
|
|
|
protected function addDefaultMutator(string &$script, Column $col): void |
106
|
|
|
{ |
107
|
|
|
$clo = $col->getLowercasedName(); |
108
|
|
|
|
109
|
|
|
$this->addMutatorOpen($script, $col); |
110
|
|
|
|
111
|
|
|
// Perform type-casting to ensure that we can use type-sensitive |
112
|
|
|
// checking in mutators. |
113
|
|
|
if ($col->isPhpPrimitiveType()) { |
114
|
|
|
$script .= " |
115
|
|
|
if (\$v !== null) { |
116
|
|
|
\$v = (" . $col->getPhpType() . ") \$v; |
117
|
|
|
} |
118
|
|
|
"; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$hasDefaultValue = $col->getDefaultValue() === null ? 'false' : 'true'; |
122
|
|
|
|
123
|
|
|
$script .= " |
124
|
|
|
// When this is true we will not check for value equality as we need to be able to set a value for this field |
125
|
|
|
// to its initial value and have the column marked as modified. This is relevant for update cases when |
126
|
|
|
// we create an instance of an entity manually. |
127
|
|
|
// @see \Spryker\Zed\Kernel\Persistence\EntityManager\TransferToEntityMapper::mapEntity() |
128
|
|
|
\$hasDefaultValue = $hasDefaultValue; |
129
|
|
|
|
130
|
|
|
if ((\$this->isNew() && \$hasDefaultValue) || \$this->$clo !== \$v) { |
131
|
|
|
\$this->$clo = \$v; |
132
|
|
|
\$this->modifiedColumns[" . $this->getColumnConstant($col) . "] = true; |
133
|
|
|
} |
134
|
|
|
"; |
135
|
|
|
$this->addMutatorClose($script, $col); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Specifies the methods that are added as part of the basic OM class. |
140
|
|
|
* This can be overridden by subclasses that wish to add more methods. |
141
|
|
|
* |
142
|
|
|
* @see ObjectBuilder::addClassBody() |
143
|
|
|
* |
144
|
|
|
* @param string $script |
145
|
|
|
* |
146
|
|
|
* @return void |
147
|
|
|
*/ |
148
|
|
|
protected function addClassBody(string &$script): void |
149
|
|
|
{ |
150
|
|
|
$classes = $this->getFactory() |
151
|
|
|
->createtPostSaveClassNamespacesCollector() |
152
|
|
|
->extractClassesToDeclare(); |
153
|
|
|
|
154
|
|
|
foreach ($classes as $class) { |
155
|
|
|
$this->declareClass($class); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
parent::addClassBody($script); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Adds the base object hook functions. |
163
|
|
|
* |
164
|
|
|
* @param string $script |
165
|
|
|
* |
166
|
|
|
* @return void |
167
|
|
|
*/ |
168
|
|
|
protected function addHookMethods(string &$script): void |
169
|
|
|
{ |
170
|
|
|
$hooks = []; |
171
|
|
|
foreach (['pre', 'post'] as $hook) { |
172
|
|
|
foreach (['Insert', 'Update', 'Save', 'Delete'] as $action) { |
173
|
|
|
$hooks[$hook . $action] = strpos($script, 'function ' . $hook . $action . '(') === false; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** @var string|null $className */ |
178
|
|
|
$className = ClassTools::classname($this->getBaseClass()); |
179
|
|
|
$hooks['hasBaseClass'] = $this->getBehaviorContent('parentClass') !== null || $className !== null; |
180
|
|
|
|
181
|
|
|
$script .= $this->renderTemplate('baseObjectMethodHook', $hooks); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Adds the function close for the save method |
186
|
|
|
* |
187
|
|
|
* @see addSave() |
188
|
|
|
* |
189
|
|
|
* @param string $script The script will be modified in this method. |
190
|
|
|
* |
191
|
|
|
* @return void |
192
|
|
|
*/ |
193
|
|
|
protected function addSaveClose(string &$script): void |
194
|
|
|
{ |
195
|
|
|
$script .= " |
196
|
|
|
} |
197
|
|
|
"; |
198
|
|
|
$this->addPostSaveMethodProcess($script); |
199
|
|
|
$this->addPostUpdateMethodProcess($script); |
200
|
|
|
$this->addPostDeleteMethodProcess($script); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Adds custom postSave hook to Propel instance |
205
|
|
|
* |
206
|
|
|
* @param string $script |
207
|
|
|
* |
208
|
|
|
* @return void |
209
|
|
|
*/ |
210
|
|
|
protected function addPostSaveMethodProcess(string &$script): void |
211
|
|
|
{ |
212
|
|
|
$script .= " |
213
|
|
|
/** |
214
|
|
|
* Code to be run after persisting the object |
215
|
|
|
* @param \\Propel\\Runtime\\Connection\\ConnectionInterface|null \$con |
216
|
|
|
* |
217
|
|
|
* @return void |
218
|
|
|
*/ |
219
|
|
|
public function postSave(?ConnectionInterface \$con = null): void |
220
|
|
|
{"; |
221
|
|
|
|
222
|
|
|
$extensionPlugins = $this->getFactory()->getPostSaveExtensionPlugins(); |
223
|
|
|
foreach ($extensionPlugins as $plugin) { |
224
|
|
|
$script = $plugin->extend($script); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
$script .= " |
228
|
|
|
} |
229
|
|
|
"; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Adds custom postUpdate hook to Propel instance |
234
|
|
|
* |
235
|
|
|
* @param string $script |
236
|
|
|
* |
237
|
|
|
* @return void |
238
|
|
|
*/ |
239
|
|
|
protected function addPostUpdateMethodProcess(string &$script): void |
240
|
|
|
{ |
241
|
|
|
$script .= " |
242
|
|
|
/** |
243
|
|
|
* Code to be run after updating the object in database |
244
|
|
|
* @param \\Propel\\Runtime\\Connection\\ConnectionInterface|null \$con |
245
|
|
|
* |
246
|
|
|
* @return void |
247
|
|
|
*/ |
248
|
|
|
public function postUpdate(?ConnectionInterface \$con = null): void |
249
|
|
|
{"; |
250
|
|
|
|
251
|
|
|
$extensionPlugins = $this->getFactory()->getPostUpdateExtensionPlugins(); |
252
|
|
|
foreach ($extensionPlugins as $plugin) { |
253
|
|
|
$script = $plugin->extend($script); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
$script .= " |
257
|
|
|
} |
258
|
|
|
"; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Adds custom postDelete hook to Propel instance |
263
|
|
|
* |
264
|
|
|
* @param string $script |
265
|
|
|
* |
266
|
|
|
* @return void |
267
|
|
|
*/ |
268
|
|
|
protected function addPostDeleteMethodProcess(string &$script): void |
269
|
|
|
{ |
270
|
|
|
$script .= " |
271
|
|
|
/** |
272
|
|
|
* Code to be run after deleting the object in database |
273
|
|
|
* @param \\Propel\\Runtime\\Connection\\ConnectionInterface|null \$con |
274
|
|
|
* |
275
|
|
|
* @return void |
276
|
|
|
*/ |
277
|
|
|
public function postDelete(?ConnectionInterface \$con = null): void |
278
|
|
|
{"; |
279
|
|
|
|
280
|
|
|
$extensionPlugins = $this->getFactory()->getPostDeleteExtensionPlugins(); |
281
|
|
|
foreach ($extensionPlugins as $plugin) { |
282
|
|
|
$script = $plugin->extend($script); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
$script .= " |
286
|
|
|
} |
287
|
|
|
"; |
288
|
|
|
} |
289
|
|
|
} |
290
|
|
|
|