Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ColumnSavior.php
Go to the documentation of this file.
1 <?php
8 
14 
19 {
23  private $selectGeneratorFactory;
24 
28  private $resourceConnection;
29 
33  private $uniqueConstraintsResolver;
34 
38  private $dumpAccessor;
39 
43  private $selectFactory;
44 
53  public function __construct(
54  ResourceConnection $resourceConnection,
55  SelectGeneratorFactory $selectGeneratorFactory,
56  DumpAccessorInterface $dumpAccessor,
57  UniqueConstraintsResolver $uniqueConstraintsResolver,
58  SelectFactory $selectFactory
59  ) {
60  $this->selectGeneratorFactory = $selectGeneratorFactory;
61  $this->resourceConnection = $resourceConnection;
62  $this->uniqueConstraintsResolver = $uniqueConstraintsResolver;
63  $this->dumpAccessor = $dumpAccessor;
64  $this->selectFactory = $selectFactory;
65  }
66 
74  private function prepareColumnSelect(Column $column, array $fieldsToDump)
75  {
76  $adapter = $this->resourceConnection->getConnection($column->getTable()->getResource());
77  $select = $this->selectFactory->create($adapter);
78  $select->from($column->getTable()->getName(), $fieldsToDump);
79  return $select;
80  }
81 
87  public function dump(ElementInterface $column)
88  {
89  $columns = $this->uniqueConstraintsResolver->resolve($column->getTable());
90 
94  if ($columns) {
95  $connectionName = $column->getTable()->getResource();
96  $columns[] = $column->getName();
97  $select = $this->prepareColumnSelect($column, $columns);
98  $selectGenerator = $this->selectGeneratorFactory->create();
99  $resourceSignature = $this->generateDumpFileSignature($column);
100 
101  foreach ($selectGenerator->generator($select, $connectionName) as $data) {
102  $this->dumpAccessor->save($resourceSignature, $data);
103  }
104  }
105  }
106 
113  private function applyDumpChunk(Table $table, $data)
114  {
115  $columns = [];
116  $adapter = $this->resourceConnection->getConnection($table->getResource());
117  $firstRow = reset($data);
118 
122  foreach ($table->getColumns() as $column) {
123  $columns[$column->getName()] = $column->getName();
124  }
125 
126  $adapter->insertOnDuplicate($table->getName(), $data, array_keys($firstRow));
127  }
128 
133  private function generateDumpFileSignature(Column $column)
134  {
135  $dimensions = [
136  $column->getTable()->getName(),
137  $column->getElementType(),
138  $column->getName()
139  ];
140 
141  return implode("_", $dimensions);
142  }
143 
148  public function restore(ElementInterface $column)
149  {
150  $file = $this->generateDumpFileSignature($column);
151  $generator = $this->dumpAccessor->read($file);
152 
153  while ($generator->valid()) {
154  $data = $generator->current();
155  $this->applyDumpChunk(
156  $column->getTable(),
157  $data
158  );
159  $generator->next();
160  }
161 
162  $this->dumpAccessor->destruct($file);
163  }
164 
170  {
171  return $element instanceof Column;
172  }
173 }
__construct(ResourceConnection $resourceConnection, SelectGeneratorFactory $selectGeneratorFactory, DumpAccessorInterface $dumpAccessor, UniqueConstraintsResolver $uniqueConstraintsResolver, SelectFactory $selectFactory)
$adapter
Definition: webapi_user.php:16
$columns
Definition: default.phtml:15
$table
Definition: trigger.php:14
$element
Definition: element.phtml:12