6 declare(strict_types=1);
44 private $tablesData = [];
54 private $elementFactory;
59 private $booleanUtils;
64 private $validationComposite;
69 private $resourceConnection;
74 private $elementNameResolver;
94 $this->sharding = $sharding;
95 $this->elementFactory = $elementFactory;
96 $this->booleanUtils = $booleanUtils;
97 $this->validationComposite = $validationComposite;
99 $this->elementNameResolver = $elementNameResolver;
112 $this->tablesData = $tablesData;
129 $messages .= sprintf(
"%s%s", PHP_EOL, $error[
'message']);
145 foreach ($this->tablesData as $tableData) {
146 if (!
$schema->getTableByName($tableData[
'name'])) {
147 if (!$this->isDisabled($tableData)) {
148 $this->processTable(
$schema, $tableData);
164 private function getStructuralElementResource(array $tableData): string
166 return isset($tableData[
'resource']) && $this->sharding->canUseResource($tableData[
'resource']) ?
167 $tableData[
'resource'] :
'default';
176 private function isDisabled(array $structuralElementData): bool
178 return isset($structuralElementData[
'disabled']) &&
179 $this->booleanUtils->toBoolean($structuralElementData[
'disabled']);
196 foreach ($tableData[
'column'] as $columnData) {
197 if ($this->isDisabled($columnData)) {
201 $columnData = $this->processGenericData($columnData,
$resource,
$table);
202 $column = $this->elementFactory->create($columnData[
'type'], $columnData);
203 $columns[$column->getName()] = $column;
217 private function processGenericData(array $elementData,
string $resource, Table
$table): array
219 $elementData[
'table'] =
$table;
234 private function processTable(Schema
$schema, array $tableData): Table
236 if (!
$schema->getTableByName($tableData[
'name'])) {
237 $resource = $this->getStructuralElementResource($tableData);
239 $tableData[
'comment'] = $tableData[
'comment'] ??
null;
241 $table = $this->elementFactory->create(
'table', $tableData);
251 return $schema->getTableByName($tableData[
'name']);
261 private function getColumnByName(
string $columnName, Table
$table): Column
263 $columnCandidate =
$table->getColumnByName($columnName);
265 if (!$columnCandidate) {
266 throw new \LogicException(
267 sprintf(
'Table %s do not have column with name %s',
$table->getName(), $columnName)
271 return $columnCandidate;
281 private function convertColumnNamesToObjects(array $columnNames, Table
$table): array
285 foreach ($columnNames as $columnName) {
300 private function processIndexes(array $tableData,
string $resource, Table
$table): array
302 if (!isset($tableData[
'index'])) {
308 foreach ($tableData[
'index'] as $indexData) {
309 if ($this->isDisabled($indexData)) {
313 $indexData[
'name'] = $this->elementNameResolver->getFullIndexName(
315 $indexData[
'column'],
316 $indexData[
'indexType'] ??
null 319 $indexData[
'columns'] = $this->convertColumnNamesToObjects($indexData[
'column'],
$table);
320 $index = $this->elementFactory->create(
'index', $indexData);
336 private function processConstraints(array $tableData,
string $resource, Schema
$schema, Table
$table): array
338 if (!isset($tableData[
'constraint'])) {
344 foreach ($tableData[
'constraint'] as $constraintData) {
345 if ($this->isDisabled($constraintData)) {
348 $constraintData = $this->processGenericData($constraintData,
$resource,
$table);
350 if ($constraintData[
'type'] ===
'foreign') {
351 $constraintData[
'column'] = $this->getColumnByName($constraintData[
'column'],
$table);
352 $referenceTableData = $this->tablesData[$constraintData[
'referenceTable']];
355 $refTableName = $this->resourceConnection->getTableName($referenceTableData[
'name']);
356 $referenceTable = $refTableName ===
$table->getName() ?
358 $this->processTable(
$schema, $referenceTableData);
360 if ($referenceTable->getResource() !==
$table->getResource()) {
364 $constraintData[
'referenceTable'] = $referenceTable;
366 if (!$constraintData[
'referenceTable']) {
367 throw new \LogicException(
368 sprintf(
'Cannot find reference table with name %s', $constraints[
'referenceTable'])
372 $constraintData[
'referenceColumn'] = $this->getColumnByName(
373 $constraintData[
'referenceColumn'],
374 $constraintData[
'referenceTable']
376 $constraintData[
'name'] = $this->elementNameResolver->getFullFKName(
378 $constraintData[
'column'],
379 $constraintData[
'referenceTable'],
380 $constraintData[
'referenceColumn']
382 $constraint = $this->elementFactory->create($constraintData[
'type'], $constraintData);
385 $constraintData[
'name'] = $this->elementNameResolver->getFullIndexName(
387 $constraintData[
'column'],
388 $constraintData[
'type']
390 $constraintData[
'columns'] = $this->convertColumnNamesToObjects($constraintData[
'column'],
$table);
391 $constraint = $this->elementFactory->create($constraintData[
'type'], $constraintData);
__construct(ElementFactory $elementFactory, BooleanUtils $booleanUtils, Sharding $sharding, ValidationComposite $validationComposite, \Magento\Framework\App\ResourceConnection $resourceConnection, ElementNameResolver $elementNameResolver)
addTablesData(array $tablesData)