1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MadWeb\Initializer; |
4
|
|
|
|
5
|
|
|
use MadWeb\Initializer\Contracts\Runner; |
6
|
|
|
|
7
|
|
|
class Run implements Runner |
8
|
|
|
{ |
9
|
|
|
protected $commands = []; |
10
|
|
|
|
11
|
|
|
public function artisan(string $command, array $arguments = []): Runner |
12
|
|
|
{ |
13
|
|
|
$this->pushCommand(__FUNCTION__, $command, $arguments); |
14
|
|
|
|
15
|
|
|
return $this; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public function external(string $command, ...$arguments): Runner |
19
|
|
|
{ |
20
|
|
|
$this->pushCommand(__FUNCTION__, $command, $arguments); |
21
|
|
|
|
22
|
|
|
return $this; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function callable(callable $function, ...$arguments): Runner |
26
|
|
|
{ |
27
|
|
|
$this->pushCommand(__FUNCTION__, $function, $arguments); |
28
|
|
|
|
29
|
|
|
return $this; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function dispatch($job): Runner |
33
|
|
|
{ |
34
|
|
|
$this->pushCommand(__FUNCTION__, $job); |
35
|
|
|
|
36
|
|
|
return $this; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function dispatchNow($job): Runner |
40
|
|
|
{ |
41
|
|
|
$this->pushCommand(__FUNCTION__, $job); |
42
|
|
|
|
43
|
|
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function publish(array $providers): Runner |
47
|
|
|
{ |
48
|
|
|
foreach ($providers as $provider => $tag) { |
49
|
|
|
$arguments['--provider'] = is_numeric($provider) ? $tag : $provider; |
|
|
|
|
50
|
|
|
|
51
|
|
|
if (! is_numeric($provider) and is_string($tag)) { |
|
|
|
|
52
|
|
|
$arguments['--tag'] = $tag; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$this->artisan('vendor:publish', $arguments); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
protected function pushCommand(string $type, $command, array $arguments = []) |
62
|
|
|
{ |
63
|
|
|
$this->commands[] = compact('type', 'command', 'arguments'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function getCommands(): array |
67
|
|
|
{ |
68
|
|
|
return $this->commands; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
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.