Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SchemaPersistor.php
Go to the documentation of this file.
1 <?php
7 
10 
15 {
19  private $componentRegistrar;
20 
24  private $xmlPersistor;
25 
32  public function __construct(ComponentRegistrar $componentRegistrar, XmlPersistor $xmlPersistor)
33  {
34  $this->componentRegistrar = $componentRegistrar;
35  $this->xmlPersistor = $xmlPersistor;
36  }
37 
43  private function initEmptyDom()
44  {
45  return new \SimpleXMLElement(
46  '<schema
47  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
48  xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
49  </schema>'
50  );
51  }
52 
58  public function persist(SchemaListener $schemaListener)
59  {
60  foreach ($schemaListener->getTables() as $moduleName => $tablesData) {
61  $path = $this->componentRegistrar->getPath(ComponentRegistrar::MODULE, $moduleName);
62  if (empty($path)) {
64  continue;
65  }
66  $schemaPatch = sprintf('%s/etc/db_schema.xml', $path);
67  if (file_exists($schemaPatch)) {
68  $dom = new \SimpleXMLElement(file_get_contents($schemaPatch));
69  } else {
70  $dom = $this->initEmptyDom();
71  }
72 
73  foreach ($tablesData as $tableName => $tableData) {
74  $tableData = $this->handleDefinition($tableData);
75  $table = $dom->addChild('table');
76  $table->addAttribute('name', $tableName);
77  $table->addAttribute('resource', $tableData['resource']);
78  if (isset($tableData['engine']) && $tableData['engine'] !== null) {
79  $table->addAttribute('engine', $tableData['engine']);
80  }
81 
82  $this->processColumns($tableData, $table);
83  $this->processConstraints($tableData, $table);
84  $this->processIndexes($tableData, $table);
85  }
86 
87  $this->persistModule($dom, $schemaPatch);
88  }
89  }
90 
98  private function handleDefinition(array $definition)
99  {
100  if (isset($definition['disabled']) && !$definition['disabled']) {
101  unset($definition['disabled']);
102  }
103 
104  return $definition;
105  }
106 
113  private function castBooleanToString($boolean)
114  {
115  return $boolean ? 'true' : 'false';
116  }
117 
125  private function processColumns(array $tableData, \SimpleXMLElement $table)
126  {
127  if (isset($tableData['columns'])) {
128  foreach ($tableData['columns'] as $columnData) {
129  $columnData = $this->handleDefinition($columnData);
130  $domColumn = $table->addChild('column');
131  $domColumn->addAttribute('xsi:type', $columnData['xsi:type'], 'xsi');
132  unset($columnData['xsi:type']);
133 
134  foreach ($columnData as $attributeKey => $attributeValue) {
135  if ($attributeValue === null) {
136  continue;
137  }
138 
139  if (is_bool($attributeValue)) {
140  $attributeValue = $this->castBooleanToString($attributeValue);
141  }
142 
143  $domColumn->addAttribute($attributeKey, $attributeValue);
144  }
145  }
146  }
147 
148  return $table;
149  }
150 
158  private function processIndexes(array $tableData, \SimpleXMLElement $table)
159  {
160  if (isset($tableData['indexes'])) {
161  foreach ($tableData['indexes'] as $indexName => $indexData) {
162  $indexData = $this->handleDefinition($indexData);
163  $domIndex = $table->addChild('index');
164  $domIndex->addAttribute('name', $indexName);
165 
166  if (isset($indexData['disabled']) && $indexData['disabled']) {
167  $domIndex->addAttribute('disabled', true);
168  } else {
169  $domIndex->addAttribute('indexType', $indexData['indexType']);
170 
171  foreach ($indexData['columns'] as $column) {
172  $columnXml = $domIndex->addChild('column');
173  $columnXml->addAttribute('name', $column);
174  }
175  }
176  }
177  }
178 
179  return $table;
180  }
181 
189  private function processConstraints(array $tableData, \SimpleXMLElement $table)
190  {
191  if (isset($tableData['constraints'])) {
192  foreach ($tableData['constraints'] as $constraintType => $constraints) {
193  if ($constraintType === 'foreign') {
194  foreach ($constraints as $name => $constraintData) {
195  $constraintData = $this->handleDefinition($constraintData);
196  $constraintDom = $table->addChild('constraint');
197  $constraintDom->addAttribute('xsi:type', $constraintType, 'xsi');
198  $constraintDom->addAttribute('name', $name);
199 
200  foreach ($constraintData as $attributeKey => $attributeValue) {
201  $constraintDom->addAttribute($attributeKey, $attributeValue);
202  }
203  }
204  } else {
205  foreach ($constraints as $name => $constraintData) {
206  $constraintData = $this->handleDefinition($constraintData);
207  $constraintDom = $table->addChild('constraint');
208  $constraintDom->addAttribute('xsi:type', $constraintType, 'xsi');
209  $constraintDom->addAttribute('name', $name);
210  $constraintData['columns'] = $constraintData['columns'] ?? [];
211 
212  if (isset($constraintData['disabled'])) {
213  $constraintDom->addAttribute('disabled', (bool) $constraintData['disabled']);
214  }
215 
216  foreach ($constraintData['columns'] as $column) {
217  $columnXml = $constraintDom->addChild('column');
218  $columnXml->addAttribute('name', $column);
219  }
220  }
221  }
222  }
223  }
224 
225  return $table;
226  }
227 
235  private function persistModule(\SimpleXMLElement $simpleXmlElementDom, $path)
236  {
237  $this->xmlPersistor->persist($simpleXmlElementDom, $path);
238  }
239 }
__construct(ComponentRegistrar $componentRegistrar, XmlPersistor $xmlPersistor)
$tableName
Definition: trigger.php:13
$componentRegistrar
Definition: bootstrap.php:23
persist(SchemaListener $schemaListener)
$table
Definition: trigger.php:14
if(!isset($_GET['name'])) $name
Definition: log.php:14