Skip to content

Commit 77ce281

Browse files
authored
Merge pull request #6662 from doctrine/4.3.x
Merge 4.3.x up into 5.0.x
2 parents 9c97edd + 03049bc commit 77ce281

File tree

9 files changed

+219
-25
lines changed

9 files changed

+219
-25
lines changed

UPGRADE.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ all drivers and middleware.
7272

7373
# Upgrade to 4.3
7474

75+
## `Table::__construct()` marked as internal
76+
77+
The `Table::__construct()` method has been marked as internal. Use `Table::editor()` to instantiate an editor and
78+
`TableEditor::create()` to create a table.
79+
7580
## Deprecated `AbstractAsset::getShortestName()`
7681

7782
The `AbstractAsset::getShortestName()` method has been deprecated. Use `AbstractAsset::getName()` instead.

src/Schema/AbstractSchemaManager.php

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -210,15 +210,26 @@ public function listTables(): array
210210
continue;
211211
}
212212

213-
$tables[] = new Table(
214-
$tableName,
215-
$this->_getPortableTableColumnList($tableName, $database, $tableColumns),
216-
$this->_getPortableTableIndexesList($indexColumnsByTable[$tableName] ?? [], $tableName),
217-
[],
218-
$this->_getPortableTableForeignKeysList($foreignKeyColumnsByTable[$tableName] ?? []),
219-
$tableOptionsByTable[$tableName] ?? [],
220-
$configuration,
221-
);
213+
$editor = Table::editor()
214+
->setName($tableName)
215+
->setColumns($this->_getPortableTableColumnList($tableName, $database, $tableColumns))
216+
->setIndexes(
217+
$this->_getPortableTableIndexesList($indexColumnsByTable[$tableName] ?? [], $tableName),
218+
);
219+
220+
if (isset($foreignKeyColumnsByTable[$tableName])) {
221+
$editor->setForeignKeyConstraints(
222+
$this->_getPortableTableForeignKeysList($foreignKeyColumnsByTable[$tableName]),
223+
);
224+
}
225+
226+
if (isset($tableOptionsByTable[$tableName])) {
227+
$editor->setOptions($tableOptionsByTable[$tableName]);
228+
}
229+
230+
$tables[] = $editor
231+
->setConfiguration($configuration)
232+
->create();
222233
}
223234

224235
return $tables;
@@ -328,14 +339,13 @@ public function introspectTable(string $name): Table
328339
throw TableDoesNotExist::new($name);
329340
}
330341

331-
return new Table(
332-
$name,
333-
$columns,
334-
$this->listTableIndexes($name),
335-
[],
336-
$this->listTableForeignKeys($name),
337-
$this->getTableOptions($name),
338-
);
342+
return Table::editor()
343+
->setName($name)
344+
->setColumns($columns)
345+
->setIndexes($this->listTableIndexes($name))
346+
->setForeignKeyConstraints($this->listTableForeignKeys($name))
347+
->setOptions($this->getTableOptions($name))
348+
->create();
339349
}
340350

341351
/**
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\DBAL\Schema\Exception;
6+
7+
use Doctrine\DBAL\Schema\SchemaException;
8+
use LogicException;
9+
10+
/** @psalm-immutable */
11+
final class InvalidTableDefinition extends LogicException implements SchemaException
12+
{
13+
public static function nameNotSet(): self
14+
{
15+
return new self('Table name is not set.');
16+
}
17+
}

src/Schema/Name.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ interface Name
1212
/**
1313
* Returns the string representation of the name.
1414
*
15-
* The consumers of this method should not rely on a specific return value. It should be used only for diagnostic
16-
* purposes.
15+
* If passed to the corresponding parser, the name should be parsed back to an equivalent object.
1716
*/
1817
public function toString(): string;
1918
}

src/Schema/Schema.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -282,12 +282,13 @@ public function createNamespace(string $name): self
282282
*/
283283
public function createTable(string $name): Table
284284
{
285-
$table = new Table($name, [], [], [], [], [], $this->_schemaConfig->toTableConfiguration());
286-
$this->_addTable($table);
285+
$table = Table::editor()
286+
->setName($name)
287+
->setOptions($this->_schemaConfig->getDefaultTableOptions())
288+
->setConfiguration($this->_schemaConfig->toTableConfiguration())
289+
->create();
287290

288-
foreach ($this->_schemaConfig->getDefaultTableOptions() as $option => $value) {
289-
$table->addOption($option, $value);
290-
}
291+
$this->_addTable($table);
291292

292293
return $table;
293294
}

src/Schema/Table.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ class Table extends AbstractNamedObject
6969
private readonly int $maxIdentifierLength;
7070

