Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TableSavior.php
Go to the documentation of this file.
1 <?php
8 
13 
18 {
22  private $selectGeneratorFactory;
23 
27  private $resourceConnection;
28 
32  private $dumpAccessor;
33 
37  private $selectFactory;
38 
46  public function __construct(
47  ResourceConnection $resourceConnection,
48  SelectGeneratorFactory $selectGeneratorFactory,
49  DumpAccessorInterface $dumpAccessor,
50  SelectFactory $selectFactory
51  ) {
52  $this->selectGeneratorFactory = $selectGeneratorFactory;
53  $this->resourceConnection = $resourceConnection;
54  $this->dumpAccessor = $dumpAccessor;
55  $this->selectFactory = $selectFactory;
56  }
57 
64  private function prepareTableSelect(Table $table)
65  {
66  $adapter = $this->resourceConnection->getConnection($table->getResource());
67  $select = $this->selectFactory->create($adapter);
68  $select->from($table->getName());
69  return $select;
70  }
71 
77  public function dump(ElementInterface $table)
78  {
79  $connectionName = $table->getResource();
80  $select = $this->prepareTableSelect($table);
81  $selectGenerator = $this->selectGeneratorFactory->create();
82  $resourceSignature = $this->generateDumpFileSignature($table);
83 
84  foreach ($selectGenerator->generator($select, $connectionName) as $data) {
85  $this->dumpAccessor->save($resourceSignature, $data);
86  }
87  }
88 
95  private function getTableColumnNames(Table $table)
96  {
97  $columns = [];
101  foreach ($table->getColumns() as $column) {
102  $columns[] = $column->getName();
103  }
104 
105  return $columns;
106  }
107 
114  private function applyDumpChunk(Table $table, $data)
115  {
116  $columns = $this->getTableColumnNames($table);
117  $adapter = $this->resourceConnection->getConnection($table->getResource());
118  $adapter->insertArray($table->getName(), $columns, $data);
119  }
120 
125  private function generateDumpFileSignature(Table $table)
126  {
127  return $table->getName();
128  }
129 
134  public function restore(ElementInterface $table)
135  {
136  $file = $this->generateDumpFileSignature($table);
137  $generator = $this->dumpAccessor->read($file);
138 
139  while ($generator->valid()) {
140  $data = $generator->current();
141  $this->applyDumpChunk($table, $data);
142  $generator->next();
143  }
144 
145  $this->dumpAccessor->destruct($file);
146  }
147 
152  {
153  return $element instanceof Table;
154  }
155 }
__construct(ResourceConnection $resourceConnection, SelectGeneratorFactory $selectGeneratorFactory, DumpAccessorInterface $dumpAccessor, SelectFactory $selectFactory)
Definition: TableSavior.php:46
$adapter
Definition: webapi_user.php:16
$columns
Definition: default.phtml:15
$table
Definition: trigger.php:14
$element
Definition: element.phtml:12