1
|
|
|
<?php |
2
|
|
|
namespace PortlandLabs\Slackbot\Command; |
3
|
|
|
|
4
|
|
|
use Illuminate\Support\Str; |
5
|
|
|
use PortlandLabs\Slackbot\Bot; |
6
|
|
|
use PortlandLabs\Slackbot\Command\Argument\Manager; |
|
|
|
|
7
|
|
|
use PortlandLabs\Slackbot\Permission\Checker; |
8
|
|
|
use PortlandLabs\Slackbot\Slack\Rtm\Event\Message; |
9
|
|
|
use Psr\SimpleCache\CacheInterface; |
10
|
|
|
use Psr\SimpleCache\CacheException; |
11
|
|
|
|
12
|
|
|
class RecallCommand extends SimpleCommand |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
protected $description = 'Recall things that have been `remember`ed'; |
16
|
|
|
|
17
|
|
|
protected $signature = 'recall {thing?} {--l|list : List the things you can recall}'; |
18
|
|
|
|
19
|
|
|
/** @var CacheInterface */ |
20
|
|
|
protected $cache; |
21
|
|
|
|
22
|
|
|
public function __construct(Bot $bot, Manager $argumentManager, Checker $checker, CacheInterface $cache) |
23
|
|
|
{ |
24
|
|
|
parent::__construct($bot, $argumentManager, $checker); |
25
|
|
|
|
26
|
|
|
$this->cache = $cache; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param Message $message |
31
|
|
|
* @param Manager $manager |
32
|
|
|
* |
33
|
|
|
* @throws \CL\Slack\Exception\SlackException |
34
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
35
|
|
|
*/ |
36
|
|
|
public function run(Message $message, Manager $manager) |
37
|
|
|
{ |
38
|
|
|
if ($manager->get('list')) { |
39
|
|
|
$this->outputList($message); |
40
|
|
|
return; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$thing = $manager->get('thing'); |
44
|
|
|
$key = Str::snake('remember ' . $thing); |
45
|
|
|
|
46
|
|
|
try { |
47
|
|
|
$value = $this->cache->get($key); |
48
|
|
|
} catch (CacheException $e) { |
49
|
|
|
$this->bot->logger()->error('[CMD.REC] Failed to recall: ' . $e->getMessage()); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if (!$value) { |
53
|
|
|
$this->bot->feignTyping($message->getChannel(), 'Hmm.. I\'m not able to recall that...'); |
|
|
|
|
54
|
|
|
return; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$value = str_replace("\n", "\n> ", $value); |
58
|
|
|
$api = $this->bot->api(); |
59
|
|
|
$api->getBuilder() |
60
|
|
|
->send("Here's what I remember:\n> " . $value)->to($message->getChannel()) |
61
|
|
|
->withIcon(':thinking_face:') |
62
|
|
|
->execute($api); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
protected function outputList(Message $message) |
66
|
|
|
{ |
67
|
|
|
$out = []; |
68
|
|
|
foreach ($this->cache->get('rememberindex', []) as $key => $thing) { |
69
|
|
|
$out[] = "`$thing`"; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$last = null; |
73
|
|
|
if (count($out) > 1) { |
74
|
|
|
$last = array_pop($out); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$result = 'I can recall ' . implode(', ', $out); |
78
|
|
|
|
79
|
|
|
if ($last) { |
80
|
|
|
$result .= ' or ' . $last; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$this->bot->feignTyping($message->getChannel(), $result); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
} |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: