-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathDcaSchemaProvider.php
495 lines (410 loc) · 14.4 KB
/
DcaSchemaProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
<?php
declare(strict_types=1);
/*
* This file is part of Contao.
*
* Copyright (c) 2005-2018 Leo Feyer
*
* @license LGPL-3.0+
*/
namespace Contao\CoreBundle\Doctrine\Schema;
use Contao\CoreBundle\Framework\ContaoFrameworkInterface;
use Contao\Database\Installer;
use Doctrine\Bundle\DoctrineBundle\Registry;
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\SchemaConfig;
use Doctrine\DBAL\Schema\Table;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Tools\SchemaTool;
class DcaSchemaProvider
{
/**
* @var ContaoFrameworkInterface
*/
private $framework;
/**
* @var Registry
*/
private $doctrine;
/**
* @param ContaoFrameworkInterface $framework
* @param Registry $doctrine
*/
public function __construct(ContaoFrameworkInterface $framework, Registry $doctrine)
{
$this->framework = $framework;
$this->doctrine = $doctrine;
}
/**
* Creates a schema.
*
* @return Schema
*/
public function createSchema(): Schema
{
if (0 !== \count($this->doctrine->getManagerNames())) {
return $this->createSchemaFromOrm();
}
return $this->createSchemaFromDca();
}
/**
* Adds the DCA data to the Doctrine schema.
*
* @param Schema $schema
*/
public function appendToSchema(Schema $schema): void
{
$config = $this->getSqlDefinitions();
foreach ($config as $tableName => $definitions) {
$table = $schema->createTable($tableName);
if (isset($definitions['SCHEMA_FIELDS'])) {
foreach ($definitions['SCHEMA_FIELDS'] as $fieldName => $config) {
$options = $config;
unset($options['name'], $options['type']);
// Use the binary collation if the "case_sensitive" option is set
if ($this->isCaseSensitive($config)) {
$options['platformOptions']['collation'] = $this->getBinaryCollation($table);
}
$table->addColumn($config['name'], $config['type'], $options);
}
}
if (isset($definitions['TABLE_FIELDS'])) {
foreach ($definitions['TABLE_FIELDS'] as $fieldName => $sql) {
$this->parseColumnSql($table, $fieldName, substr($sql, \strlen($fieldName) + 3));
}
}
if (isset($definitions['TABLE_CREATE_DEFINITIONS'])) {
foreach ($definitions['TABLE_CREATE_DEFINITIONS'] as $keyName => $sql) {
$this->parseIndexSql($table, $keyName, strtolower($sql));
}
}
if (isset($definitions['TABLE_OPTIONS'])) {
if (preg_match('/ENGINE=([^ ]+)/i', $definitions['TABLE_OPTIONS'], $match)) {
$table->addOption('engine', $match[1]);
}
if (preg_match('/DEFAULT CHARSET=([^ ]+)/i', $definitions['TABLE_OPTIONS'], $match)) {
$table->addOption('charset', $match[1]);
$table->addOption('collate', $match[1].'_general_ci');
}
if (preg_match('/COLLATE ([^ ]+)/i', $definitions['TABLE_OPTIONS'], $match)) {
$table->addOption('collate', $match[1]);
}
}
// The default InnoDB row format before MySQL 5.7.9 is "Compact" but innodb_large_prefix requires "DYNAMIC"
if ($table->hasOption('engine') && 'InnoDB' === $table->getOption('engine')) {
$table->addOption('row_format', 'DYNAMIC');
}
}
}
/**
* Creates a Schema instance from Doctrine ORM metadata.
*
* @return Schema
*/
private function createSchemaFromOrm(): Schema
{
/** @var EntityManagerInterface $manager */
$manager = $this->doctrine->getManager();
/** @var ClassMetadata[] $metadata */
$metadata = $manager->getMetadataFactory()->getAllMetadata();
// Apply the schema filter
if ($filter = $this->doctrine->getConnection()->getConfiguration()->getFilterSchemaAssetsExpression()) {
foreach ($metadata as $key => $data) {
if (!preg_match($filter, $data->getTableName())) {
unset($metadata[$key]);
}
}
}
if (empty($metadata)) {
return $this->createSchemaFromDca();
}
$tool = new SchemaTool($manager);
return $tool->getSchemaFromMetadata($metadata);
}
/**
* Creates a Schema instance and adds DCA metadata.
*
* @return Schema
*/
private function createSchemaFromDca(): Schema
{
$config = new SchemaConfig();
$params = $this->doctrine->getConnection()->getParams();
if (isset($params['defaultTableOptions'])) {
$config->setDefaultTableOptions($params['defaultTableOptions']);
}
$schema = new Schema([], [], $config);
$this->appendToSchema($schema);
return $schema;
}
/**
* Parses the column definition and adds it to the schema table.
*
* @param Table $table
* @param string $columnName
* @param string $sql
*/
private function parseColumnSql(Table $table, string $columnName, string $sql): void
{
[$dbType, $def] = explode(' ', $sql, 2);
$type = strtok(strtolower($dbType), '(), ');
$length = (int) strtok('(), ');
$fixed = false;
$scale = null;
$precision = null;
$default = null;
$collation = null;
$this->setLengthAndPrecisionByType($type, $dbType, $length, $scale, $precision, $fixed);
$connection = $this->doctrine->getConnection();
$type = $connection->getDatabasePlatform()->getDoctrineTypeMapping($type);
if (0 === $length) {
$length = null;
}
if (preg_match('/default (\'[^\']*\'|\d+)/i', $def, $match)) {
$default = trim($match[1], "'");
}
if (preg_match('/collate ([^ ]+)/i', $def, $match)) {
$collation = $match[1];
}
// Use the binary collation if the BINARY flag is set (see #1286)
if (0 === strncasecmp($def, 'binary ', 7)) {
$collation = $this->getBinaryCollation($table);
}
$options = [
'length' => $length,
'unsigned' => false !== stripos($def, 'unsigned'),
'fixed' => $fixed,
'default' => $default,
'notnull' => false !== stripos($def, 'not null'),
'scale' => null,
'precision' => null,
'autoincrement' => false !== stripos($def, 'auto_increment'),
'comment' => null,
];
if (null !== $scale && null !== $precision) {
$options['scale'] = $scale;
$options['precision'] = $precision;
}
if (null !== $collation) {
$options['platformOptions'] = ['collation' => $collation];
}
$table->addColumn($columnName, $type, $options);
}
/**
* Sets the length, scale, precision and fixed values by field type.
*
* @param string $type
* @param string $dbType
* @param int|null $length
* @param int|null $scale
* @param int|null $precision
* @param bool $fixed
*/
private function setLengthAndPrecisionByType(string $type, string $dbType, ?int &$length, ?int &$scale, ?int &$precision, bool &$fixed): void
{
switch ($type) {
case 'char':
case 'binary':
$fixed = true;
break;
case 'float':
case 'double':
case 'real':
case 'numeric':
case 'decimal':
if (preg_match('/[A-Za-z]+\((\d+)\,(\d+)\)/', $dbType, $match)) {
$length = null;
$precision = $match[1];
$scale = $match[2];
}
break;
case 'tinytext':
$length = MySqlPlatform::LENGTH_LIMIT_TINYTEXT;
break;
case 'text':
$length = MySqlPlatform::LENGTH_LIMIT_TEXT;
break;
case 'mediumtext':
$length = MySqlPlatform::LENGTH_LIMIT_MEDIUMTEXT;
break;
case 'tinyblob':
$length = MySqlPlatform::LENGTH_LIMIT_TINYBLOB;
break;
case 'blob':
$length = MySqlPlatform::LENGTH_LIMIT_BLOB;
break;
case 'mediumblob':
$length = MySqlPlatform::LENGTH_LIMIT_MEDIUMBLOB;
break;
case 'tinyint':
case 'smallint':
case 'mediumint':
case 'int':
case 'integer':
case 'bigint':
case 'year':
$length = null;
break;
}
}
/**
* Parses the index definition and adds it to the schema table.
*
* @param Table $table
* @param string $keyName
* @param string $sql
*/
private function parseIndexSql(Table $table, string $keyName, string $sql): void
{
if ('PRIMARY' === $keyName) {
if (!preg_match_all('/`([^`]+)`/', $sql, $matches)) {
throw new \RuntimeException(sprintf('Primary key definition "%s" could not be parsed.', $sql));
}
$table->setPrimaryKey($matches[1]);
return;
}
if (!preg_match('/(.*) `([^`]+)` \((.*)\)/', $sql, $matches)) {
throw new \RuntimeException(sprintf('Key definition "%s" could not be parsed.', $sql));
}
$columns = [];
$flags = [];
foreach (explode(',', $matches[3]) as $column) {
preg_match('/`([^`]+)`(\((\d+)\))?/', $column, $cm);
$column = $cm[1];
if (isset($cm[3])) {
$maxlen = $this->getMaximumIndexLength($table, $column);
if ($cm[3] > $maxlen) {
$cm[3] = $maxlen;
}
$column .= '('.$cm[3].')';
}
$columns[$cm[1]] = $column;
}
if (false !== strpos($matches[1], 'unique')) {
$table->addUniqueIndex($columns, $matches[2]);
} else {
if (false !== strpos($matches[1], 'fulltext')) {
$flags[] = 'fulltext';
}
$table->addIndex($columns, $matches[2], $flags);
}
}
/**
* Returns the SQL definitions from the Contao installer.
*
* @return array
*/
private function getSqlDefinitions(): array
{
$this->framework->initialize();
/** @var Installer $installer */
$installer = $this->framework->createInstance(Installer::class);
$sqlTarget = $installer->getFromDca();
$sqlLegacy = $installer->getFromFile();
// Manually merge the legacy definitions (see #4766)
if (!empty($sqlLegacy)) {
foreach ($sqlLegacy as $table => $categories) {
foreach ($categories as $category => $fields) {
if (\is_array($fields)) {
foreach ($fields as $name => $sql) {
$sqlTarget[$table][$category][$name] = $sql;
}
} else {
$sqlTarget[$table][$category] = $fields;
}
}
}
}
// Apply the schema filter (see contao/installation-bundle#78)
if ($filter = $this->doctrine->getConnection()->getConfiguration()->getFilterSchemaAssetsExpression()) {
foreach (array_keys($sqlTarget) as $key) {
if (!preg_match($filter, $key)) {
unset($sqlTarget[$key]);
}
}
}
return $sqlTarget;
}
/**
* Returns the maximum index length of a column depending on the collation.
*
* @param Table $table
* @param string $column
*
* @return int
*/
private function getMaximumIndexLength(Table $table, string $column): int
{
$indexLength = $this->getDefaultIndexLength($table);
$collation = $table->getOption('collate');
$connection = $this->doctrine->getConnection();
// Read the table collation if the table exists
if ($connection->getSchemaManager()->tablesExist([$table->getName()])) {
$columnOptions = $connection
->query(sprintf("SHOW FULL COLUMNS FROM %s LIKE '%s'", $table->getName(), $column))
->fetch(\PDO::FETCH_OBJ)
;
$collation = $columnOptions->Collation;
}
if (0 === strncmp($collation, 'utf8mb4', 7)) {
return (int) floor($indexLength / 4);
}
return (int) floor($indexLength / 3);
}
/**
* Returns the default index length of a table.
*
* @param Table $table
*
* @return int
*/
private function getDefaultIndexLength(Table $table): int
{
$connection = $this->doctrine->getConnection();
$tableOptions = $connection
->query(sprintf("SHOW TABLE STATUS LIKE '%s'", $table->getName()))
->fetch(\PDO::FETCH_OBJ)
;
if ('InnoDB' !== $tableOptions->Engine) {
return 1000;
}
$largePrefix = $connection
->query("SHOW VARIABLES LIKE 'innodb_large_prefix'")
->fetch(\PDO::FETCH_OBJ)
;
if (\in_array(strtolower((string) $largePrefix->Value), ['1', 'on'], true)) {
return 3072;
}
return 767;
}
/**
* Checks if a field has the case-sensitive flag.
*
* @param array $config
*
* @return bool
*/
private function isCaseSensitive(array $config): bool
{
if (!isset($config['customSchemaOptions']['case_sensitive'])) {
return false;
}
return true === $config['customSchemaOptions']['case_sensitive'];
}
/**
* Returns the binary collation depending on the charset.
*
* @param Table $table
*
* @return string|null
*/
private function getBinaryCollation(Table $table): ?string
{
if (!$table->hasOption('charset')) {
return null;
}
return $table->getOption('charset').'_bin';
}
}