‪TYPO3CMS  9.5
SchemaColumnDefinitionListener.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
3 
5 
6 /*
7  * This file is part of the TYPO3 CMS project.
8  *
9  * It is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License, either version 2
11  * of the License, or any later version.
12  *
13  * For the full copyright and license information, please read the
14  * LICENSE.txt file that was distributed with this source code.
15  *
16  * The TYPO3 project - inspiring people to share!
17  */
18 
19 use Doctrine\DBAL\Event\SchemaColumnDefinitionEventArgs;
20 use Doctrine\DBAL\Platforms\AbstractPlatform;
21 use Doctrine\DBAL\Schema\Column;
22 use Doctrine\DBAL\Types\Type;
23 
29 {
37  public function ‪onSchemaColumnDefinition(SchemaColumnDefinitionEventArgs $event)
38  {
39  $tableColumn = $event->getTableColumn();
40  $tableColumn = array_change_key_case($tableColumn, CASE_LOWER);
41 
42  $dbType = $this->‪getDatabaseType($tableColumn['type']);
43  if ($dbType !== 'enum' && $dbType !== 'set') {
44  return;
45  }
46 
48  $tableColumn,
49  $event->getDatabasePlatform()
50  );
51 
52  $event->setColumn($column);
53  $event->preventDefault();
54  }
55 
65  protected function ‪getEnumerationTableColumnDefinition(array $tableColumn, AbstractPlatform $platform): Column
66  {
67  $options = [
68  'length' => $tableColumn['length'] ?? null,
69  'unsigned' => false,
70  'fixed' => false,
71  'default' => $tableColumn['default'] ?? null,
72  'notnull' => ($tableColumn['null'] ?? '') !== 'YES',
73  'scale' => null,
74  'precision' => null,
75  'autoincrement' => false,
76  'comment' => $tableColumn['comment'] ?? null,
77  ];
78 
79  $dbType = $this->‪getDatabaseType($tableColumn['type']);
80  $doctrineType = $platform->getDoctrineTypeMapping($dbType);
81 
82  $column = new Column($tableColumn['field'] ?? null, Type::getType($doctrineType), $options);
83  $column->setPlatformOption('unquotedValues', $this->‪getUnquotedEnumerationValues($tableColumn['type']));
84 
85  return $column;
86  }
87 
94  protected function ‪getDatabaseType(string $typeDefiniton): string
95  {
96  $dbType = strtolower($typeDefiniton);
97  $dbType = strtok($dbType, '(), ');
98 
99  return $dbType;
100  }
101 
106  protected function ‪getUnquotedEnumerationValues(string $typeDefiniton): array
107  {
108  $valuesDefinition = preg_replace('#^(enum|set)\‍((.*)\‍)\s*$#i', '$2', $typeDefiniton);
109  $quoteChar = $valuesDefinition[0];
110  $separator = $quoteChar . ',' . $quoteChar;
111 
112  $valuesDefinition = preg_replace(
113  '#' . $quoteChar . ',\s*' . $quoteChar . '#',
114  $separator,
115  $valuesDefinition
116  );
117 
118  $values = explode($quoteChar . ',' . $quoteChar, substr($valuesDefinition, 1, -1));
119 
120  return array_map(
121  function (string $value) use ($quoteChar) {
122  return str_replace($quoteChar . $quoteChar, $quoteChar, $value);
123  },
124  $values
125  );
126  }
127 }
‪TYPO3\CMS\Core\Database\Schema\EventListener\SchemaColumnDefinitionListener\getEnumerationTableColumnDefinition
‪Doctrine DBAL Schema Column getEnumerationTableColumnDefinition(array $tableColumn, AbstractPlatform $platform)
Definition: SchemaColumnDefinitionListener.php:65
‪TYPO3\CMS\Core\Database\Schema\EventListener\SchemaColumnDefinitionListener\getUnquotedEnumerationValues
‪array getUnquotedEnumerationValues(string $typeDefiniton)
Definition: SchemaColumnDefinitionListener.php:106
‪TYPO3\CMS\Core\Database\Schema\EventListener
Definition: SchemaAlterTableListener.php:4
‪TYPO3\CMS\Core\Database\Schema\EventListener\SchemaColumnDefinitionListener\onSchemaColumnDefinition
‪onSchemaColumnDefinition(SchemaColumnDefinitionEventArgs $event)
Definition: SchemaColumnDefinitionListener.php:37
‪TYPO3\CMS\Core\Database\Schema\EventListener\SchemaColumnDefinitionListener\getDatabaseType
‪string getDatabaseType(string $typeDefiniton)
Definition: SchemaColumnDefinitionListener.php:94
‪TYPO3\CMS\Core\Database\Schema\EventListener\SchemaColumnDefinitionListener
Definition: SchemaColumnDefinitionListener.php:29