|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MadWeb\Initializer\ExecutorActions; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
use Illuminate\Console\Command; |
|
7
|
|
|
|
|
8
|
|
|
class Publish |
|
9
|
|
|
{ |
|
10
|
|
|
private const COMMAND = 'vendor:publish'; |
|
11
|
|
|
|
|
12
|
|
|
private $artisanCommand; |
|
13
|
|
|
|
|
14
|
|
|
/** @var string|array */ |
|
15
|
|
|
private $providers; |
|
16
|
|
|
|
|
17
|
|
|
/** @var bool */ |
|
18
|
|
|
private $force; |
|
19
|
|
|
|
|
20
|
|
|
/** @var array */ |
|
21
|
|
|
private $arguments = []; |
|
22
|
|
|
|
|
23
|
66 |
|
public function __construct(Command $artisanCommand, $providers, bool $force = false) |
|
24
|
|
|
{ |
|
25
|
66 |
|
$this->artisanCommand = $artisanCommand; |
|
26
|
66 |
|
$this->providers = $providers; |
|
27
|
66 |
|
$this->force = $force; |
|
28
|
66 |
|
} |
|
29
|
|
|
|
|
30
|
66 |
|
public function __invoke() |
|
31
|
|
|
{ |
|
32
|
66 |
|
if (is_string($this->providers)) { |
|
33
|
12 |
|
$this->addProvider($this->providers); |
|
34
|
54 |
|
} elseif (is_array($this->providers)) { |
|
35
|
48 |
|
$this->handleArray(); |
|
36
|
|
|
} else { |
|
37
|
6 |
|
throw new InvalidArgumentException('Invalid publishable argument.'); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
60 |
|
foreach ($this->arguments as $argument) { |
|
41
|
60 |
|
new Artisan($this->artisanCommand, self::COMMAND, $argument); |
|
42
|
60 |
|
value(new Artisan($this->artisanCommand, self::COMMAND, $argument))(); |
|
43
|
|
|
} |
|
44
|
60 |
|
} |
|
45
|
|
|
|
|
46
|
60 |
|
private function addProvider(string $provider, $tag = null) |
|
47
|
|
|
{ |
|
48
|
60 |
|
$arguments['--provider'] = $provider; |
|
|
|
|
|
|
49
|
|
|
|
|
50
|
60 |
|
if ($tag !== null) { |
|
51
|
30 |
|
$arguments['--tag'] = $tag; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
60 |
|
if ($this->force) { |
|
55
|
12 |
|
$arguments['--force'] = true; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
60 |
|
$this->arguments[] = $arguments; |
|
59
|
60 |
|
} |
|
60
|
|
|
|
|
61
|
48 |
|
private function handleArray(): void |
|
62
|
|
|
{ |
|
63
|
48 |
|
foreach ($this->providers as $key => $value) { |
|
|
|
|
|
|
64
|
48 |
|
if (is_numeric($key)) { |
|
65
|
18 |
|
$this->addProvider($value); |
|
66
|
|
|
} else { |
|
67
|
30 |
|
$this->addProvider($key, $value); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
48 |
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
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.