1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\SlashCommand\Handlers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use Spatie\SlashCommand\Attachment; |
7
|
|
|
use Spatie\SlashCommand\AttachmentField; |
8
|
|
|
use Spatie\SlashCommand\HandlesSlashCommand; |
9
|
|
|
use Spatie\SlashCommand\Request; |
10
|
|
|
use Spatie\SlashCommand\Response; |
11
|
|
|
|
12
|
|
|
class Help extends BaseHandler |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Check if the command begins with 'help' |
17
|
|
|
* |
18
|
|
|
* @param \Spatie\SlashCommand\Request $request |
19
|
|
|
* |
20
|
|
|
* @return bool |
21
|
|
|
*/ |
22
|
|
|
public function canHandle(Request $request): bool |
23
|
|
|
{ |
24
|
|
|
return Str::startsWith($request->text, 'help'); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Handle the given request. |
29
|
|
|
* |
30
|
|
|
* @param \Spatie\SlashCommand\Request $request |
31
|
|
|
* |
32
|
|
|
* @return \Spatie\SlashCommand\Response |
33
|
|
|
*/ |
34
|
|
|
public function handle(Request $request): Response |
35
|
|
|
{ |
36
|
|
|
$command = trim(substr($this->request->text, 4)); |
37
|
|
|
$helpRequest = clone $this->request; |
38
|
|
|
$helpRequest->text = $command; |
39
|
|
|
|
40
|
|
|
$handlers = collect(config('laravel-slack-slash-command.handlers')) |
41
|
|
|
->map(function (string $handlerClassName) use($helpRequest) { |
42
|
|
|
return new $handlerClassName($helpRequest); |
43
|
|
|
}) |
44
|
|
|
->filter(function (HandlesSlashCommand $handler) use ($helpRequest){ |
45
|
|
|
if ($handler instanceof SignatureHandler && isset($handler->signature)) { |
46
|
|
|
$signatureParts = new SignatureParts($handler->signature); |
|
|
|
|
47
|
|
|
return in_array($signatureParts->getSlashCommandName(), [$this->request->command, '*']); |
48
|
|
|
} |
49
|
|
|
}); |
50
|
|
|
|
51
|
|
|
// When command is passed, find all commands |
52
|
|
|
if (! empty($command)) { |
53
|
|
|
|
54
|
|
|
/** @var SignatureHandler $handler */ |
55
|
|
|
$handler = $handlers |
56
|
|
|
->filter(function (HandlesSlashCommand $handler) use ($helpRequest){ |
57
|
|
|
return $handler->canHandle($helpRequest); |
|
|
|
|
58
|
|
|
}) |
59
|
|
|
->first(); |
60
|
|
|
|
61
|
|
|
$signature = $this->formatSignature($handler->signature); |
|
|
|
|
62
|
|
|
|
63
|
|
|
return $this->respondToSlack("Usage for command */{$this->request->command} {$command}*") |
64
|
|
|
->withAttachment(Attachment::create()->setText($signature)); |
65
|
|
|
} else { |
66
|
|
|
// Create AttachmentFields for each handler |
67
|
|
|
$attachmentFields = collect($handlers)->reduce(function (array $attachmentFields, SignatureHandler $handler) { |
68
|
|
|
|
69
|
|
|
$signature = $this->formatSignature($handler->signature); |
|
|
|
|
70
|
|
|
$signatureParts = new SignatureParts($signature); |
71
|
|
|
$attachmentFields[] = AttachmentField::create($signatureParts->getHandlerName(), $signature); |
72
|
|
|
|
73
|
|
|
return $attachmentFields; |
74
|
|
|
}, []); |
75
|
|
|
|
76
|
|
|
return $this->respondToSlack("Listing all commands available for */{$this->request->command}*:") |
77
|
|
|
->withAttachment(Attachment::create() |
78
|
|
|
->setFields($attachmentFields) |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
protected function formatSignature($signature) |
84
|
|
|
{ |
85
|
|
|
$signatureParts = new SignatureParts($signature); |
86
|
|
|
return '/' . $this->request->command . ' ' . $signatureParts->getSignatureWithoutCommandName(); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
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.