1 | <?php |
||
2 | |||
3 | /** |
||
4 | * Database query builder core, named Blueprint |
||
5 | * |
||
6 | * PHP version 7.4 |
||
7 | * |
||
8 | * @category Database |
||
9 | * @package Chaospelt\Kernel\Database |
||
10 | * |
||
11 | * @author Stf Kolev <[email protected]> |
||
12 | * @license BSD-3-Clause https://opensource.org/licenses/BSD-3-Clause |
||
13 | * |
||
14 | * @link https://github.com/stfkolev/chaospelt |
||
15 | */ |
||
16 | |||
17 | namespace Chaospelt\Kernel\Database; |
||
18 | |||
19 | use Chaospelt\Kernel\Support\Vivid; |
||
20 | use Chaospelt\Kernel\Database\ColumnDefinition; |
||
21 | |||
22 | /** |
||
23 | * Database query builder core, named Blueprint |
||
24 | * |
||
25 | * @category Database |
||
26 | * @package Chaospelt\Kernel\Database |
||
27 | * |
||
28 | * @author Stf Kolev <[email protected]> |
||
29 | * @license BSD-3-Clause https://opensource.org/licenses/BSD-3-Clause |
||
30 | * |
||
31 | * @link https://github.com/stfkolev/chaospelt |
||
32 | */ |
||
33 | class Blueprint |
||
34 | { |
||
35 | /** |
||
36 | * The table the blueprint describes. |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $table; |
||
41 | |||
42 | /** |
||
43 | * The prefix of the table. |
||
44 | * |
||
45 | * @var string |
||
46 | */ |
||
47 | protected $prefix; |
||
48 | |||
49 | /** |
||
50 | * The columns that should be added to the table. |
||
51 | * |
||
52 | * @var \Chaospelt\Kernel\Database\ColumnDefinition[] |
||
53 | */ |
||
54 | protected $columns = []; |
||
55 | |||
56 | /** |
||
57 | * The commands that should be run for the table. |
||
58 | * |
||
59 | * @var \Chaospelt\Kernel\Support\Fluent[] |
||
60 | */ |
||
61 | protected $commands = []; |
||
62 | |||
63 | /** |
||
64 | * The storage engine that should be used for the table. |
||
65 | * |
||
66 | * @var string |
||
67 | */ |
||
68 | public $engine; |
||
69 | |||
70 | /** |
||
71 | * The default character set that should be used for the table. |
||
72 | * |
||
73 | * @var string |
||
74 | */ |
||
75 | public $charset; |
||
76 | |||
77 | /** |
||
78 | * The collation that should be used for the table. |
||
79 | * |
||
80 | * @var string |
||
81 | */ |
||
82 | public $collation; |
||
83 | |||
84 | /** |
||
85 | * Whether to make the table temporary. |
||
86 | * |
||
87 | * @var bool |
||
88 | */ |
||
89 | public $temporary = false; |
||
90 | |||
91 | /** |
||
92 | * The column to add new columns after. |
||
93 | * |
||
94 | * @var string |
||
95 | */ |
||
96 | public $after; |
||
97 | |||
98 | /** |
||
99 | * Create a new schema blueprint. |
||
100 | * |
||
101 | * @param string $table |
||
102 | * @param \Closure|null $callback |
||
103 | * @param string $prefix |
||
104 | * |
||
105 | * @return void |
||
106 | */ |
||
107 | public function __construct($table, Closure $callback = null, $prefix = '') |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
108 | { |
||
109 | $this->table = $table; |
||
110 | $this->prefix = $prefix; |
||
111 | |||
112 | if (! is_null($callback)) { |
||
113 | $callback($this); |
||
114 | } |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Create a new string column on the table. |
||
119 | * |
||
120 | * @param string $column |
||
121 | * @param int|null $length |
||
122 | * |
||
123 | * @return \Chaospelt\Kernel\Database\ColumnDefinition |
||
124 | */ |
||
125 | public function string($column, $length = null) |
||
126 | { |
||
127 | $length = $length ?: \Chaospelt\Helpers\Schema::$defaultStringLength; |
||
128 | |||
129 | return $this->addColumn('string', $column, compact('length')); |
||
130 | } |
||
131 | |||
132 | |||
133 | /** |
||
134 | * Add a new column to the blueprint. |
||
135 | * |
||
136 | * @param string $type Column Type |
||
137 | * @param string $name Column Name |
||
138 | * @param array $parameters Column Parameters List |
||
139 | * |
||
140 | * @return \Chaospelt\Kernel\Database\ColumnDefinition |
||
141 | */ |
||
142 | public function addColumn($type, $name, array $parameters = []) |
||
143 | { |
||
144 | return $this->addColumnDefinition( |
||
145 | new ColumnDefinition( |
||
146 | array_merge(compact('type', 'name'), $parameters) |
||
147 | ) |
||
148 | ); |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Add a new column definition to the blueprint. |
||
153 | * |
||
154 | * @param \Chaospelt\Kernel\Database\ColumnDefinition $definition |
||
155 | * |
||
156 | * @return \Chaospelt\Kernel\Database\ColumnDefinition |
||
157 | */ |
||
158 | protected function addColumnDefinition($definition) |
||
159 | { |
||
160 | $this->columns[] = $definition; |
||
161 | |||
162 | if ($this->after) { |
||
163 | $definition->after($this->after); |
||
164 | |||
165 | $this->after = $definition->name; |
||
0 ignored issues
–
show
The property
name does not exist on Chaospelt\Kernel\Database\ColumnDefinition . Since you implemented __get , consider adding a @property annotation.
![]() |
|||
166 | } |
||
167 | |||
168 | return $definition; |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * Get the columns on the blueprint. |
||
173 | * |
||
174 | * @return \Chaospelt\Kernel\Database\ColumnDefinition[] |
||
175 | */ |
||
176 | public function getColumns() |
||
177 | { |
||
178 | return $this->columns; |
||
179 | } |
||
180 | } |