|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\SlashCommand\Handlers; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Console\Parser; |
|
6
|
|
|
use Spatie\SlashCommand\Exceptions\InvalidHandler; |
|
7
|
|
|
use Spatie\SlashCommand\Request; |
|
8
|
|
|
use Symfony\Component\Console\Exception\RuntimeException; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputDefinition; |
|
10
|
|
|
use Symfony\Component\Console\Input\StringInput; |
|
11
|
|
|
|
|
12
|
|
|
abstract class SignatureHandler extends BaseHandler |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var string */ |
|
15
|
|
|
protected $name; |
|
16
|
|
|
|
|
17
|
|
|
/** @var \Symfony\Component\Console\Input\StringInput */ |
|
18
|
|
|
protected $input; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct(Request $request) |
|
21
|
|
|
{ |
|
22
|
|
|
parent::__construct($request); |
|
23
|
|
|
|
|
24
|
|
|
if (empty($this->signature)) { |
|
|
|
|
|
|
25
|
|
|
throw InvalidHandler::signatureIsRequired(static::class); |
|
26
|
|
|
} |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
public function getArgument($foo) |
|
32
|
|
|
{ |
|
33
|
|
|
return $this->input->getArgument($foo); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function getOption($foo) |
|
37
|
|
|
{ |
|
38
|
|
|
return $this->input->getOption($foo); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function getArguments(): array |
|
42
|
|
|
{ |
|
43
|
|
|
return $this->input->getArguments(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function getOptions(): array |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->input->getOptions(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function canHandle(Request $request): bool |
|
52
|
|
|
{ |
|
53
|
|
|
$signatureParts = new SignatureParts($this->signature); |
|
54
|
|
|
|
|
55
|
|
|
if (! $this->bind($signatureParts->getSignatureWithoutCommandName())) { |
|
56
|
|
|
return false; |
|
57
|
|
|
}; |
|
58
|
|
|
|
|
59
|
|
|
if ($request->command != $signatureParts->getSlashCommandName()) { |
|
60
|
|
|
return false; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
if (explode(' ', $request->text)[0] = $signatureParts->getHandlerName()) { |
|
64
|
|
|
return false; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return true; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
protected function bind(string $signature) |
|
71
|
|
|
{ |
|
72
|
|
|
list($name, $arguments, $options) = Parser::parse($signature); |
|
73
|
|
|
|
|
74
|
|
|
$this->name = $name; |
|
75
|
|
|
|
|
76
|
|
|
$inputDefinition = new InputDefinition(); |
|
77
|
|
|
|
|
78
|
|
|
foreach ($arguments as $argument) { |
|
79
|
|
|
$inputDefinition->addArgument($argument); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
foreach ($options as $option) { |
|
83
|
|
|
$inputDefinition->addOption($option); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$this->input = new StringInput($this->request->text); |
|
87
|
|
|
|
|
88
|
|
|
try { |
|
89
|
|
|
$this->input->bind($inputDefinition); |
|
90
|
|
|
|
|
91
|
|
|
} catch (RuntimeException $exception) { |
|
92
|
|
|
|
|
93
|
|
|
throw $exception; |
|
94
|
|
|
return false; |
|
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return true; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: