Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
OperationsExecutor.php
Go to the documentation of this file.
1 <?php
8 
19 
27 {
31  const KEY_SAFE_MODE = 'safe-mode';
32  const KEY_DATA_RESTORE = 'data-restore';
33 
37  private $operations;
38 
42  private $sharding;
43 
47  private $resourceConnection;
48 
52  private $statementFactory;
53 
57  private $dbSchemaWriter;
58 
62  private $statementAggregatorFactory;
63 
67  private $dataSaviorsCollection;
68 
72  private $dryRunLogger;
73 
86  public function __construct(
87  array $operations,
88  array $dataSaviorsCollection,
89  Sharding $sharding,
90  ResourceConnection $resourceConnection,
91  StatementFactory $statementFactory,
92  DbSchemaWriterInterface $dbSchemaWriter,
93  StatementAggregatorFactory $statementAggregatorFactory,
94  DryRunLogger $dryRunLogger
95  ) {
96  $this->operations = $operations;
97  $this->sharding = $sharding;
98  $this->resourceConnection = $resourceConnection;
99  $this->statementFactory = $statementFactory;
100  $this->dbSchemaWriter = $dbSchemaWriter;
101  $this->statementAggregatorFactory = $statementAggregatorFactory;
102  $this->dataSaviorsCollection = $dataSaviorsCollection;
103  $this->dryRunLogger = $dryRunLogger;
104  }
105 
113  public function getDestructiveOperations()
114  {
115  $operations = [];
116 
117  foreach ($this->operations as $operation) {
118  if ($operation->isOperationDestructive()) {
119  $operations[$operation->getOperationName()] = $operation->getOperationName();
120  }
121  }
122 
123  return $operations;
124  }
125 
132  private function startSetupForAllConnections()
133  {
134  foreach ($this->sharding->getResources() as $resource) {
135  $this->resourceConnection->getConnection($resource)
136  ->startSetup();
137  $this->resourceConnection->getConnection($resource)
138  ->query('SET UNIQUE_CHECKS=0');
139  }
140  }
141 
148  private function endSetupForAllConnections()
149  {
150  foreach ($this->sharding->getResources() as $resource) {
151  $this->resourceConnection->getConnection($resource)
152  ->endSetup();
153  }
154  }
155 
162  private function operationIsOppositeToDestructive(OperationInterface $operation)
163  {
164  return $operation instanceof AddColumn ||
165  $operation instanceof CreateTable ||
166  $operation instanceof ReCreateTable;
167  }
168 
180  public function execute(DiffInterface $diff, array $requestData)
181  {
182  $this->startSetupForAllConnections();
183  $tableHistories = $diff->getAll();
186 
187  if ($dryRun) {
188  $this->dryRunLogger->prepareToDryRun();
189  }
190 
191  if (is_array($tableHistories)) {
192  foreach ($tableHistories as $tableHistory) {
193  $destructiveElements = [];
194  $oppositeToDestructiveElements = [];
195  $statementAggregator = $this->statementAggregatorFactory->create();
196 
197  foreach ($this->operations as $operation) {
198  if (isset($tableHistory[$operation->getOperationName()])) {
200  foreach ($tableHistory[$operation->getOperationName()] as $elementHistory) {
201  $statementAggregator->addStatements($operation->doOperation($elementHistory));
202 
203  if ($operation->isOperationDestructive()) {
204  $destructiveElements[] = $elementHistory->getOld();
205  } elseif ($this->operationIsOppositeToDestructive($operation)) {
206  $oppositeToDestructiveElements[] = $elementHistory->getNew();
207  }
208  }
209  }
210  }
211 
212  $this->doDump($destructiveElements, $requestData);
213  $this->dbSchemaWriter->compile($statementAggregator, $dryRun);
214  $this->doRestore($oppositeToDestructiveElements, $requestData);
215  }
216  }
217 
218  $this->endSetupForAllConnections();
219  }
220 
227  private function doRestore(array $elements, array $requestData)
228  {
229  $restoreMode = isset($requestData[self::KEY_DATA_RESTORE]) && $requestData[self::KEY_DATA_RESTORE];
230 
231  if ($restoreMode) {
235  foreach ($elements as $element) {
236  foreach ($this->dataSaviorsCollection as $dataSavior) {
237  if ($dataSavior->isAcceptable($element)) {
238  $dataSavior->restore($element);
239  break;
240  }
241  }
242  }
243  }
244  }
245 
252  private function doDump(array $elements, array $requestData)
253  {
254  $safeMode = isset($requestData[self::KEY_SAFE_MODE]) && $requestData[self::KEY_SAFE_MODE];
255 
256  if ($safeMode) {
260  foreach ($elements as $element) {
261  foreach ($this->dataSaviorsCollection as $dataSavior) {
262  if ($dataSavior->isAcceptable($element)) {
263  $dataSavior->dump($element);
264  break;
265  }
266  }
267  }
268  }
269  }
270 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$operations
Definition: bulk.php:55
$resource
Definition: bulk.php:12
__construct(array $operations, array $dataSaviorsCollection, Sharding $sharding, ResourceConnection $resourceConnection, StatementFactory $statementFactory, DbSchemaWriterInterface $dbSchemaWriter, StatementAggregatorFactory $statementAggregatorFactory, DryRunLogger $dryRunLogger)
$element
Definition: element.phtml:12