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
|
|
|
use Symfony\Component\Console\Command\Command; |
12
|
|
|
use Symfony\Component\Console\Helper\DescriptorHelper; |
13
|
|
|
use Symfony\Component\Console\Output\BufferedOutput; |
14
|
|
|
|
15
|
|
|
class Help extends SignatureHandler |
16
|
|
|
{ |
17
|
|
|
protected $signature = '* help {command? : The command you want information about}'; |
18
|
|
|
|
19
|
|
|
protected $description = 'List all commands or provide information about all commands'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Handle the given request. |
23
|
|
|
* |
24
|
|
|
* @param \Spatie\SlashCommand\Request $request |
25
|
|
|
* |
26
|
|
|
* @return \Spatie\SlashCommand\Response |
27
|
|
|
*/ |
28
|
|
|
public function handle(Request $request): Response |
29
|
|
|
{ |
30
|
|
|
$command = $this->getArgument('command'); |
31
|
|
|
|
32
|
|
|
$helpRequest = clone $this->request; |
33
|
|
|
$helpRequest->text = $command; |
34
|
|
|
|
35
|
|
|
$handlers = collect(config('laravel-slack-slash-command.handlers')) |
36
|
|
|
->map(function (string $handlerClassName) use($helpRequest) { |
37
|
|
|
return new $handlerClassName($helpRequest); |
38
|
|
|
}) |
39
|
|
|
->filter(function (HandlesSlashCommand $handler) use ($helpRequest){ |
40
|
|
|
if ($handler instanceof SignatureHandler) { |
41
|
|
|
$signatureParts = new SignatureParts($handler->getSignature()); |
42
|
|
|
return in_array($signatureParts->getSlashCommandName(), [$this->request->command, '*']); |
43
|
|
|
} |
44
|
|
|
}); |
45
|
|
|
|
46
|
|
|
// When command is passed, find all commands |
47
|
|
|
if (! empty($command)) { |
48
|
|
|
|
49
|
|
|
/** @var SignatureHandler $handler */ |
50
|
|
|
$handler = $handlers |
51
|
|
|
->filter(function (HandlesSlashCommand $handler) use ($helpRequest){ |
52
|
|
|
return $handler->canHandle($helpRequest); |
|
|
|
|
53
|
|
|
}) |
54
|
|
|
->first(); |
55
|
|
|
|
56
|
|
|
return $this->respondToSlack('') |
57
|
|
|
->withAttachment(Attachment::create() |
58
|
|
|
->addField($this->getAttachmentFieldForHandler($handler)) |
59
|
|
|
); |
60
|
|
|
} else { |
61
|
|
|
// Create AttachmentFields for each handler |
62
|
|
|
$attachmentFields = collect($handlers)->reduce(function (array $attachmentFields, SignatureHandler $handler) { |
63
|
|
|
|
64
|
|
|
$attachmentFields[] = AttachmentField::create($this->getFullCommand($handler), $handler->getDescription()); |
65
|
|
|
|
66
|
|
|
return $attachmentFields; |
67
|
|
|
}, []); |
68
|
|
|
|
69
|
|
|
return $this->respondToSlack("Available commands:") |
70
|
|
|
->withAttachment(Attachment::create() |
71
|
|
|
->setFields($attachmentFields) |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
protected function getFullCommand(SignatureHandler $handler): string |
77
|
|
|
{ |
78
|
|
|
$signatureParts = new SignatureParts($handler->signature); |
79
|
|
|
|
80
|
|
|
return '/' . $this->request->command . ' ' . $signatureParts->getHandlerName(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
protected function getAttachmentFieldForHandler(SignatureHandler $handler): AttachmentField |
84
|
|
|
{ |
85
|
|
|
$fullCommand = $this->getFullCommand($handler); |
86
|
|
|
|
87
|
|
|
$inputDefinition = $handler->getInputDefinition(); |
88
|
|
|
$output = new BufferedOutput(); |
89
|
|
|
|
90
|
|
|
$command = (new Command($fullCommand)) |
91
|
|
|
->setDefinition($inputDefinition) |
92
|
|
|
->setDescription($handler->getDescription()) |
93
|
|
|
; |
94
|
|
|
|
95
|
|
|
$descriptor = new DescriptorHelper(); |
96
|
|
|
$descriptor->describe($output, $command); |
97
|
|
|
|
98
|
|
|
return AttachmentField::create($fullCommand, $output->fetch()); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: