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