|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LaravelFreelancerNL\Aranguent\Schema\Concerns; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Fluent; |
|
6
|
|
|
|
|
7
|
|
|
trait Tables |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Indicate that the table needs to be created. |
|
11
|
|
|
* |
|
12
|
|
|
* @param array $options |
|
13
|
|
|
* @return Fluent |
|
14
|
|
|
*/ |
|
15
|
|
|
public function create($options = []) |
|
16
|
|
|
{ |
|
17
|
|
|
$parameters['options'] = $options; |
|
|
|
|
|
|
18
|
|
|
$parameters['explanation'] = "Create '{$this->table}' table."; |
|
|
|
|
|
|
19
|
|
|
$parameters['handler'] = 'table'; |
|
20
|
|
|
|
|
21
|
|
|
return $this->addCommand('create', $parameters); |
|
|
|
|
|
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Determine if the blueprint has a create command. |
|
26
|
|
|
* |
|
27
|
|
|
* @return bool |
|
28
|
|
|
*/ |
|
29
|
|
|
protected function creating() |
|
30
|
|
|
{ |
|
31
|
|
|
return collect($this->commands)->contains(function ($command) { |
|
|
|
|
|
|
32
|
|
|
return $command->name === 'create'; |
|
33
|
|
|
}); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function executeCreateCommand($command) |
|
37
|
|
|
{ |
|
38
|
|
|
if ($this->connection->pretending()) { |
|
39
|
|
|
$this->connection->logQuery('/* '.$command->explanation." */\n", []); |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
return; |
|
42
|
|
|
} |
|
43
|
|
|
$options = $command->options; |
|
44
|
|
|
if ($this->temporary === true) { |
|
|
|
|
|
|
45
|
|
|
$options['isVolatile'] = true; |
|
46
|
|
|
} |
|
47
|
|
|
if ($this->autoIncrement === true) { |
|
|
|
|
|
|
48
|
|
|
$options['keyOptions']['autoincrement'] = true; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$collections = $this->collectionHandler->getAllCollections(['excludeSystem' => true]); |
|
|
|
|
|
|
52
|
|
|
if (! isset($collections[$this->table])) { |
|
53
|
|
|
$this->collectionHandler->create($this->table, $options); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Alias for getCollection. |
|
59
|
|
|
* |
|
60
|
|
|
* @return string |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getTable() |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->table; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Rename the table to a given name. |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $to |
|
72
|
|
|
* @return Fluent |
|
73
|
|
|
*/ |
|
74
|
|
|
public function rename($to) |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->addCommand('rename', compact('to')); |
|
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Indicate that the table should be dropped. |
|
82
|
|
|
* |
|
83
|
|
|
* @return Fluent |
|
84
|
|
|
*/ |
|
85
|
|
|
public function drop() |
|
86
|
|
|
{ |
|
87
|
|
|
$parameters['explanation'] = "Drop the '{$this->table}' table."; |
|
|
|
|
|
|
88
|
|
|
$parameters['handler'] = 'table'; |
|
89
|
|
|
|
|
90
|
|
|
return $this->addCommand('drop', $parameters); |
|
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Indicate that the table should be dropped if it exists. |
|
95
|
|
|
* |
|
96
|
|
|
* @return Fluent |
|
97
|
|
|
*/ |
|
98
|
|
|
public function dropIfExists() |
|
99
|
|
|
{ |
|
100
|
|
|
$parameters['explanation'] = "Drop the '{$this->table}' table."; |
|
|
|
|
|
|
101
|
|
|
$parameters['handler'] = 'table'; |
|
102
|
|
|
|
|
103
|
|
|
return $this->addCommand('dropIfExists'); |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
} |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.