7171
/**
72+
* @internal Use {@link Table::editor()} to instantiate an editor and {@link TableEditor::create()}
73+
* to create a table.
74+
*
7275
* @param array<Column> $columns
7376
* @param array<Index> $indexes
7477
* @param array<UniqueConstraint> $uniqueConstraints
@@ -788,6 +791,31 @@ public function getComment(): ?string
788791
return $this->_options['comment'] ?? null;
789792
}
790793

794+
/**
795+
* Instantiates a new table editor.
796+
*/
797+
public static function editor(): TableEditor
798+
{
799+
return new TableEditor();
800+
}
801+
802+
/**
803+
* Instantiates a new table editor and initializes it with the table's properties.
804+
*/
805+
public function edit(): TableEditor
806+
{
807+
return self::editor()
808+
->setName($this->getObjectName()->toString())
809+
->setColumns($this->_columns)
810+
->setIndexes($this->_indexes)
811+
->setUniqueConstraints($this->uniqueConstraints)
812+
->setForeignKeyConstraints($this->_fkConstraints)
813+
->setOptions($this->_options)
814+
->setConfiguration(
815+
new TableConfiguration($this->maxIdentifierLength),
816+
);
817+
}
818+
791819
/**
792820
* @param array<string|int, string> $columns
793821
* @param array<int, string> $flags

src/Schema/TableConfiguration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/**
88
* Contains platform-specific parameters used for creating and managing objects scoped to a {@see Table}.
99
*/
10-
class TableConfiguration
10+
final class TableConfiguration
1111
{
1212
/** @internal The configuration can be only instantiated by a {@see SchemaConfig}. */
1313
public function __construct(private readonly int $maxIdentifierLength)

src/Schema/TableEditor.php

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\DBAL\Schema;
6+
7+
use Doctrine\DBAL\Schema\Exception\InvalidTableDefinition;
8+
9+
final class TableEditor
10+
{
11+
private ?string $name = null;
12+
13+
/** @var array<Column> */
14+
private array $columns = [];
15+
16+
/** @var array<Index> */
17+
private array $indexes = [];
18+
19+
/** @var array<UniqueConstraint> */
20+
private array $uniqueConstraints = [];
21+
22+
/** @var array<ForeignKeyConstraint> */
23+
private array $foreignKeyConstraints = [];
24+
25+
/** @var array<string, mixed> */
26+
private array $options = [];
27+
28+
private ?TableConfiguration $configuration = null;
29+
30+
/** @internal Use {@link Table::editor()} or {@link Table::edit()} to create an instance */
31+
public function __construct()
32+
{
33+
}
34+
35+
public function setName(string $name): self
36+
{
37+
$this->name = $name;
38+
39+
return $this;
40+
}
41+
42+
/** @param array<Column> $columns */
43+
public function setColumns(array $columns): self
44+
{
45+
$this->columns = $columns;
46+
47+
return $this;
48+
}
49+
50+
/** @param array<Index> $indexes */
51+
public function setIndexes(array $indexes): self
52+
{
53+
$this->indexes = $indexes;
54+
55+
return $this;
56+
}
57+
58+
/** @param array<UniqueConstraint> $uniqueConstraints */
59+
public function setUniqueConstraints(array $uniqueConstraints): self
60+
{
61+
$this->uniqueConstraints = $uniqueConstraints;
62+
63+
return $this;
64+
}
65+
66+
/** @param array<ForeignKeyConstraint> $foreignKeyConstraints */
67+
public function setForeignKeyConstraints(array $foreignKeyConstraints): self
68+
{
69+
$this->foreignKeyConstraints = $foreignKeyConstraints;
70+
71+
return $this;
72+
}
73+
74+
/** @param array<string, mixed> $options */
75+
public function setOptions(array $options): self
76+
{
77+
$this->options = $options;
78+
79+
return $this;
80+
}
81+
82+
public function setConfiguration(TableConfiguration $configuration): self
83+
{
84+
$this->configuration = $configuration;
85+
86+
return $this;
87+
}
88+
89+
public function create(): Table
90+
{
91+
if ($this->name === null) {
92+
throw InvalidTableDefinition::nameNotSet();
93+
}
94+
95+
return new Table(
96+
$this->name,
97+
$this->columns,
98+
$this->indexes,
99+
$this->uniqueConstraints,
100+
$this->foreignKeyConstraints,
101+
$this->options,
102+
$this->configuration,
103+
);
104+
}
105+
}

tests/Schema/TableEditorTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\DBAL\Tests\Schema;
6+
7+
use Doctrine\DBAL\Schema\Exception\InvalidTableDefinition;
8+
use Doctrine\DBAL\Schema\Table;
9+
use PHPUnit\Framework\TestCase;
10+
11+
class TableEditorTest extends TestCase
12+
{
13+
public function testSetName(): void
14+
{
15+
$table = (new Table('accounts'))
16+
->edit()
17+
->setName('contacts')
18+
->create();
19+
20+
self::assertSame('contacts', $table->getName());
21+
}
22+
23+
public function testNameNotSet(): void
24+
{
25+
$this->expectException(InvalidTableDefinition::class);
26+
27+
Table::editor()->create();
28+
}
29+
}

0 commit comments

Comments
 (0)