Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Diff.php
Go to the documentation of this file.
1 <?php
8 
19 
27 class Diff implements DiffInterface
28 {
32  const GENERATED_WHITELIST_FILE_NAME = 'db_schema_whitelist.json';
33 
37  private $changes;
38 
44  public $debugChanges;
45 
49  private $whiteListTables = [];
50 
54  private $componentRegistrar;
55 
59  private $elementHistoryFactory;
60 
67  private $tableIndexes;
68 
75  private $destructiveOperations;
76 
85  public function __construct(
86  ComponentRegistrar $componentRegistrar,
87  ElementHistoryFactory $elementHistoryFactory,
88  array $tableIndexes,
89  array $destructiveOperations
90  ) {
91  $this->componentRegistrar = $componentRegistrar;
92  $this->elementHistoryFactory = $elementHistoryFactory;
93  $this->tableIndexes = $tableIndexes;
94  $this->destructiveOperations = $destructiveOperations;
95  }
96 
104  public function getAll()
105  {
106  if ($this->changes) {
107  ksort($this->changes);
108  }
109  return $this->changes;
110  }
111 
119  public function getChange($table, $operation)
120  {
121  $tableIndex = $this->tableIndexes[$table];
122  return $this->changes[$tableIndex][$operation] ?? [];
123  }
124 
132  private function getWhiteListTables()
133  {
134  if (!$this->whiteListTables) {
135  foreach ($this->componentRegistrar->getPaths(ComponentRegistrar::MODULE) as $path) {
136  $whiteListPath = $path . DIRECTORY_SEPARATOR . 'etc' .
137  DIRECTORY_SEPARATOR . 'db_schema_whitelist.json';
138 
139  if (file_exists($whiteListPath)) {
140  $this->whiteListTables = array_replace_recursive(
141  $this->whiteListTables,
142  json_decode(file_get_contents($whiteListPath), true)
143  );
144  }
145  }
146  }
147 
148  return $this->whiteListTables;
149  }
150 
163  public function canBeRegistered(ElementInterface $object, $operation): bool
164  {
165  if (!isset($this->destructiveOperations[$operation])) {
166  return true;
167  }
168 
169  $checkResult = false;
170  $whiteList = $this->getWhiteListTables();
171 
172  if ($object instanceof TableElementInterface) {
173  $tableNameWithoutPrefix = $object->getTable()->getNameWithoutPrefix();
174  $type = $object->getElementType();
175 
176  if ($this->isElementHaveAutoGeneratedName($object)) {
177  $checkResult =
178  isset($whiteList[$tableNameWithoutPrefix][$type][$object->getNameWithoutPrefix()]);
179  } else {
180  $checkResult = isset($whiteList[$tableNameWithoutPrefix][$type][$object->getName()]);
181  }
182  } elseif ($object instanceof Table) {
183  $checkResult = isset($whiteList[$object->getNameWithoutPrefix()]);
184  }
185 
186  return $checkResult;
187  }
188 
195  private function isElementHaveAutoGeneratedName(ElementInterface $element): bool
196  {
197  return in_array($element->getElementType(), [Index::TYPE, Constraint::TYPE], true);
198  }
199 
206  public function register(
207  ElementInterface $dtoObject,
208  $operation,
209  ElementInterface $oldDtoObject = null
210  ) {
211  if (!$this->canBeRegistered($dtoObject, $operation)) {
212  return $this;
213  }
214 
215  $historyData = ['new' => $dtoObject, 'old' => $oldDtoObject];
216  $history = $this->elementHistoryFactory->create($historyData);
217  //dtoObjects can have 4 types: column, constraint, index, table
218  $this->changes[$this->findTableIndex($dtoObject, $operation)][$operation][] = $history;
219  $this->debugChanges[$operation][] = $history;
220  return $this;
221  }
222 
233  private function findTableIndex(ElementInterface $element, string $operation) : int
234  {
236  $element->getTable()->getName() : $element->getName();
237  //We use not real tables but table indexes in order to be sure that order of table is correct
238  $tableIndex = $this->tableIndexes[$elementName] ?? INF;
239  return $operation === DropReference::OPERATION_NAME ? 0 : (int) $tableIndex;
240  }
241 }
$componentRegistrar
Definition: bootstrap.php:23
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$type
Definition: item.phtml:13
__construct(ComponentRegistrar $componentRegistrar, ElementHistoryFactory $elementHistoryFactory, array $tableIndexes, array $destructiveOperations)
Definition: Diff.php:85
canBeRegistered(ElementInterface $object, $operation)
Definition: Diff.php:163
$table
Definition: trigger.php:14
$elementName
Definition: gallery.phtml:10
$element
Definition: element.phtml:12