|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MadWeb\Initializer\Actions; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
|
6
|
|
|
use InvalidArgumentException; |
|
7
|
|
|
|
|
8
|
|
|
class PublishTag extends Action |
|
9
|
|
|
{ |
|
10
|
|
|
protected const LOADING_TEXT = 'publishing'; |
|
11
|
|
|
|
|
12
|
|
|
private const COMMAND = 'vendor:publish'; |
|
13
|
|
|
|
|
14
|
|
|
/** @var string|array */ |
|
15
|
|
|
private $tags; |
|
16
|
|
|
|
|
17
|
|
|
/** @var bool */ |
|
18
|
|
|
private $force; |
|
19
|
|
|
|
|
20
|
|
|
/** @var array */ |
|
21
|
|
|
private $arguments = []; |
|
22
|
|
|
|
|
23
|
|
|
private $currentArgument = []; |
|
24
|
|
|
|
|
25
|
24 |
|
public function __construct(Command $artisanCommand, $tags, bool $force = false) |
|
26
|
|
|
{ |
|
27
|
24 |
|
parent::__construct($artisanCommand); |
|
28
|
|
|
|
|
29
|
24 |
|
$this->tags = $tags; |
|
30
|
24 |
|
$this->force = $force; |
|
31
|
24 |
|
} |
|
32
|
|
|
|
|
33
|
24 |
|
public function __invoke(): bool |
|
34
|
|
|
{ |
|
35
|
24 |
|
if (is_string($this->tags)) { |
|
36
|
12 |
|
$this->addTag($this->tags); |
|
37
|
12 |
|
} elseif (is_array($this->tags)) { |
|
38
|
12 |
|
$this->handleArray(); |
|
39
|
|
|
} else { |
|
40
|
|
|
throw new InvalidArgumentException('Invalid publishable argument.'); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
24 |
|
foreach ($this->arguments as $argument) { |
|
44
|
24 |
|
$this->currentArgument = $argument; |
|
45
|
|
|
|
|
46
|
24 |
|
$errors = []; |
|
47
|
24 |
|
parent::__invoke(); |
|
48
|
|
|
|
|
49
|
24 |
|
if ($this->errorMessage) { |
|
50
|
|
|
$errors[] = $this->errorMessage; |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
24 |
|
$this->errorMessage = implode(PHP_EOL, $errors); |
|
|
|
|
|
|
55
|
|
|
|
|
56
|
24 |
|
return true; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
24 |
|
public function title(): string |
|
60
|
|
|
{ |
|
61
|
24 |
|
$title = '<comment>Publish resource:</comment> '; |
|
62
|
|
|
|
|
63
|
|
|
$tagStringCallback = function (string $tag) { |
|
64
|
24 |
|
return " Tag [$tag]"; |
|
65
|
24 |
|
}; |
|
66
|
|
|
|
|
67
|
24 |
|
if (isset($this->currentArgument['--tag'])) { |
|
68
|
24 |
|
if (is_string($this->currentArgument['--tag'])) { |
|
69
|
24 |
|
$title .= $tagStringCallback($this->currentArgument['--tag']); |
|
70
|
|
|
} else { |
|
71
|
|
|
foreach ($this->currentArgument['--tag'] as $tag) { |
|
72
|
|
|
$title .= $tagStringCallback($tag); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
24 |
|
return trim($title); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
24 |
|
public function run(): bool |
|
81
|
|
|
{ |
|
82
|
24 |
|
$action = new Artisan($this->getArtisanCommnad(), self::COMMAND, $this->currentArgument); |
|
83
|
|
|
|
|
84
|
24 |
|
return $action->run(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
24 |
|
private function addTag(string $tag) |
|
88
|
|
|
{ |
|
89
|
24 |
|
$arguments['--tag'] = $tag; |
|
|
|
|
|
|
90
|
|
|
|
|
91
|
24 |
|
if ($this->force) { |
|
92
|
12 |
|
$arguments['--force'] = true; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
24 |
|
$this->arguments[] = $arguments; |
|
96
|
24 |
|
} |
|
97
|
|
|
|
|
98
|
12 |
|
private function handleArray(): void |
|
99
|
|
|
{ |
|
100
|
12 |
|
foreach ($this->tags as $tag) { |
|
|
|
|
|
|
101
|
12 |
|
$this->addTag($tag); |
|
102
|
|
|
} |
|
103
|
12 |
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
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: