Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Persistor.php
Go to the documentation of this file.
1 <?php
8 
12 class Persistor extends \Magento\Framework\Code\Generator\EntityAbstract
13 {
17  const ENTITY_TYPE = 'persistor';
18 
24  protected function _getClassProperties()
25  {
26  $properties = [
27  [
28  'name' => $this->_getSourceFactoryPropertyName(),
29  'visibility' => 'protected',
30  'docblock' => [
31  'shortDescription' => 'Entity factory',
32  'tags' => [
33  [
34  'name' => 'var',
35  'description' => $this->getSourceClassName() . 'Factory',
36  ],
37  ],
38  ],
39  ],
40  [
41  'name' => $this->_getSourceResourcePropertyName(),
42  'visibility' => 'protected',
43  'docblock' => [
44  'shortDescription' => 'Resource model',
45  'tags' => [
46  [
47  'name' => 'var',
48  'description' => $this->_getSourceResourceClassName(),
49  ],
50  ],
51  ]
52  ],
53  [
54  'name' => 'resource',
55  'visibility' => 'protected',
56  'docblock' => [
57  'shortDescription' => 'Application Resource',
58  'tags' => [
59  [
60  'name' => 'var',
61  'description' => '\\' . \Magento\Framework\App\ResourceConnection::class,
62  ],
63  ],
64  ]
65  ],
66  [
67  'name' => 'connection',
68  'visibility' => 'protected',
69  'docblock' => [
70  'shortDescription' => 'Database Adapter',
71  'tags' => [
72  [
73  'name' => 'var',
74  'description' => '\\' . \Magento\Framework\DB\Adapter\AdapterInterface::class,
75  ],
76  ],
77  ]
78  ],
79  [
80  'name' => 'entitiesPool',
81  'visibility' => 'protected',
82  'defaultValue' => [],
83  'docblock' => [
84  'shortDescription' => '',
85  'tags' => [
86  [
87  'name' => 'var',
88  'description' => 'array',
89  ],
90  ],
91  ]
92  ],
93  [
94  'name' => 'stack',
95  'visibility' => 'protected',
96  'defaultValue' => [],
97  'docblock' => [
98  'shortDescription' => '',
99  'tags' => [
100  [
101  'name' => 'var',
102  'description' => 'array',
103  ],
104  ],
105  ]
106  ],
107  ];
108  return $properties;
109  }
110 
116  protected function _getSourceFactoryPropertyName()
117  {
118  return lcfirst($this->getSourceClassNameWithoutNamespace()) . 'Factory';
119  }
120 
125  protected function _getSourceResourcePropertyName() // InvoiceResource
126  {
127  return lcfirst($this->getSourceClassNameWithoutNamespace()) . "Resource";
128  }
129 
135  protected function _getSourceResourceClassName() // Invoice\Resource
136  {
137  $temporary = str_replace('\\Api\\Data\\', '\\Model\\Spi\\', $this->getSourceClassName());
138  $parts = explode('\\', ltrim($temporary, '\\'));
139  $className = array_pop($parts);
140  $className = str_replace('Interface', '', $className);
141  return '\\' . implode('\\', $parts) . '\\' . $className . 'ResourceInterface';
142  }
143 
149  protected function _getClassMethods()
150  {
151  return [
153  $this->_getGetConnectionMethod(),
154  $this->_getLoadEntityMethod(),
155  $this->_getRegisterDeletedMethod(),
156  $this->_getRegisterNewMethod(),
158  $this->_getDoPersistMethod(),
160  ];
161  }
162 
168  protected function _getDefaultConstructorDefinition()
169  {
170  return [
171  'name' => '__construct',
172  'parameters' => [
173  [
174  'name' => $this->_getSourceResourcePropertyName(),
175  'type' => $this->_getSourceResourceClassName(),
176  ],
177  [
178  'name' => $this->_getSourceFactoryPropertyName(),
179  'type' => $this->getSourceClassName() . 'Factory'
180  ],
181  [
182  'name' => 'resource',
183  'type' => '\\' . \Magento\Framework\App\ResourceConnection::class
184  ],
185  ],
186  'body' => "\$this->"
188  . " = \$" . $this->_getSourceResourcePropertyName() . ";\n"
189  . "\$this->"
191  . " = \$" . $this->_getSourceFactoryPropertyName() . ";\n"
192  . "\$this->resource = \$resource;"
193  ,
194  'docblock' => [
195  'shortDescription' => ucfirst(static::ENTITY_TYPE) . ' constructor',
196  'tags' => [
197  [
198  'name' => 'param',
199  'description' => $this->_getSourceResourceClassName()
200  . " \$" . $this->_getSourceResourcePropertyName(),
201  ],
202  [
203  'name' => 'param',
204  'description' => $this->getSourceClassName() . 'Factory'
205  . " \$" . $this->_getSourceFactoryPropertyName()
206  ],
207  [
208  'name' => 'param',
209  'description' => '\Magento\Framework\App\ResourceConnection $resource'
210  ],
211  ],
212  ]
213  ];
214  }
215 
219  protected function _getGetConnectionMethod()
220  {
221  $body = "if (!\$this->connection) {\n"
222  . " \$this->connection = \$this->resource->getConnection("
223  . "\\Magento\\Framework\\App\\ResourceConnection::DEFAULT_CONNECTION);\n"
224  . "}\n"
225  . "return \$this->connection;";
226 
227  return [
228  'name' => 'getConnection',
229  'parameters' => [],
230  'body' => $body,
231  'docblock' => [
232  'shortDescription' => 'Returns Adapter interface',
233  'tags' => [
234  [
235  'name' => 'return',
236  'description' => "array \\Magento\\Framework\\DB\\Adapter\\AdapterInterface",
237  ],
238  ],
239  ]
240  ];
241  }
242 
248  protected function _getLoadEntityMethod()
249  {
250  $body = "\$entity = \$this->{$this->_getSourceFactoryPropertyName()}->create()->load(\$key);\n"
251  . "return \$entity;";
252  return [
253  'name' => 'loadEntity',
254  'parameters' => [
255  [
256  'name' => 'key',
257  ],
258  ],
259  'body' => $body,
260  'docblock' => [
261  'shortDescription' => 'Load entity by key',
262  'tags' => [
263  [
264  'name' => 'param',
265  'description' => "int \$key",
266  ],
267  [
268  'name' => 'return',
269  'description' => $this->_getResultClassName() . " \$entity"
270  ],
271  ],
272  ]
273  ];
274  }
275 
281  protected function _getRegisterDeletedMethod()
282  {
283  $body = "\$hash = spl_object_hash(\$entity);\n"
284  . "array_push(\$this->stack, \$hash);\n"
285  . "\$this->entitiesPool[\$hash] = [\n"
286  . " 'entity' => \$entity,\n"
287  . " 'action' => 'removed'\n"
288  . "];";
289  return [
290  'name' => 'registerDeleted',
291  'parameters' => [
292  [
293  'name' => 'entity',
294  'type' => $this->getSourceClassName(),
295  ],
296  ],
297  'body' => $body,
298  'docblock' => [
299  'shortDescription' => 'Register entity to delete',
300  'tags' => [
301  [
302  'name' => 'param',
303  'description' => $this->getSourceClassName() . " \$entity",
304  ],
305  ],
306  ]
307  ];
308  }
309 
313  protected function _getDoPersistMethod()
314  {
315  $body = "\$ids = [];\n"
316  . "\$this->getConnection()->beginTransaction();\n"
317  . "try {\n"
318  . " do {\n"
319  . " \$hash = array_pop(\$this->stack);\n"
320  . " if (isset(\$this->entitiesPool[\$hash])) {\n"
321  . " \$data = \$this->entitiesPool[\$hash];\n"
322  . " \$entity = \$data['entity'];\n"
323  . " if (\$data['action'] == 'created') {\n"
324  . " \$this->{$this->_getSourceResourcePropertyName()}->save(\$entity);\n"
325  . " \$ids[] = \$entity->getId();\n"
326  . " } else {\n"
327  . " \$ids[] = \$entity->getId();\n"
328  . " \$this->{$this->_getSourceResourcePropertyName()}->delete(\$entity);\n"
329  . " }\n"
330  . " }\n"
331  . " unset(\$this->entitiesPool[\$hash]);\n"
332  . " \$items--;\n"
333  . " } while (!empty(\$this->entitiesPool) || \$items === 0);\n"
334  . " \$this->getConnection()->commit();\n"
335  . " return \$ids;\n"
336  . "} catch (\\Exception \$e) {\n"
337  . " \$this->getConnection()->rollback();\n"
338  . " throw \$e;\n"
339  . "}";
340  return [
341  'name' => 'doPersist',
342  'parameters' => [
343  [
344  'name' => 'items',
345  'defaultValue' => 0,
346 
347  ],
348  ],
349  'body' => $body,
350  'docblock' => [
351  'shortDescription' => 'Perform persist operation',
352  'tags' => [
353  [
354  'name' => 'param',
355  'description' => "int \$items",
356  ],
357  [
358  'name' => 'return',
359  'description' => "array",
360  ],
361  ],
362  ]
363  ];
364  }
365 
371  protected function _getDoPersistEntityMethod()
372  {
373  $body = "\$hash = spl_object_hash(\$entity);\n"
374  . "\$action = 'created';\n"
375  . "if (isset(\$this->entitiesPool[\$hash])) {\n"
376  . " \$action = \$this->entitiesPool[\$hash]['action'];\n"
377  . " \$tempStack = \$this->stack;\n"
378  . " array_flip(\$tempStack);\n"
379  . " unset(\$tempStack[\$hash]);\n"
380  . " \$this->stack = array_flip(\$tempStack);\n"
381  . " unset(\$this->entitiesPool[\$hash]);\n"
382  . "}\n"
383  . "\$action == 'created' ? \$this->registerNew(\$entity) : \$this->registerDeleted(\$entity);\n"
384  . "return \$this->doPersist(1);";
385  return [
386  'name' => 'doPersistEntity',
387  'parameters' => [
388  [
389  'name' => 'entity',
390  'type' => $this->getSourceClassName(),
391  ],
392  ],
393  'body' => $body,
394  'docblock' => [
395  'shortDescription' => 'Persist entity',
396  'tags' => [
397  [
398  'name' => 'param',
399  'description' => $this->getSourceClassName() . " \$entity",
400  ],
401  ],
402  ]
403  ];
404  }
405 
411  protected function _getRegisterFromArrayMethod()
412  {
413  $body = "\$entity = \$this->{$this->_getSourceFactoryPropertyName()}->create(['data' => \$data]);\n"
414  . "\$this->registerNew(\$entity);\n"
415  . "return \$entity;";
416  return [
417  'name' => 'registerFromArray',
418  'parameters' => [
419  [
420  'name' => 'data',
421  'type' => 'array',
422  ],
423  ],
424  'body' => $body,
425  'docblock' => [
426  'shortDescription' => 'Register entity to create',
427  'tags' => [
428  [
429  'name' => 'param',
430  'description' => "array \$data",
431  ],
432  [
433  'name' => 'param',
434  'description' => $this->getSourceClassName() . " \$entity",
435  ],
436  ],
437  ]
438  ];
439  }
440 
446  protected function _getRegisterNewMethod()
447  {
448  $body = "\$hash = spl_object_hash(\$entity);\n"
449  . "\$data = [\n"
450  . " 'entity' => \$entity,\n"
451  . " 'action' => 'created'\n"
452  . "];\n"
453  . "array_push(\$this->stack, \$hash);\n"
454  . "\$this->entitiesPool[\$hash] = \$data;";
455  return [
456  'name' => 'registerNew',
457  'parameters' => [
458  [
459  'name' => 'entity',
460  'type' => $this->getSourceClassName(),
461  ],
462  ],
463  'body' => $body,
464  'docblock' => [
465  'shortDescription' => 'Register entity to create',
466  'tags' => [
467 
468  [
469  'name' => 'param',
470  'description' => $this->getSourceClassName() . " \$entity",
471  ],
472  ],
473  ]
474  ];
475  }
476 
480  protected function _validateData()
481  {
482  $result = parent::_validateData();
483 
484  if ($result) {
485  $sourceClassName = $this->getSourceClassName();
486  $resultClassName = $this->_getResultClassName();
487 
488  if ($resultClassName !== $sourceClassName . 'Persistor') {
489  $this->_addError(
490  'Invalid Factory class name [' . $resultClassName . ']. Use ' . $sourceClassName . 'Persistor'
491  );
492  $result = false;
493  }
494  }
495  return $result;
496  }
497 }
$properties
Definition: categories.php:26
if($currentSelectedMethod==$_code) $className
Definition: form.phtml:31