1 | <?php |
||
21 | class Yabot |
||
22 | { |
||
23 | use LogTrait; |
||
24 | use LoopTrait; |
||
25 | use SlackTrait; |
||
26 | use ConfigTrait; |
||
27 | |||
28 | /** @var MessageFactory */ |
||
29 | private $messageFactory; |
||
30 | |||
31 | /** @var PluginManager */ |
||
32 | private $pluginManager; |
||
33 | |||
34 | private $messageLog; |
||
35 | |||
36 | public function __construct( |
||
37 | LoggerInterface $logger, |
||
38 | LoopInterface $eventLoop, |
||
39 | Client $slackClient, |
||
40 | MessageFactory $messageFactory, |
||
41 | PluginManager $pluginManager, |
||
42 | array $config = [] |
||
43 | ) { |
||
44 | $this->setLog($logger); |
||
45 | $this->setLoop($eventLoop); |
||
46 | $this->setSlack($slackClient); |
||
47 | $this->setConfig($config); |
||
48 | $this->messageFactory = $messageFactory; |
||
49 | $this->pluginManager = $pluginManager; |
||
50 | $this->messageLog = null; |
||
51 | } |
||
52 | |||
53 | public function getMessageLog() |
||
54 | { |
||
55 | return $this->messageLog; |
||
56 | } |
||
57 | |||
58 | public function setMessageLog(string $messageLog = null) |
||
59 | { |
||
60 | $this->messageLog = $messageLog ?? null; |
||
61 | } |
||
62 | |||
63 | public function init(array $plugins) |
||
64 | { |
||
65 | foreach ($plugins as $pluginId => $plugin) { |
||
66 | /** @var PluginInterface $plugin */ |
||
67 | |||
68 | $this->info("loading $pluginId"); |
||
69 | |||
70 | try { |
||
71 | $this->pluginManager->loadPlugin($pluginId, $plugin); |
||
72 | } catch (Exception $e) { |
||
73 | $this->warning("Unhandled Exception while loading $pluginId: ".$e->getMessage()); |
||
74 | $this->warning($e->getTraceAsString()); |
||
75 | } |
||
76 | } |
||
77 | } |
||
78 | |||
79 | public function run() |
||
80 | { |
||
81 | $slack = $this->getSlack(); |
||
82 | |||
83 | $slack->init(); |
||
84 | |||
85 | $slack->connect()->then([$this, 'connected']); |
||
86 | |||
87 | $this->addMemoryReporting(); |
||
88 | |||
89 | $this->getLoop()->run(); |
||
90 | } |
||
91 | |||
92 | |||
93 | public function shutDown() |
||
94 | { |
||
95 | $this->getSlack()->disconnect(); |
||
96 | $this->getLoop()->stop(); |
||
97 | } |
||
98 | |||
99 | public function connected() |
||
100 | { |
||
101 | $slack = $this->getSlack(); |
||
102 | |||
103 | $slack->update(function(User $authedUser) { |
||
104 | $this->pluginManager->setAuthedUser($authedUser); |
||
105 | }); |
||
106 | |||
107 | $slack->onEvent('message', [$this, 'onMessage']); |
||
108 | |||
109 | $this->startConnectionMonitor(); |
||
110 | } |
||
111 | |||
112 | public function onMessage(Payload $payload) |
||
113 | { |
||
114 | $data = $payload->getData(); |
||
115 | |||
116 | $this->debug('Received message', $data); |
||
117 | |||
118 | try { |
||
119 | $this->logMessage($data); |
||
120 | $message = $this->messageFactory->create($data); |
||
121 | } catch (Throwable $throwable) { |
||
122 | $errmsg = $throwable->getMessage()."\n" |
||
123 | .$throwable->getTraceAsString()."\n" |
||
124 | ."Payload data: ".json_encode($data); |
||
125 | $this->warning($errmsg); |
||
126 | return; |
||
127 | } |
||
128 | |||
129 | if ($message->isSelf()) { |
||
130 | return; |
||
131 | } |
||
132 | |||
133 | $this->pluginManager->dispatchMessage($message); |
||
134 | } |
||
135 | |||
136 | public function getHelp() : string |
||
137 | { |
||
138 | return implode("\n", $this->pluginManager->getHelp()); |
||
139 | } |
||
140 | |||
141 | public function getStatus() : string |
||
142 | { |
||
143 | $statuses = $this->pluginManager->getStatuses(); |
||
144 | |||
145 | array_unshift($statuses, $this->getFormattedMemoryUsage()); |
||
146 | |||
147 | return implode("\n", $statuses); |
||
148 | } |
||
149 | |||
150 | protected function addMemoryReporting() |
||
164 | |||
165 | protected function getFormattedMemoryUsage() : string |
||
171 | |||
172 | protected function logMessage($data) |
||
173 | { |
||
174 | if ($this->messageLog !== null) { |
||
175 | file_put_contents($this->messageLog, json_encode($data) . "\n", FILE_APPEND); |
||
176 | } |
||
177 | } |
||
178 | |||
179 | protected function startConnectionMonitor() |
||
200 | } |
||
201 |