-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathCommandWrapper.php
184 lines (165 loc) · 3.74 KB
/
CommandWrapper.php
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
<?php declare(strict_types=1);
/**
* The file is part of inhere/console
*
* @author https://github.com/inhere
* @homepage https://github.com/inhere/php-console
* @license https://github.com/inhere/php-console/blob/master/LICENSE
*/
namespace Inhere\Console\Handler;
use Inhere\Console\Command;
use Inhere\Console\IO\Input;
use Inhere\Console\IO\Output;
use BadMethodCallException;
use Toolkit\PFlag\FlagsParser;
/**
* Class CommandWrapper - wrap an callable as Command
*
* @package Inhere\Console\Handler
*/
class CommandWrapper extends Command
{
/**
* @var callable(FlagsParser, Output): mixed
*/
private $callable;
/**
* @var array{name: string, desc: string, options: array, arguments: array} = [
* 'name' => '',
* 'desc' => '',
* 'aliases' => [],
* 'options' => [],
* 'arguments' => []
* ]
*/
protected array $config = [];
/**
* @param callable(FlagsParser, Output):mixed $handleFn
*
* @return $this
*/
public static function new(callable $handleFn, array $config = []): self
{
return (new self())->withConfig($config)->setCallable($handleFn);
}
/**
* @param callable(FlagsParser, Output):mixed $handleFn
* @param array $config
*
* @return static
*/
public static function wrap(callable $handleFn, array $config = []): self
{
return (new self())->withConfig($config)->setCallable($handleFn);
}
/**
* @param callable $fn
*
* @return $this
*/
public function withCustom(callable $fn): self
{
$fn($this);
return $this;
}
/**
* @param callable(FlagsParser, Output):mixed $fn
*
* @return $this
*/
public function withFunc(callable $fn): self
{
return $this->setCallable($fn);
}
/**
* @param array $config
*
* @return $this
*/
public function withConfig(array $config): self
{
$this->config = $config;
return $this;
}
/**
* @param array $options
*
* @return $this
*/
public function withOptions(array $options): self
{
$this->config['options'] = $options;
return $this;
}
/**
* @param array $arguments
*
* @return $this
*/
public function withArguments(array $arguments): self
{
$this->config['arguments'] = $arguments;
return $this;
}
/**
* @param callable(FlagsParser, Output):mixed $callable
*
* @return static
*/
public function setCallable(callable $callable): self
{
$this->callable = $callable;
return $this;
}
/**
* @return array
*/
protected function getOptions(): array
{
return $this->config['options'] ?? [];
}
/**
* @return array
*/
protected function getArguments(): array
{
return $this->config['arguments'] ?? [];
}
/**
* @return string
*/
public function getRealDesc(): string
{
return $this->config['desc'] ?? '';
}
/**
* @return string
*/
public function getRealName(): string
{
return $this->config['name'] ?? '';
}
/**
* @return array
*/
public function getConfig(): array
{
return $this->config;
}
/**
* Do execute command
*
* @param Input $input
* @param Output $output
*
* @return mixed
*/
protected function execute(Input $input, Output $output): mixed
{
if (!$call = $this->callable) {
throw new BadMethodCallException('The command handler property is empty');
}
// call custom callable
return $call($this->flags, $output);
}
}