Skip to content

Commit 966665b

Browse files
committed
A few updates based on dogfooding
1 parent c89462e commit 966665b

3 files changed

Lines changed: 92 additions & 35 deletions

File tree

src/Commands/CommandRouter.php

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@
2222
*/
2323
class CommandRouter implements Listener
2424
{
25-
/** @var array<string, Listener> */
26-
private array $routes;
25+
private Listener $default;
2726
private string $description;
2827
private int $maxLevels;
28+
/** @var array<string, Listener> */
29+
private array $routes;
2930

3031
public static function new(): self
3132
{
@@ -37,21 +38,37 @@ public function __construct(array $routes = [])
3738
$this->routes = [];
3839
$this->description = '';
3940
$this->maxLevels = 1;
40-
$this->add('list', Closure::fromCallable([$this, 'listSubCommands']));
41+
$this->add('help', Closure::fromCallable([$this, 'showHelp']));
4142
foreach ($routes as $subCommand => $listener) {
4243
$this->add($subCommand, $listener);
4344
}
4445
}
4546

4647
/**
4748
* @param string $subCommand
48-
* @param Listener|callable|class-string $listener
49+
* @param Listener|callable(Context): void|class-string $listener
4950
* @return $this
5051
*/
5152
public function add(string $subCommand, $listener): self
5253
{
53-
$this->routes[$subCommand] = Coerce::listener($listener);
54-
$this->maxLevels = max($this->maxLevels, count(explode(' ', $subCommand)));
54+
$listener = Coerce::listener($listener);
55+
if ($subCommand === '*') {
56+
$this->default = $listener;
57+
} else {
58+
$this->routes[$subCommand] = $listener;
59+
$this->maxLevels = max($this->maxLevels, count(explode(' ', $subCommand)));
60+
}
61+
62+
return $this;
63+
}
64+
65+
/**
66+
* @param Listener|callable(Context): void|class-string $listener
67+
* @return $this
68+
*/
69+
public function withDefault($listener): self
70+
{
71+
$this->default = Coerce::listener($listener);
5572

5673
return $this;
5774
}
@@ -60,7 +77,7 @@ public function add(string $subCommand, $listener): self
6077
* @param string $description
6178
* @return self
6279
*/
63-
public function description(string $description): self
80+
public function withDescription(string $description): self
6481
{
6582
$this->description = $description;
6683

@@ -85,31 +102,35 @@ public function handle(Context $context): void
85102
array_pop($nameArgs);
86103
}
87104

88-
$context->logger()->debug('CommandRouter could not find sub-command; routing to "list" instead');
89-
$this->listSubCommands($context);
105+
if ($this->default) {
106+
$this->default->handle($context);
107+
} else {
108+
$this->showHelp($context);
109+
}
90110
}
91111

92-
private function listSubCommands(Context $ctx): void
112+
private function showHelp(Context $context): void
93113
{
94-
$cmd = $ctx->payload()->get('command');
95-
$fmt = $ctx->fmt();
96-
$msg = $ctx->blocks()->message()->header("The {$cmd} Command");
114+
$cmd = $context->payload()->get('command');
115+
$fmt = $context->fmt();
116+
$msg = $context->blocks()->message();
117+
118+
$msg->header("The {$cmd} Command");
97119
if ($this->description) {
98120
$msg->text($this->description);
99121
}
100122

101123
$routes = array_keys($this->routes);
102124
natsort($routes);
103-
104125
$msg->newSection()->mrkdwnText($fmt->lines([
105-
'*Available commands*:',
126+
'*Available sub-commands*:',
106127
$fmt->bulletedList(array_map(fn (string $subCommand) => $fmt->code("{$cmd} {$subCommand}"), $routes))
107128
]));
108129

109-
if ($ctx->isAcknowledged()) {
110-
$ctx->respond($msg);
130+
if ($context->isAcknowledged()) {
131+
$context->respond($msg);
111132
} else {
112-
$ctx->ack($msg);
133+
$context->ack($msg);
113134
}
114135
}
115136
}

src/Context.php

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,28 @@ public function getAppId(): ?string
221221
return $this->appId;
222222
}
223223

224+
public function getAppConfig(): AppConfig
225+
{
226+
if (!isset($this->appConfig)) {
227+
$this->appConfig = new AppConfig();
228+
}
229+
230+
return $this->appConfig;
231+
}
232+
233+
public function getApiClient(): ApiClient
234+
{
235+
if (!isset($this->apiClient)) {
236+
$tokenStore = $this->getAppConfig()->getTokenStore();
237+
$this->apiClient = new SimpleApiClient($tokenStore->get(
238+
$this->payload->getTeamId(),
239+
$this->payload->getEnterpriseId()
240+
));
241+
}
242+
243+
return $this->apiClient;
244+
}
245+
224246
public function getAck(): ?string
225247
{
226248
return $this->ack;
@@ -258,14 +280,7 @@ public function logger(): SlackLogger
258280
*/
259281
public function api(string $api, array $params): array
260282
{
261-
if (!isset($this->apiClient)) {
262-
$this->apiClient = new SimpleApiClient($this->getAppConfig()->getTokenStore()->get(
263-
$this->payload->getTeamId(),
264-
$this->payload->getEnterpriseId()
265-
));
266-
}
267-
268-
return $this->apiClient->call($api, $params);
283+
return $this->getApiClient()->call($api, $params);
269284
}
270285

271286
public function blocks(): Blocks
@@ -457,15 +472,6 @@ public function offsetUnset($offset)
457472
$this->set($offset, null);
458473
}
459474

460-
private function getAppConfig(): AppConfig
461-
{
462-
if (!isset($this->appConfig)) {
463-
$this->appConfig = new AppConfig();
464-
}
465-
466-
return $this->appConfig;
467-
}
468-
469475
private function reconcileAppId(): void
470476
{
471477
$this->appId ??= $this->appConfig->getId();

src/Listeners/WIP.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SlackPhp\Framework\Listeners;
6+
7+
use SlackPhp\Framework\{Context, Listener};
8+
use SlackPhp\Framework\Contexts\PayloadType;
9+
10+
/**
11+
* Simple listener that displays/logs a "Work in progress" message in whichever medium makes the most sense.
12+
*/
13+
class WIP implements Listener
14+
{
15+
public function handle(Context $context): void
16+
{
17+
$message = 'Work in progress';
18+
if ($context->payload()->isType(PayloadType::command())) {
19+
$context->ack($message);
20+
} elseif ($context->payload()->get('view')) {
21+
$context->view()->push($message);
22+
} elseif ($context->payload()->get('trigger_id')) {
23+
$context->modals()->open($message);
24+
} elseif ($context->payload()->getResponseUrl()) {
25+
$context->respond($message);
26+
} else {
27+
$context->logger()->debug($message);
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)