1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the slince/spike package. |
5
|
|
|
* |
6
|
|
|
* (c) Slince <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Spike\Server\Listener; |
13
|
|
|
|
14
|
|
|
use Slince\EventDispatcher\Event; |
15
|
|
|
use Slince\EventDispatcher\SubscriberInterface; |
16
|
|
|
use Spike\Common\Logger\Logger; |
17
|
|
|
use Spike\Server\Event\Events; |
18
|
|
|
use Spike\Server\Event\FilterActionHandlerEvent; |
19
|
|
|
use Spike\Server\Server; |
20
|
|
|
|
21
|
|
|
class LoggerListener implements SubscriberInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var Server |
25
|
|
|
*/ |
26
|
|
|
protected $server; |
27
|
|
|
|
28
|
|
|
public function __construct(Server $server) |
29
|
|
|
{ |
30
|
|
|
$this->server = $server; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return Logger |
35
|
|
|
*/ |
36
|
|
|
protected function getLogger() |
37
|
|
|
{ |
38
|
|
|
return $this->server->getLogger(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public static function getSubscribedEvents() |
42
|
|
|
{ |
43
|
|
|
return [ |
44
|
|
|
Events::SERVER_RUN => 'onServerRun', |
45
|
|
|
Events::ACCEPT_CONNECTION => 'onAcceptConnection', |
46
|
|
|
Events::SERVER_ACTION => 'onReceiveMessage', |
47
|
|
|
Events::SOCKET_ERROR => 'onSocketError', |
48
|
|
|
Events::CONNECTION_ERROR => 'onConnectionError', |
49
|
|
|
]; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function onServerRun(Event $event) |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
$this->getLogger()->info('The server is running ...'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function onAcceptConnection(Event $event) |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
$this->getLogger()->info('Accepted a connection.'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function onReceiveMessage(FilterActionHandlerEvent $event) |
63
|
|
|
{ |
64
|
|
|
$this->getLogger()->info("Received a message:\r\n".$event->getMessage()); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function onSocketError(Event $event) |
68
|
|
|
{ |
69
|
|
|
$this->getLogger()->warning('Received a error: ' |
70
|
|
|
.$event->getArgument('exception')->getMessage()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function onConnectionError(Event $event) |
74
|
|
|
{ |
75
|
|
|
$this->getLogger()->warning(sprintf('Got a bad protocol message: "%s" from "%s"', |
76
|
|
|
$event->getArgument('exception')->getMessage(), |
77
|
|
|
$event->getArgument('connection')->getRemoteAddress() |
78
|
|
|
)); |
79
|
|
|
} |
80
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.