1 | <?php |
||
26 | class Blueprint |
||
27 | { |
||
28 | use Macroable, |
||
29 | Tables, |
||
30 | Columns, |
||
31 | Indexes; |
||
32 | |||
33 | /** |
||
34 | * The connection that is used by the blueprint. |
||
35 | * |
||
36 | * @var Connection |
||
37 | */ |
||
38 | protected $connection; |
||
39 | |||
40 | /** |
||
41 | * The grammar that is used by the blueprint. |
||
42 | * |
||
43 | * @var Grammar |
||
44 | */ |
||
45 | protected $grammar; |
||
46 | |||
47 | /** |
||
48 | * The table the blueprint describes. |
||
49 | * |
||
50 | * @var string |
||
51 | */ |
||
52 | protected $table; |
||
53 | |||
54 | /** |
||
55 | * The handler for table manipulation. |
||
56 | */ |
||
57 | protected $collectionHandler; |
||
58 | |||
59 | /** |
||
60 | * The prefix of the table. |
||
61 | * |
||
62 | * @var string |
||
63 | */ |
||
64 | protected $prefix; |
||
65 | |||
66 | /** |
||
67 | * The commands that should be run for the table. |
||
68 | * |
||
69 | * @var Fluent[] |
||
70 | */ |
||
71 | protected $commands = []; |
||
72 | |||
73 | /** |
||
74 | * Catching columns to be able to add fluent indexes. |
||
75 | * |
||
76 | * @var array |
||
77 | */ |
||
78 | protected $columns = []; |
||
79 | |||
80 | /** |
||
81 | * Whether to make the table temporary. |
||
82 | * |
||
83 | * @var bool |
||
84 | */ |
||
85 | public $temporary = false; |
||
86 | |||
87 | /** |
||
88 | * Detect if _key (and thus proxy _id) should autoincrement. |
||
89 | * |
||
90 | * @var bool |
||
91 | */ |
||
92 | protected $autoIncrement = false; |
||
93 | |||
94 | /** |
||
95 | * Create a new schema blueprint. |
||
96 | * |
||
97 | * Blueprint constructor. |
||
98 | * @param string $collection |
||
99 | * @param CollectionHandler $collectionHandler |
||
100 | * @param Closure|null $callback |
||
101 | * @param string $prefix |
||
102 | */ |
||
103 | public function __construct($collection, $collectionHandler, Closure $callback = null, $prefix = '') |
||
115 | |||
116 | /** |
||
117 | * Execute the blueprint against the database. |
||
118 | * |
||
119 | * @param Connection $connection |
||
120 | * @param Grammar $grammar |
||
121 | * @return void |
||
122 | */ |
||
123 | public function build(Connection $connection, Grammar $grammar) |
||
139 | |||
140 | /** |
||
141 | * Generate the compilation method name and call it if method exists in the Grammar object. |
||
142 | * |
||
143 | * @param Fluent $command |
||
144 | * @return mixed |
||
145 | */ |
||
146 | public function compileAqlCommand($command) |
||
153 | |||
154 | /** |
||
155 | * Generate the execution method name and call it if the method exists. |
||
156 | * |
||
157 | * @param $command |
||
158 | */ |
||
159 | public function executeCommand($command) |
||
169 | |||
170 | /** |
||
171 | * Execute an AQL statement. |
||
172 | * |
||
173 | * @param $command |
||
174 | */ |
||
175 | public function executeAqlCommand($command) |
||
179 | |||
180 | public function executeCollectionCommand($command) |
||
192 | |||
193 | /** |
||
194 | * Solely provides feedback to the developer in pretend mode. |
||
195 | * |
||
196 | * @param $command |
||
197 | * @return null |
||
198 | */ |
||
199 | public function executeIgnoreCommand($command) |
||
207 | |||
208 | /** |
||
209 | * Add a new command to the blueprint. |
||
210 | * |
||
211 | * @param string $name |
||
212 | * @param array $parameters |
||
213 | * @return Fluent |
||
214 | */ |
||
215 | protected function addCommand($name, array $parameters = []) |
||
221 | |||
222 | /** |
||
223 | * Create a new Fluent command. |
||
224 | * |
||
225 | * @param string $name |
||
226 | * @param array $parameters |
||
227 | * @return Fluent |
||
228 | */ |
||
229 | protected function createCommand($name, array $parameters = []) |
||
233 | |||
234 | /** |
||
235 | * Get the commands on the blueprint. |
||
236 | * |
||
237 | * @return Fluent[] |
||
238 | */ |
||
239 | public function getCommands() |
||
243 | |||
244 | /** |
||
245 | * Silently catch unsupported schema methods. Store columns for backwards compatible fluent index creation. |
||
246 | * |
||
247 | * @param $method |
||
248 | * @param $args |
||
249 | * @return Blueprint |
||
250 | */ |
||
251 | public function __call($method, $args) |
||
282 | } |
||
283 |
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
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key 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.