-
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathCommand.php
More file actions
198 lines (176 loc) · 5.58 KB
/
Command.php
File metadata and controls
198 lines (176 loc) · 5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
namespace Winter\Storm\Console;
use Illuminate\Console\Command as BaseCommand;
use Illuminate\Console\OutputStyle;
use Illuminate\Console\View\Components\Factory;
use Illuminate\Contracts\Console\PromptsForMissingInput;
use Symfony\Component\Console\Command\Command as SymfonyCommand;
use Symfony\Component\Console\Command\SignalableCommandInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Termwind\Termwind;
use Winter\Storm\Extension\ExtendableTrait;
use Winter\Storm\Support\Traits\Emitter;
use function Termwind\renderUsing;
/**
* Command base class
* Contains utilities to make developing CLI commands nicer
*
* @author Luke Towers
*
* @method static mixed extend(callable $callback, bool $scoped = false, ?object $outerScope = null)
*/
abstract class Command extends BaseCommand implements PromptsForMissingInput, SignalableCommandInterface
{
use Traits\HandlesCleanup;
use Traits\ProvidesAutocompletion;
use ExtendableTrait;
use Emitter;
/**
* @var string|array|null Extensions implemented by this class.
*/
public $implement = null;
/**
* @var \Winter\Storm\Foundation\Application
*/
protected $laravel;
/**
* @var array List of commands that this command replaces (aliases)
*/
protected $replaces = [];
/**
* Create a new command instance.
*/
public function __construct()
{
parent::__construct();
if (!empty($this->replaces)) {
$this->setAliases($this->replaces);
}
$this->extendableConstruct();
}
/**
* Override the laravel run function to allow us to run callbacks on the command prior to excution.
* Run the console command.
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @return int
*/
public function run(InputInterface $input, OutputInterface $output): int
{
$this->output = $this->laravel->make(
OutputStyle::class,
['input' => $input, 'output' => $output]
);
$this->components = $this->laravel->make(Factory::class, ['output' => $this->output]);
/**
* @event command.beforeRun
* Called before the command is run; useful for intercepting the output of the command or auditing the commands run
*
* Example usage:
*
* Command::extend(function (Command $command) {
* $command->bindEvent('command.beforeRun', function () use ($command) {
* MyTaskManager::instance()->setOutput($command->getOutput());
* });
* });
*
*/
$this->fireEvent('command.beforeRun', [$this]);
$renderer = Termwind::getRenderer();
renderUsing($this->output->getOutput());
try {
// Calling the grandparent run() method, see: https://www.php.net/manual/en/language.oop5.inheritance.php#100005
return SymfonyCommand::run(
$this->input = $input,
$this->output
);
} finally {
$this->untrap();
// Restore the original termwind renderer
renderUsing($renderer);
}
}
/**
* Write a string in an alert box.
*
* @param string $string
* @param int|string|null $verbosity
* @return void
*/
public function alert($string, $verbosity = null)
{
$this->components->alert($string, $this->parseVerbosity($verbosity));
}
/**
* Write a string as error output.
*
* @param string $string
* @param int|string|null $verbosity
* @return void
*/
public function error($string, $verbosity = null)
{
$this->components->error($string, $this->parseVerbosity($verbosity));
}
/**
* Magic allowing for extendable properties
*
* @param $name
* @return mixed|null
*/
public function __get($name)
{
return $this->extendableGet($name);
}
/**
* Magic allowing for extendable properties
*
* @param $name
* @param $value
* @return void
*/
public function __set($name, $value)
{
$this->extendableSet($name, $value);
}
/**
* Magic allowing for dynamic extension
*
* @param $name
* @param $params
* @return mixed
*/
public function __call($name, $params)
{
if ($name === 'extend') {
if (empty($params[0]) || !is_callable($params[0])) {
throw new \InvalidArgumentException('The extend() method requires a callback parameter or closure.');
}
if ($params[0] instanceof \Closure) {
return $params[0]->call($this, $params[1] ?? $this);
}
return \Closure::fromCallable($params[0])->call($this, $params[1] ?? $this);
}
return $this->extendableCall($name, $params);
}
/**
* Magic allowing for dynamic static extension
*
* @param $name
* @param $params
* @return mixed|void
*/
public static function __callStatic($name, $params)
{
if ($name === 'extend') {
if (empty($params[0])) {
throw new \InvalidArgumentException('The extend() method requires a callback parameter or closure.');
}
self::extendableExtendCallback($params[0], $params[1] ?? false, $params[2] ?? null);
return;
}
return parent::__callStatic($name, $params);
}
}