CollationMetadataProvider
2 years ago
CollationMetadataProvider.php
2 years ago
Comparator.php
2 years ago
index.php
2 years ago
Comparator.php
52 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Doctrine\DBAL\Platforms\MySQL; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use MailPoetVendor\Doctrine\DBAL\Platforms\AbstractMySQLPlatform; |
| 5 | use MailPoetVendor\Doctrine\DBAL\Schema\Comparator as BaseComparator; |
| 6 | use MailPoetVendor\Doctrine\DBAL\Schema\Table; |
| 7 | use MailPoetVendor\Doctrine\DBAL\Schema\TableDiff; |
| 8 | use function array_diff_assoc; |
| 9 | use function array_intersect_key; |
| 10 | class Comparator extends BaseComparator |
| 11 | { |
| 12 | private $collationMetadataProvider; |
| 13 | public function __construct(AbstractMySQLPlatform $platform, CollationMetadataProvider $collationMetadataProvider) |
| 14 | { |
| 15 | parent::__construct($platform); |
| 16 | $this->collationMetadataProvider = $collationMetadataProvider; |
| 17 | } |
| 18 | public function compareTables(Table $fromTable, Table $toTable) : TableDiff |
| 19 | { |
| 20 | return parent::compareTables($this->normalizeColumns($fromTable), $this->normalizeColumns($toTable)); |
| 21 | } |
| 22 | public function diffTable(Table $fromTable, Table $toTable) |
| 23 | { |
| 24 | return parent::diffTable($this->normalizeColumns($fromTable), $this->normalizeColumns($toTable)); |
| 25 | } |
| 26 | private function normalizeColumns(Table $table) : Table |
| 27 | { |
| 28 | $tableOptions = array_intersect_key($table->getOptions(), ['charset' => null, 'collation' => null]); |
| 29 | $table = clone $table; |
| 30 | foreach ($table->getColumns() as $column) { |
| 31 | $originalOptions = $column->getPlatformOptions(); |
| 32 | $normalizedOptions = $this->normalizeOptions($originalOptions); |
| 33 | $overrideOptions = array_diff_assoc($normalizedOptions, $tableOptions); |
| 34 | if ($overrideOptions === $originalOptions) { |
| 35 | continue; |
| 36 | } |
| 37 | $column->setPlatformOptions($overrideOptions); |
| 38 | } |
| 39 | return $table; |
| 40 | } |
| 41 | private function normalizeOptions(array $options) : array |
| 42 | { |
| 43 | if (isset($options['collation']) && !isset($options['charset'])) { |
| 44 | $charset = $this->collationMetadataProvider->getCollationCharset($options['collation']); |
| 45 | if ($charset !== null) { |
| 46 | $options['charset'] = $charset; |
| 47 | } |
| 48 | } |
| 49 | return $options; |
| 50 | } |
| 51 | } |
| 52 |