|
16 | 16 | use Illuminate\Database\Schema\{
|
17 | 17 | Blueprint, ColumnDefinition
|
18 | 18 | };
|
19 |
| -use Laramore\Database\Schema\Builder; |
20 |
| -use Metas, Types; |
| 19 | +use Laramore\Facades\{ |
| 20 | + Metas, Types |
| 21 | +}; |
21 | 22 |
|
22 | 23 | class BlueprintNode extends MetaNode
|
23 | 24 | {
|
| 25 | + protected $type = 'update'; |
| 26 | + |
24 | 27 | /**
|
25 | 28 | * Create a node based on a blueprint definition.
|
26 | 29 | *
|
27 | 30 | * @param Blueprint $blueprint
|
28 | 31 | */
|
29 | 32 | public function __construct(Blueprint $blueprint)
|
30 | 33 | {
|
31 |
| - $commands = \array_filter($blueprint->getCommands(), function (Fluent $command) { |
32 |
| - return $command->name !== 'create'; |
33 |
| - }); |
34 |
| - $nodes = \array_merge($blueprint->getColumns(), $commands); |
35 |
| - |
36 |
| - $this->type = (\count($commands) === \count($blueprint->getCommands()) ? 'update' : 'create'); |
37 | 34 | $this->tableNames = [$blueprint->getTable()];
|
38 | 35 |
|
39 |
| - $this->setNodes($nodes); |
40 |
| - } |
| 36 | + $constraints = \array_map([$this, 'commandToConstraint'], \array_filter($blueprint->getCommands(), function (Fluent $command) { |
| 37 | + return $command->name !== 'create'; |
| 38 | + })); |
| 39 | + $commands = \array_map([$this, 'columnToCommand'], $blueprint->getColumns()); |
41 | 40 |
|
42 |
| - /** |
43 |
| - * Define the sub nodes/commands. |
44 |
| - * |
45 |
| - * @param array $nodes |
46 |
| - * @return void |
47 |
| - */ |
48 |
| - protected function setNodes(array $nodes) |
49 |
| - { |
50 |
| - $this->nodes = \array_map(function ($node) { |
51 |
| - if ($node instanceof ColumnDefinition) { |
52 |
| - return $this->columnToCommand($node); |
53 |
| - } else if ($node instanceof Fluent) { |
54 |
| - return $this->commandToConstraint($node); |
55 |
| - } |
56 |
| - }, $nodes); |
| 41 | + $this->setNodes(\array_merge($commands, $constraints)); |
57 | 42 | }
|
58 |
| - |
| 43 | + |
59 | 44 | /**
|
60 | 45 | * This method is called when the node is asked to be optimized.
|
61 | 46 | * Only optimize if a meta exists for this table.
|
@@ -108,25 +93,25 @@ protected function popFromColumn(Fluent $column, string $name)
|
108 | 93 | /**
|
109 | 94 | * Pop the type definition from a column definition.
|
110 | 95 | *
|
111 |
| - * @param ColumnDefinition $column |
| 96 | + * @param ColumnDefinition|Fluent $column |
112 | 97 | * @return string
|
113 | 98 | */
|
114 |
| - protected function popTypeFromColumn(ColumnDefinition $column): string |
| 99 | + protected function popTypeFromColumn(Fluent $column): string |
115 | 100 | {
|
116 | 101 | $type = $this->popFromColumn($column, 'type');
|
117 | 102 |
|
118 | 103 | // Here, if our field is an integer, we need to handle unsigned and increment integers.
|
119 |
| - if ($type === Types::integer()->getMigrationType()) { |
| 104 | + if ($type === Types::get('integer')->getMigrationType()) { |
120 | 105 | if ($column->unsigned) {
|
121 | 106 | unset($column->unsigned);
|
122 | 107 |
|
123 |
| - $type = Types::unsignedInteger()->getMigrationType(); |
| 108 | + $type = Types::get('unsigned_integer')->getMigrationType(); |
124 | 109 | }
|
125 | 110 |
|
126 | 111 | if ($column->autoIncrement) {
|
127 | 112 | unset($column->autoIncrement);
|
128 | 113 |
|
129 |
| - $type = Types::increment()->getMigrationType(); |
| 114 | + $type = Types::get('increment')->getMigrationType(); |
130 | 115 | }
|
131 | 116 | }
|
132 | 117 |
|
@@ -156,10 +141,10 @@ protected function getNeedsForCommand(Fluent $command): array
|
156 | 141 | /**
|
157 | 142 | * Transform a column definition to a migration command.
|
158 | 143 | *
|
159 |
| - * @param ColumnDefinition $column |
| 144 | + * @param ColumnDefinition|Fluent $column |
160 | 145 | * @return Command
|
161 | 146 | */
|
162 |
| - public function columnToCommand(ColumnDefinition $column): Command |
| 147 | + public function columnToCommand(Fluent $column): Command |
163 | 148 | {
|
164 | 149 | $this->cleanUnrelevantAttributes($column);
|
165 | 150 |
|
|
0 commit comments