‪TYPO3CMS  10.4
SchemaColumnDefinitionListener.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\Event\SchemaColumnDefinitionEventArgs;
21 use Doctrine\DBAL\Platforms\AbstractPlatform;
22 use Doctrine\DBAL\Schema\Column;
23 use Doctrine\DBAL\Types\Type;
24 
30 {
38  public function ‪onSchemaColumnDefinition(SchemaColumnDefinitionEventArgs $event)
39  {
40  $tableColumn = $event->getTableColumn();
41  $tableColumn = array_change_key_case($tableColumn, CASE_LOWER);
42 
43  $dbType = $this->‪getDatabaseType($tableColumn['type']);
44  if ($dbType !== 'enum' && $dbType !== 'set') {
45  return;
46  }
47 
49  $tableColumn,
50  $event->getDatabasePlatform()
51  );
52 
53  $event->setColumn($column);
54  $event->preventDefault();
55  }
56 
66  protected function ‪getEnumerationTableColumnDefinition(array $tableColumn, AbstractPlatform $platform): Column
67  {
68  $options = [
69  'length' => $tableColumn['length'] ?? null,
70  'unsigned' => false,
71  'fixed' => false,
72  'default' => $tableColumn['default'] ?? null,
73  'notnull' => ($tableColumn['null'] ?? '') !== 'YES',
74  'scale' => null,
75  'precision' => null,
76  'autoincrement' => false,
77  'comment' => $tableColumn['comment'] ?? null,
78  ];
79 
80  $dbType = $this->‪getDatabaseType($tableColumn['type']);
81  $doctrineType = $platform->getDoctrineTypeMapping($dbType);
82 
83  $column = new Column($tableColumn['field'] ?? null, Type::getType($doctrineType), $options);
84  $column->setPlatformOption('unquotedValues', $this->‪getUnquotedEnumerationValues($tableColumn['type']));
85 
86  return $column;
87  }
88 
95  protected function ‪getDatabaseType(string $typeDefinition): string
96  {
97  $dbType = strtolower($typeDefinition);
98  $dbType = strtok($dbType, '(), ');
99 
100  return $dbType;
101  }
102 
107  protected function ‪getUnquotedEnumerationValues(string $typeDefinition): array
108  {
109  $valuesDefinition = preg_replace('#^(enum|set)\‍((.*)\‍)\s*$#i', '$2', $typeDefinition) ?? '';
110  $quoteChar = $valuesDefinition[0];
111  $separator = $quoteChar . ',' . $quoteChar;
112 
113  $valuesDefinition = preg_replace(
114  '#' . $quoteChar . ',\s*' . $quoteChar . '#',
115  $separator,
116  $valuesDefinition
117  ) ?? '';
118 
119  $values = explode($quoteChar . ',' . $quoteChar, substr($valuesDefinition, 1, -1)) ?: [];
120 
121  return array_map(
122  function (string $value) use ($quoteChar) {
123  return str_replace($quoteChar . $quoteChar, $quoteChar, $value);
124  },
125  $values
126  );
127  }
128 }
‪TYPO3\CMS\Core\Database\Schema\EventListener\SchemaColumnDefinitionListener\getUnquotedEnumerationValues
‪array getUnquotedEnumerationValues(string $typeDefinition)
Definition: SchemaColumnDefinitionListener.php:107
‪TYPO3\CMS\Core\Database\Schema\EventListener\SchemaColumnDefinitionListener\getDatabaseType
‪string getDatabaseType(string $typeDefinition)
Definition: SchemaColumnDefinitionListener.php:95
‪TYPO3\CMS\Core\Database\Schema\EventListener\SchemaColumnDefinitionListener\getEnumerationTableColumnDefinition
‪Doctrine DBAL Schema Column getEnumerationTableColumnDefinition(array $tableColumn, AbstractPlatform $platform)
Definition: SchemaColumnDefinitionListener.php:66
‪TYPO3\CMS\Core\Database\Schema\EventListener
Definition: SchemaAlterTableListener.php:18
‪TYPO3\CMS\Core\Database\Schema\EventListener\SchemaColumnDefinitionListener\onSchemaColumnDefinition
‪onSchemaColumnDefinition(SchemaColumnDefinitionEventArgs $event)
Definition: SchemaColumnDefinitionListener.php:38
‪TYPO3\CMS\Core\Database\Schema\EventListener\SchemaColumnDefinitionListener
Definition: SchemaColumnDefinitionListener.php:30