-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Copy pathAbstractResource.php
279 lines (259 loc) · 7.46 KB
/
AbstractResource.php
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\Model\ResourceModel;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\DataObject;
use Magento\Framework\Model\CallbackPool;
use Magento\Framework\Serialize\Serializer\Json;
/**
* Abstract resource model
*
* phpcs:disable Magento2.Classes.AbstractApi
* @api
* @since 100.0.2
*/
abstract class AbstractResource
{
/**
* @var Json
* @since 101.0.0
*/
protected $serializer;
/**
* @var \Psr\Log\LoggerInterface
* @since 102.0.0
*/
protected $_logger;
/**
* Constructor
*/
public function __construct()
{
/**
* Please override this one instead of overriding real __construct constructor
*/
$this->_construct();
}
/**
* Resource initialization
*
* @return void
*/
abstract protected function _construct();
/**
* Get connection
*
* @return \Magento\Framework\DB\Adapter\AdapterInterface
*/
abstract public function getConnection();
/**
* Start resource transaction
*
* @return $this
* @api
*/
public function beginTransaction()
{
$this->getConnection()->beginTransaction();
return $this;
}
/**
* Subscribe some callback to transaction commit
*
* @param callable|array $callback
* @return $this
* @api
*/
public function addCommitCallback($callback)
{
CallbackPool::attach(spl_object_hash($this->getConnection()), $callback);
return $this;
}
/**
* Commit resource transaction
*
* @deprecated see \Magento\Framework\Model\ExecuteCommitCallbacks::afterCommit
* @return $this
* @api
*/
public function commit()
{
$this->getConnection()->commit();
/**
* Process after commit callbacks
*/
if ($this->getConnection()->getTransactionLevel() === 0) {
$callbacks = CallbackPool::get(spl_object_hash($this->getConnection()));
foreach ($callbacks as $callback) {
try {
call_user_func($callback);
} catch (\Exception $e) {
$this->getLogger()->critical($e);
}
}
}
return $this;
}
/**
* Roll back resource transaction
*
* @return $this
* @api
*/
public function rollBack()
{
$this->getConnection()->rollBack();
CallbackPool::clear(spl_object_hash($this->getConnection()));
return $this;
}
/**
* Serialize specified field in an object
*
* @param DataObject $object
* @param string $field
* @param mixed $defaultValue
* @param bool $unsetEmpty
* @return $this
*/
protected function _serializeField(DataObject $object, $field, $defaultValue = null, $unsetEmpty = false)
{
$value = $object->getData($field);
if (empty($value) && $unsetEmpty) {
$object->unsetData($field);
} else {
$object->setData($field, $this->getSerializer()->serialize($value ?: $defaultValue));
}
return $this;
}
/**
* Unserialize \Magento\Framework\DataObject field in an object
*
* @param \Magento\Framework\Model\AbstractModel $object
* @param string $field
* @param mixed $defaultValue
* @return void
*/
protected function _unserializeField(DataObject $object, $field, $defaultValue = null)
{
$value = $object->getData($field);
if ($value) {
$value = $this->getSerializer()->unserialize($object->getData($field));
if (empty($value)) {
$object->setData($field, $defaultValue);
} else {
$object->setData($field, $value);
}
} else {
$object->setData($field, $defaultValue);
}
}
/**
* Prepare data for passed table
*
* @param DataObject $object
* @param string $table
* @return array
*/
protected function _prepareDataForTable(DataObject $object, $table)
{
$data = [];
$fields = $this->getConnection()->describeTable($table);
foreach (array_keys($fields) as $field) {
if ($object->hasData($field)) {
$fieldValue = $object->getData($field);
if ($fieldValue instanceof \Zend_Db_Expr) {
$data[$field] = $fieldValue;
} else {
if (null !== $fieldValue) {
$fieldValue = $this->_prepareTableValueForSave($fieldValue, $fields[$field]['DATA_TYPE']);
$data[$field] = $this->getConnection()->prepareColumnValue($fields[$field], $fieldValue);
} elseif (!empty($fields[$field]['NULLABLE'])) {
$data[$field] = null;
}
}
}
}
return $data;
}
/**
* Prepare value for save
*
* @param mixed $value
* @param string $type
* @return mixed
*/
protected function _prepareTableValueForSave($value, $type)
{
$type = strtolower($type);
if ($type == 'decimal' || $type == 'numeric' || $type == 'float') {
$value = \Magento\Framework\App\ObjectManager::getInstance()->get(
\Magento\Framework\Locale\FormatInterface::class
)->getNumber(
$value
);
}
return $value;
}
/**
* Template method to return validate rules to be executed before entity is saved
*
* @return null
*/
public function getValidationRulesBeforeSave()
{
return null;
}
/**
* Prepare the list of entity fields that should be selected from DB. Apply filtration based on active fieldset.
*
* @param \Magento\Framework\Model\AbstractModel $object
* @param string $tableName
* @return array|string
*/
protected function _getColumnsForEntityLoad(\Magento\Framework\Model\AbstractModel $object, $tableName)
{
$fieldsetColumns = $object->getFieldset();
if (!empty($fieldsetColumns)) {
$connection = $this->getConnection();
if ($connection instanceof \Magento\Framework\DB\Adapter\AdapterInterface) {
$entityTableColumns = $connection->describeTable($tableName);
$columns = array_intersect($fieldsetColumns, array_keys($entityTableColumns));
}
}
if (empty($columns)) {
/** In case when fieldset was specified but no columns were matched with it, ID column is returned. */
$columns = empty($fieldsetColumns) ? '*' : [$object->getIdFieldName()];
}
return $columns;
}
/**
* Get serializer
*
* @return Json
* @deprecated 101.0.0
* @since 101.0.0
*/
protected function getSerializer()
{
if (null === $this->serializer) {
$this->serializer = ObjectManager::getInstance()->get(Json::class);
}
return $this->serializer;
}
/**
* Get logger
*
* @return \Psr\Log\LoggerInterface
* @deprecated 101.0.1
*/
private function getLogger()
{
if (null === $this->_logger) {
$this->_logger = ObjectManager::getInstance()->get(\Psr\Log\LoggerInterface::class);
}
return $this->_logger;
}
}