1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\SlashCommand\Handlers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Parser; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use Spatie\SlashCommand\Exceptions\InvalidHandler; |
8
|
|
|
use Spatie\SlashCommand\Request; |
9
|
|
|
use Symfony\Component\Console\Exception\RuntimeException; |
10
|
|
|
use Symfony\Component\Console\Input\InputDefinition; |
11
|
|
|
use Symfony\Component\Console\Input\StringInput; |
12
|
|
|
|
13
|
|
|
abstract class SignatureHandler extends BaseHandler |
14
|
|
|
{ |
15
|
|
|
/** @var string */ |
16
|
|
|
protected $name; |
17
|
|
|
|
18
|
|
|
/** @var \Symfony\Component\Console\Input\StringInput */ |
19
|
|
|
protected $input; |
20
|
|
|
|
21
|
|
|
/** @var bool */ |
22
|
|
|
protected $signatureIsBound; |
23
|
|
|
|
24
|
|
|
public function __construct(Request $request) |
25
|
|
|
{ |
26
|
|
|
parent::__construct($request); |
27
|
|
|
|
28
|
|
|
if (empty($this->signature)) { |
|
|
|
|
29
|
|
|
throw InvalidHandler::signatureIsRequired(static::class); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$this->signatureIsBound = $this->bindSignature($this->signature); |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function getArgument($foo) |
36
|
|
|
{ |
37
|
|
|
return $this->input->getArgument($foo); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function getOption($foo) |
41
|
|
|
{ |
42
|
|
|
return $this->input->getOption($foo); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function getArguments(): array |
46
|
|
|
{ |
47
|
|
|
return $this->input->getArguments(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function getOptions(): array |
51
|
|
|
{ |
52
|
|
|
return $this->input->getOptions(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function canHandle(Request $request): bool |
56
|
|
|
{ |
57
|
|
|
if (! $this->signatureIsBound) { |
58
|
|
|
return false; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$signatureParts = new SignatureParts($this->signature); |
|
|
|
|
62
|
|
|
|
63
|
|
|
|
64
|
|
|
if (! Str::is($signatureParts->getSlashCommandName(), $request->command)) { |
65
|
|
|
return false; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if (explode(' ', $request->text)[0] != $signatureParts->getHandlerName()) { |
69
|
|
|
return false; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return true; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
protected function bindSignature() |
76
|
|
|
{ |
77
|
|
|
$signatureParts = new SignatureParts($this->signature); |
|
|
|
|
78
|
|
|
|
79
|
|
|
$signature = $signatureParts->getSignatureWithoutCommandName(); |
80
|
|
|
|
81
|
|
|
list($name, $arguments, $options) = Parser::parse($signature); |
82
|
|
|
|
83
|
|
|
$this->name = $name; |
84
|
|
|
|
85
|
|
|
$inputDefinition = new InputDefinition(); |
86
|
|
|
|
87
|
|
|
foreach ($arguments as $argument) { |
88
|
|
|
$inputDefinition->addArgument($argument); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
foreach ($options as $option) { |
92
|
|
|
$inputDefinition->addOption($option); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$inputWithoutHandlerName = explode(' ', $this->request->text, 2)[1] ?? ''; |
96
|
|
|
|
97
|
|
|
$this->input = new StringInput($inputWithoutHandlerName); |
98
|
|
|
|
99
|
|
|
try { |
100
|
|
|
$this->input->bind($inputDefinition); |
101
|
|
|
} catch (RuntimeException $exception) { |
102
|
|
|
return false; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return true; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return Response|null |
110
|
|
|
*/ |
111
|
|
|
protected function validate() |
112
|
|
|
{ |
113
|
|
|
try { |
114
|
|
|
$this->input->validate(); |
115
|
|
|
} catch (RuntimeException $e) { |
116
|
|
|
return $this->respondToSlack('')->withAttachment( |
117
|
|
|
Attachment::create() |
118
|
|
|
->setColor('danger') |
119
|
|
|
->setText($e->getMessage()) |
120
|
|
|
) |
121
|
|
|
->withAttachment( |
122
|
|
|
Attachment::create() |
123
|
|
|
->setText($this->getHelpDescription()) |
|
|
|
|
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.