|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\SlashCommand\Handlers; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
|
6
|
|
|
use Illuminate\Support\Str; |
|
7
|
|
|
use Spatie\SlashCommand\Attachment; |
|
8
|
|
|
use Spatie\SlashCommand\AttachmentField; |
|
9
|
|
|
use Spatie\SlashCommand\HandlesSlashCommand; |
|
10
|
|
|
use Spatie\SlashCommand\Request; |
|
11
|
|
|
use Spatie\SlashCommand\Response; |
|
12
|
|
|
|
|
13
|
|
|
class CatchAll extends BaseHandler |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* If this function returns true, the handle method will get called. |
|
17
|
|
|
* |
|
18
|
|
|
* @param \Spatie\SlashCommand\Request $request |
|
19
|
|
|
* |
|
20
|
|
|
* @return bool |
|
21
|
|
|
*/ |
|
22
|
|
|
public function canHandle(Request $request): bool |
|
23
|
|
|
{ |
|
24
|
|
|
return true; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Handle the given request. Remember that Slack expects a response |
|
29
|
|
|
* within three seconds after the slash command was issued. If |
|
30
|
|
|
* there is more time needed, dispatch a job. |
|
31
|
|
|
* |
|
32
|
|
|
* @param \Spatie\SlashCommand\Request $request |
|
33
|
|
|
* |
|
34
|
|
|
* @return \Spatie\SlashCommand\Response |
|
35
|
|
|
*/ |
|
36
|
|
|
public function handle(Request $request): Response |
|
37
|
|
|
{ |
|
38
|
|
|
$response = $this->respondToSlack("I did not recognize this command: `/{$request->command} {$request->text}`"); |
|
39
|
|
|
|
|
40
|
|
|
[$command] = explode(' ', $this->request->text); |
|
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
$alternativeHandlers = $this->findAlternativeHandlers($command); |
|
43
|
|
|
|
|
44
|
|
|
if ($alternativeHandlers->count()) { |
|
45
|
|
|
$response->withAttachment($this->getCommandListAttachment($alternativeHandlers)); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
if ($this->containsHelpHandler($alternativeHandlers)) { |
|
49
|
|
|
$response->withAttachment(Attachment::create() |
|
50
|
|
|
->setText("For all available commands, try `/{$request->command} help`") |
|
51
|
|
|
); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return $response; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
protected function findAlternativeHandlers(string $command): Collection |
|
58
|
|
|
{ |
|
59
|
|
|
$alternativeHandlers = collect(config('laravel-slack-slash-command.handlers')) |
|
60
|
|
|
->map(function (string $handlerClassName) { |
|
61
|
|
|
return new $handlerClassName($this->request); |
|
62
|
|
|
}) |
|
63
|
|
|
->filter(function (HandlesSlashCommand $handler) { |
|
64
|
|
|
return $handler instanceof SignatureHandler; |
|
65
|
|
|
}) |
|
66
|
|
|
->filter(function (SignatureHandler $handler) { |
|
67
|
|
|
$signatureParts = new SignatureParts($handler->getSignature()); |
|
68
|
|
|
|
|
69
|
|
|
return Str::is($signatureParts->getSlashCommandName(), $this->request->command); |
|
70
|
|
|
}); |
|
71
|
|
|
|
|
72
|
|
|
if (strpos($command, ':') !== false) { |
|
73
|
|
|
$subHandlers = $this->findInNamespace($alternativeHandlers, $command); |
|
74
|
|
|
if ($subHandlers->count()) { |
|
75
|
|
|
return $subHandlers; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $alternativeHandlers->filter(function (SignatureHandler $handler) use ($command) { |
|
80
|
|
|
return levenshtein($handler->getName(), $command) <= 2; |
|
81
|
|
|
}); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
protected function findInNamespace(Collection $handlers, string $command): Collection |
|
85
|
|
|
{ |
|
86
|
|
|
// Find commands in the same namespace |
|
87
|
|
|
[$namespace, $subCommand] = explode(':', $command); |
|
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
$subHandlers = $handlers->filter(function (SignatureHandler $handler) use ($namespace) { |
|
90
|
|
|
return Str::startsWith($handler->getName(), $namespace.':'); |
|
91
|
|
|
}); |
|
92
|
|
|
|
|
93
|
|
|
return $subHandlers; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
protected function getCommandListAttachment(Collection $handlers): Attachment |
|
97
|
|
|
{ |
|
98
|
|
|
$attachmentFields = $handlers |
|
99
|
|
|
->map(function (SignatureHandler $handler) { |
|
100
|
|
|
return AttachmentField::create($handler->getFullCommand(), $handler->getDescription()); |
|
101
|
|
|
}) |
|
102
|
|
|
->all(); |
|
103
|
|
|
|
|
104
|
|
|
return Attachment::create() |
|
105
|
|
|
->setColor('warning') |
|
106
|
|
|
->setTitle('Did you mean:') |
|
107
|
|
|
->setFields($attachmentFields); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
protected function containsHelpHandler(Collection $alternativeHandlers): bool |
|
111
|
|
|
{ |
|
112
|
|
|
return ! $alternativeHandlers->filter(function (SignatureHandler $handler) { |
|
113
|
|
|
return $handler instanceof Help; |
|
114
|
|
|
})->isEmpty(); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.