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
|
|
|
private const COMMAND = 'vendor:publish'; |
11
|
|
|
|
12
|
|
|
/** @var string|array */ |
13
|
|
|
private $providers; |
14
|
|
|
|
15
|
|
|
/** @var bool */ |
16
|
|
|
private $force; |
17
|
|
|
|
18
|
|
|
/** @var array */ |
19
|
|
|
private $arguments = []; |
20
|
|
|
|
21
|
|
|
private $currentArgument = []; |
22
|
|
|
|
23
|
66 |
|
public function __construct(Command $artisanCommand, $providers, bool $force = false) |
24
|
|
|
{ |
25
|
66 |
|
parent::__construct($artisanCommand); |
26
|
|
|
|
27
|
66 |
|
$this->providers = $providers; |
28
|
66 |
|
$this->force = $force; |
29
|
66 |
|
} |
30
|
|
|
|
31
|
66 |
|
public function __invoke(): bool |
32
|
|
|
{ |
33
|
66 |
|
if (is_string($this->providers)) { |
34
|
12 |
|
$this->addProvider($this->providers); |
35
|
54 |
|
} elseif (is_array($this->providers)) { |
36
|
48 |
|
$this->handleArray(); |
37
|
|
|
} else { |
38
|
6 |
|
throw new InvalidArgumentException('Invalid publishable argument.'); |
39
|
|
|
} |
40
|
|
|
|
41
|
60 |
|
foreach ($this->arguments as $argument) { |
42
|
60 |
|
$this->currentArgument = $argument; |
43
|
|
|
|
44
|
60 |
|
$errors = []; |
45
|
60 |
|
parent::__invoke(); |
46
|
|
|
|
47
|
60 |
|
if ($this->errorMessage) { |
48
|
|
|
$errors[] = $this->errorMessage; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
60 |
|
$this->errorMessage = implode(PHP_EOL, $errors); |
|
|
|
|
53
|
|
|
|
54
|
60 |
|
return true; |
55
|
|
|
} |
56
|
|
|
|
57
|
60 |
|
public function title(): string |
58
|
|
|
{ |
59
|
60 |
|
$title = '<comment>Publishing resource:</comment> '; |
60
|
|
|
|
61
|
60 |
|
if (isset($this->currentArgument['--provider'])) { |
62
|
60 |
|
$title .= "Provider [{$this->currentArgument['--provider']}]"; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$tagStringCallback = function (string $tag) { |
66
|
30 |
|
return " Tag [$tag]"; |
67
|
60 |
|
}; |
68
|
|
|
|
69
|
60 |
|
if (isset($this->currentArgument['--tag'])) { |
70
|
30 |
|
if (is_string($this->currentArgument['--tag'])) { |
71
|
24 |
|
$title .= $tagStringCallback($this->currentArgument['--tag']); |
72
|
|
|
} else { |
73
|
6 |
|
foreach ($this->currentArgument['--tag'] as $tag) { |
74
|
6 |
|
$title .= $tagStringCallback($tag); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
60 |
|
return $title; |
80
|
|
|
} |
81
|
|
|
|
82
|
60 |
|
public function run(): bool |
83
|
|
|
{ |
84
|
60 |
|
$action = new Artisan($this->getArtisanCommnad(), self::COMMAND, $this->currentArgument); |
85
|
|
|
|
86
|
60 |
|
return $action->run(); |
87
|
|
|
} |
88
|
|
|
|
89
|
60 |
|
private function addProvider(string $provider, $tag = null) |
90
|
|
|
{ |
91
|
60 |
|
$arguments['--provider'] = $provider; |
|
|
|
|
92
|
|
|
|
93
|
60 |
|
if ($tag !== null) { |
94
|
30 |
|
$arguments['--tag'] = $tag; |
95
|
|
|
} |
96
|
|
|
|
97
|
60 |
|
if ($this->force) { |
98
|
12 |
|
$arguments['--force'] = true; |
99
|
|
|
} |
100
|
|
|
|
101
|
60 |
|
$this->arguments[] = $arguments; |
102
|
60 |
|
} |
103
|
|
|
|
104
|
48 |
|
private function handleArray(): void |
105
|
|
|
{ |
106
|
48 |
|
foreach ($this->providers as $key => $value) { |
|
|
|
|
107
|
48 |
|
if (is_numeric($key)) { |
108
|
18 |
|
$this->addProvider($value); |
109
|
|
|
} else { |
110
|
30 |
|
$this->addProvider($key, $value); |
111
|
|
|
} |
112
|
|
|
} |
113
|
48 |
|
} |
114
|
|
|
} |
115
|
|
|
|
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: