‪TYPO3CMS  ‪main
SchemaDiff.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Doctrine\DBAL\Schema\SchemaDiff as DoctrineSchemaDiff;
21 use Doctrine\DBAL\Schema\Sequence;
22 use Doctrine\DBAL\Schema\Table;
23 use Doctrine\DBAL\Schema\TableDiff as DoctrineTableDiff;
25 
32 class ‪SchemaDiff extends DoctrineSchemaDiff
33 {
48  public function ‪__construct(
49  public array $createdSchemas,
50  public array $droppedSchemas,
51  public array $createdTables,
52  public array $alteredTables,
53  public array $droppedTables,
54  public array $createdSequences,
55  public array $alteredSequences,
56  public array $droppedSequences,
57  ) {
58  $this->‪alteredTables = array_filter($alteredTables, static function (‪TableDiff $diff): bool {
59  return !$diff->‪isEmpty();
60  });
61  // NOTE: parent::__construct() not called by intention.
62  }
63 
65  public function ‪getCreatedSchemas(): array
66  {
67  return $this->createdSchemas;
68  }
69 
71  public function ‪getDroppedSchemas(): array
72  {
73  return $this->droppedSchemas;
74  }
75 
77  public function getCreatedTables(): array
78  {
79  return $this->createdTables;
80  }
81 
83  public function getAlteredTables(): array
84  {
85  return $this->alteredTables;
86  }
87 
89  public function getDroppedTables(): array
90  {
91  return $this->droppedTables;
92  }
93 
95  public function ‪getCreatedSequences(): array
96  {
97  return $this->createdSequences;
98  }
99 
101  public function ‪getAlteredSequences(): array
102  {
103  return $this->alteredSequences;
104  }
105 
107  public function ‪getDroppedSequences(): array
108  {
109  return $this->droppedSequences;
110  }
111 
115  public function ‪isEmpty(): bool
116  {
117  return count($this->createdSchemas) === 0
118  && count($this->droppedSchemas) === 0
119  && count($this->‪createdTables) === 0
120  && count($this->‪alteredTables) === 0
121  && count($this->‪droppedTables) === 0
122  && count($this->createdSequences) === 0
123  && count($this->alteredSequences) === 0
124  && count($this->droppedSequences) === 0;
125  }
126 
127  public static function ‪ensure(‪SchemaDiff|DoctrineSchemaDiff $schemaDiff): self
128  {
129  return new self(
130  // createdSchemas
131  $schemaDiff->getCreatedSchemas(),
132  // droppedSchemas
133  $schemaDiff->getDroppedSchemas(),
134  // createdTables
135  ‪self::ensureCollection(...$schemaDiff->getCreatedTables()),
136  // alteredTables
137  ‪self::ensureCollection(...$schemaDiff->getAlteredTables()),
138  // droppedTables
139  ‪self::ensureCollection(...$schemaDiff->getDroppedTables()),
140  // createdSequences
141  $schemaDiff->getCreatedSequences(),
142  // alteredSequences
143  $schemaDiff->getAlteredSequences(),
144  // droppedSequences
145  $schemaDiff->getDroppedSequences(),
146  );
147  }
148 
153  public static function ‪ensureCollection(DoctrineTableDiff|‪TableDiff|Table ...$tableDiffs): array
154  {
155  $collection = [];
156  foreach ($tableDiffs as $key => $tableDiff) {
157  if ($tableDiff instanceof DoctrineTableDiff) {
158  $tableDiff = ‪TableDiff::ensure($tableDiff);
159  }
160  if (is_int($key) || ‪MathUtility::canBeInterpretedAsInteger($key)) {
161  $key = $tableDiff instanceof Table
162  ? $tableDiff->getName()
163  : $tableDiff->getOldTable()->getName();
164  }
165  $collection[$key] = $tableDiff;
166  }
167  return $collection;
168  }
169 }
‪TYPO3\CMS\Core\Database\Schema\SchemaDiff\getDroppedSequences
‪array< Sequence > getDroppedSequences()
Definition: SchemaDiff.php:107
‪TYPO3\CMS\Core\Database\Schema\SchemaDiff\getCreatedSchemas
‪array< string > getCreatedSchemas()
Definition: SchemaDiff.php:65
‪TYPO3\CMS\Core\Database\Schema\SchemaDiff\getDroppedSchemas
‪array< string > getDroppedSchemas()
Definition: SchemaDiff.php:71
‪TYPO3\CMS\Core\Database\Schema\SchemaDiff\alteredTables
‪array< string, function getAlteredTables():array { return $this-> alteredTables
Definition: SchemaDiff.php:85
‪TYPO3\CMS\Core\Database\Schema\SchemaDiff\isEmpty
‪isEmpty()
Definition: SchemaDiff.php:115
‪TYPO3\CMS\Core\Database\Schema
Definition: ColumnDiff.php:18
‪TYPO3\CMS\Core\Database\Schema\SchemaDiff\ensure
‪static ensure(SchemaDiff|DoctrineSchemaDiff $schemaDiff)
Definition: SchemaDiff.php:127
‪TYPO3\CMS\Core\Database\Schema\TableDiff\ensure
‪static ensure(DoctrineTableDiff|TableDiff $tableDiff)
Definition: TableDiff.php:213
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger(mixed $var)
Definition: MathUtility.php:69
‪TYPO3\CMS\Core\Database\Schema\SchemaDiff\__construct
‪__construct(public array $createdSchemas, public array $droppedSchemas, public array $createdTables, public array $alteredTables, public array $droppedTables, public array $createdSequences, public array $alteredSequences, public array $droppedSequences,)
Definition: SchemaDiff.php:48
‪TYPO3\CMS\Core\Database\Schema\SchemaDiff\createdTables
‪array< string, function getCreatedTables():array { return $this-> createdTables
Definition: SchemaDiff.php:79
‪TYPO3\CMS\Core\Database\Schema\SchemaDiff\droppedTables
‪array< string, function getDroppedTables():array { return $this-> droppedTables
Definition: SchemaDiff.php:91
‪TYPO3\CMS\Core\Database\Schema\SchemaDiff\getCreatedSequences
‪array< Sequence > getCreatedSequences()
Definition: SchemaDiff.php:95
‪TYPO3\CMS\Core\Database\Schema\SchemaDiff\ensureCollection
‪static TableDiff[] Table[] ensureCollection(DoctrineTableDiff|TableDiff|Table ... $tableDiffs)
Definition: SchemaDiff.php:153
‪TYPO3\CMS\Core\Database\Schema\SchemaDiff
Definition: SchemaDiff.php:33
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:24
‪TYPO3\CMS\Core\Database\Schema\TableDiff\isEmpty
‪isEmpty()
Definition: TableDiff.php:186
‪TYPO3\CMS\Core\Database\Schema\TableDiff
Definition: TableDiff.php:33
‪TYPO3\CMS\Core\Database\Schema\SchemaDiff\getAlteredSequences
‪array< Sequence > getAlteredSequences()
Definition: SchemaDiff.php:101