TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
SchemaColumnDefinitionListener.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types=1);
3 
4 namespace TYPO3\CMS\Core\Database\Schema\EventListener;
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 
47  $column = $this->getEnumerationTableColumnDefinition(
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' => (bool)($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'], 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 }