1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* LnmsCommand.php |
4
|
|
|
* |
5
|
|
|
* Convenience class for common command code |
6
|
|
|
* |
7
|
|
|
* This program is free software: you can redistribute it and/or modify |
8
|
|
|
* it under the terms of the GNU General Public License as published by |
9
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
10
|
|
|
* (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the |
15
|
|
|
* GNU General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU General Public License |
18
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
19
|
|
|
* |
20
|
|
|
* @link https://www.librenms.org |
21
|
|
|
* |
22
|
|
|
* @copyright 2019 Tony Murray |
23
|
|
|
* @author Tony Murray <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
namespace App\Console; |
27
|
|
|
|
28
|
|
|
use Illuminate\Console\Command; |
29
|
|
|
use Illuminate\Validation\ValidationException; |
30
|
|
|
use Symfony\Component\Console\Exception\InvalidArgumentException; |
31
|
|
|
use Validator; |
32
|
|
|
|
33
|
|
|
abstract class LnmsCommand extends Command |
34
|
|
|
{ |
35
|
|
|
protected $developer = false; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Create a new command instance. |
39
|
|
|
* |
40
|
|
|
* @return void |
41
|
|
|
*/ |
42
|
|
|
public function __construct() |
43
|
|
|
{ |
44
|
|
|
parent::__construct(); |
45
|
|
|
$this->setDescription(__('commands.' . $this->getName() . '.description')); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function isHidden() |
49
|
|
|
{ |
50
|
|
|
$env = $this->getLaravel() ? $this->getLaravel()->environment() : getenv('APP_ENV'); |
51
|
|
|
|
52
|
|
|
return $this->hidden || ($this->developer && $env !== 'production'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Adds an argument. If $description is null, translate commands.command-name.arguments.name |
57
|
|
|
* If you want the description to be empty, just set an empty string |
58
|
|
|
* |
59
|
|
|
* @param string $name The argument name |
60
|
|
|
* @param int|null $mode The argument mode: InputArgument::REQUIRED or InputArgument::OPTIONAL |
61
|
|
|
* @param string $description A description text |
62
|
|
|
* @param string|string[]|null $default The default value (for InputArgument::OPTIONAL mode only) |
63
|
|
|
* @return $this |
64
|
|
|
* |
65
|
|
|
* @throws InvalidArgumentException When argument mode is not valid |
66
|
|
|
*/ |
67
|
|
|
public function addArgument($name, $mode = null, $description = null, $default = null) |
68
|
|
|
{ |
69
|
|
|
// use a generated translation location by default |
70
|
|
|
if (is_null($description)) { |
71
|
|
|
$description = __('commands.' . $this->getName() . '.arguments.' . $name); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
parent::addArgument($name, $mode, $description, $default); |
|
|
|
|
75
|
|
|
|
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Adds an option. If $description is null, translate commands.command-name.arguments.name |
81
|
|
|
* If you want the description to be empty, just set an empty string |
82
|
|
|
* |
83
|
|
|
* @param string $name The option name |
84
|
|
|
* @param string|array|null $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts |
85
|
|
|
* @param int|null $mode The option mode: One of the InputOption::VALUE_* constants |
86
|
|
|
* @param string $description A description text |
87
|
|
|
* @param string|string[]|int|bool|null $default The default value (must be null for InputOption::VALUE_NONE) |
88
|
|
|
* @return $this |
89
|
|
|
* |
90
|
|
|
* @throws InvalidArgumentException If option mode is invalid or incompatible |
91
|
|
|
*/ |
92
|
|
|
public function addOption($name, $shortcut = null, $mode = null, $description = null, $default = null) |
93
|
|
|
{ |
94
|
|
|
// use a generated translation location by default |
95
|
|
|
if (is_null($description)) { |
96
|
|
|
$description = __('commands.' . $this->getName() . '.options.' . $name); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
parent::addOption($name, $shortcut, $mode, $description, $default); |
|
|
|
|
100
|
|
|
|
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Validate the input of this command. Uses Laravel input validation |
106
|
|
|
* merging the arguments and options together to check. |
107
|
|
|
*/ |
108
|
|
|
protected function validate(array $rules, array $messages = []): array |
109
|
|
|
{ |
110
|
|
|
$validator = Validator::make( |
111
|
|
|
$this->arguments() + $this->options(), |
112
|
|
|
$rules, |
113
|
|
|
array_merge(trans('commands.' . $this->getName() . '.validation-errors'), $messages) |
|
|
|
|
114
|
|
|
); |
115
|
|
|
|
116
|
|
|
try { |
117
|
|
|
$validator->validate(); |
118
|
|
|
|
119
|
|
|
return $validator->validated(); |
120
|
|
|
} catch (ValidationException $e) { |
121
|
|
|
collect($validator->getMessageBag()->all())->each(function ($message) { |
122
|
|
|
$this->error($message); |
123
|
|
|
}); |
124
|
|
|
exit(1); |
|
|
|
|
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|