|
1
|
|
|
<?php |
|
2
|
|
|
namespace PortlandLabs\Slackbot\Command; |
|
3
|
|
|
|
|
4
|
|
|
use PortlandLabs\Slackbot\Slack\Rtm\Event\Message; |
|
5
|
|
|
use Psr\Container\ContainerInterface; |
|
6
|
|
|
use Psr\Log\LoggerAwareTrait; |
|
7
|
|
|
use Psr\Log\LoggerInterface; |
|
8
|
|
|
|
|
9
|
|
|
class Manager |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
use LoggerAwareTrait; |
|
13
|
|
|
|
|
14
|
|
|
/** @var Command[] */ |
|
15
|
|
|
protected $commands = []; |
|
16
|
|
|
|
|
17
|
|
|
/** @var ContainerInterface */ |
|
18
|
|
|
protected $container; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct(ContainerInterface $container, LoggerInterface $logger) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->container = $container; |
|
23
|
|
|
$this->setLogger($logger); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Get all commands |
|
28
|
|
|
* |
|
29
|
|
|
* @return Command[] |
|
30
|
|
|
*/ |
|
31
|
|
|
public function all(): iterable |
|
32
|
|
|
{ |
|
33
|
|
|
foreach ($this->commands as $key => $command) { |
|
34
|
|
|
if (is_string($command)) { |
|
35
|
|
|
$command = $this->container->get($command); |
|
36
|
|
|
$this->commands[$key] = $command; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
yield $key => $command; |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Add a command to this manager |
|
45
|
|
|
* |
|
46
|
|
|
* @param Command|string $command |
|
47
|
|
|
* |
|
48
|
|
|
* @return Manager |
|
49
|
|
|
*/ |
|
50
|
|
|
public function addCommand($command): Manager |
|
51
|
|
|
{ |
|
52
|
|
|
$this->commands[] = $command; |
|
53
|
|
|
return $this; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Handle a message |
|
58
|
|
|
* |
|
59
|
|
|
* @param Message $message |
|
60
|
|
|
*/ |
|
61
|
|
|
public function handle(Message $message) |
|
62
|
|
|
{ |
|
63
|
|
|
foreach ($this->all() as $command) { |
|
64
|
|
|
if ($command->shouldHandle($message)) { |
|
65
|
|
|
$this->runCommand($command, $message); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Run a command |
|
72
|
|
|
* |
|
73
|
|
|
* @param Command $command |
|
74
|
|
|
* @param Message $message |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function runCommand(Command $command, Message $message) |
|
77
|
|
|
{ |
|
78
|
|
|
$commandClass = basename(get_class($command)); |
|
79
|
|
|
$this->logger->debug("[CMD !! ] Matched message to command '$commandClass'"); |
|
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
$command->handle($message); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
} |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.