1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MadWeb\Initializer\Actions; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
|
8
|
|
|
class Publish extends Action |
9
|
|
|
{ |
10
|
|
|
protected const LOADING_TEXT = 'publishing'; |
11
|
|
|
|
12
|
|
|
private const COMMAND = 'vendor:publish'; |
13
|
|
|
|
14
|
|
|
/** @var string|array */ |
15
|
|
|
private $providers; |
16
|
|
|
|
17
|
|
|
/** @var bool */ |
18
|
|
|
private $force; |
19
|
|
|
|
20
|
|
|
/** @var array */ |
21
|
|
|
private $arguments = []; |
22
|
|
|
|
23
|
|
|
private $currentArgument = []; |
24
|
|
|
|
25
|
72 |
|
public function __construct(Command $artisanCommand, $providers, bool $force = false) |
26
|
|
|
{ |
27
|
72 |
|
parent::__construct($artisanCommand); |
28
|
|
|
|
29
|
72 |
|
$this->providers = $providers; |
30
|
72 |
|
$this->force = $force; |
31
|
72 |
|
} |
32
|
|
|
|
33
|
72 |
|
public function __invoke(): bool |
34
|
|
|
{ |
35
|
72 |
|
if (is_string($this->providers)) { |
36
|
12 |
|
$this->addProvider($this->providers); |
37
|
60 |
|
} elseif (is_array($this->providers)) { |
38
|
48 |
|
$this->handleArray(); |
39
|
|
|
} else { |
40
|
12 |
|
throw new InvalidArgumentException('Invalid publishable argument.'); |
41
|
|
|
} |
42
|
|
|
|
43
|
60 |
|
foreach ($this->arguments as $argument) { |
44
|
60 |
|
$this->currentArgument = $argument; |
45
|
|
|
|
46
|
60 |
|
$errors = []; |
47
|
60 |
|
parent::__invoke(); |
48
|
|
|
|
49
|
60 |
|
if ($this->errorMessage) { |
50
|
|
|
$errors[] = $this->errorMessage; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
60 |
|
$this->errorMessage = implode(PHP_EOL, $errors); |
|
|
|
|
55
|
|
|
|
56
|
60 |
|
return true; |
57
|
|
|
} |
58
|
|
|
|
59
|
60 |
|
public function title(): string |
60
|
|
|
{ |
61
|
60 |
|
$title = '<comment>Publish resource:</comment> '; |
62
|
|
|
|
63
|
60 |
|
if (isset($this->currentArgument['--provider'])) { |
64
|
60 |
|
$title .= "Provider [{$this->currentArgument['--provider']}]"; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$tagStringCallback = function (string $tag) { |
68
|
30 |
|
return " Tag [$tag]"; |
69
|
60 |
|
}; |
70
|
|
|
|
71
|
60 |
|
if (isset($this->currentArgument['--tag'])) { |
72
|
30 |
|
if (is_string($this->currentArgument['--tag'])) { |
73
|
24 |
|
$title .= $tagStringCallback($this->currentArgument['--tag']); |
74
|
|
|
} else { |
75
|
6 |
|
foreach ($this->currentArgument['--tag'] as $tag) { |
76
|
6 |
|
$title .= $tagStringCallback($tag); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
60 |
|
return $title; |
82
|
|
|
} |
83
|
|
|
|
84
|
60 |
|
public function run(): bool |
85
|
|
|
{ |
86
|
60 |
|
$action = new Artisan($this->getArtisanCommnad(), self::COMMAND, $this->currentArgument); |
87
|
|
|
|
88
|
60 |
|
return $action->run(); |
89
|
|
|
} |
90
|
|
|
|
91
|
60 |
|
private function addProvider(string $provider, $tag = null) |
92
|
|
|
{ |
93
|
60 |
|
$arguments['--provider'] = $provider; |
|
|
|
|
94
|
|
|
|
95
|
60 |
|
if ($tag !== null) { |
96
|
30 |
|
$arguments['--tag'] = $tag; |
97
|
|
|
} |
98
|
|
|
|
99
|
60 |
|
if ($this->force) { |
100
|
12 |
|
$arguments['--force'] = true; |
101
|
|
|
} |
102
|
|
|
|
103
|
60 |
|
$this->arguments[] = $arguments; |
104
|
60 |
|
} |
105
|
|
|
|
106
|
48 |
|
private function handleArray(): void |
107
|
|
|
{ |
108
|
48 |
|
foreach ($this->providers as $key => $value) { |
|
|
|
|
109
|
48 |
|
if (is_numeric($key)) { |
110
|
18 |
|
$this->addProvider($value); |
111
|
|
|
} else { |
112
|
30 |
|
$this->addProvider($key, $value); |
113
|
|
|
} |
114
|
|
|
} |
115
|
48 |
|
} |
116
|
|
|
} |
117
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: