1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the phpflo/phpflo-flowtrace package. |
4
|
|
|
* |
5
|
|
|
* (c) Marc Aschmann <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
declare(strict_types=1); |
11
|
|
|
namespace PhpFlo; |
12
|
|
|
|
13
|
|
|
use PhpFlo\Common\AbstractNetworkDecorator; |
14
|
|
|
use PhpFlo\Common\NetworkDecoratorInterface; |
15
|
|
|
use PhpFlo\Common\NetworkInterface; |
16
|
|
|
use Psr\Log\LoggerInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Network for tracing events. |
20
|
|
|
* |
21
|
|
|
* @package PhpFlo |
22
|
|
|
* @author Marc Aschmann <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class TraceableNetwork extends AbstractNetworkDecorator implements NetworkDecoratorInterface |
25
|
|
|
{ |
26
|
|
|
const TYPE_DATA = 'data'; |
27
|
|
|
const TYPE_CONNECT = 'connect'; |
28
|
|
|
const TYPE_DISCONNECT = 'disconnect'; |
29
|
|
|
const TYPE_BEGIN_GROUP = 'begin.group'; |
30
|
|
|
const TYPE_END_GROUP = 'end.group'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
private $actions; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var LoggerInterface |
39
|
|
|
*/ |
40
|
|
|
private $logger; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* TraceableNetwork constructor. |
44
|
|
|
* |
45
|
|
|
* @param NetworkInterface $network |
46
|
|
|
* @param LoggerInterface $logger |
47
|
|
|
*/ |
48
|
2 |
|
public function __construct(NetworkInterface $network, LoggerInterface $logger) |
49
|
|
|
{ |
50
|
2 |
|
parent::__construct($network); |
51
|
|
|
|
52
|
2 |
|
$this->actions = [ |
53
|
2 |
|
self::TYPE_DATA => 'DATA', |
54
|
2 |
|
self::TYPE_CONNECT => 'CONN', |
55
|
2 |
|
self::TYPE_DISCONNECT => 'DISC' |
56
|
2 |
|
]; |
57
|
|
|
|
58
|
2 |
|
$this->logger = $logger; |
59
|
2 |
|
$this->network->hook(self::TYPE_DATA, 'flowtrace', $this->trace(self::TYPE_DATA)); |
60
|
2 |
|
$this->network->hook(self::TYPE_CONNECT, 'flowtrace', $this->trace(self::TYPE_CONNECT)); |
61
|
2 |
|
$this->network->hook(self::TYPE_DISCONNECT, 'flowtrace', $this->trace(self::TYPE_DISCONNECT)); |
62
|
2 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Wrap the creation of the callback |
66
|
|
|
* |
67
|
|
|
* @param string $type |
68
|
|
|
* @return \Closure |
69
|
|
|
*/ |
70
|
|
|
private function trace(string $type) : \Closure |
71
|
|
|
{ |
72
|
2 |
|
$trace = function() use ($type) { |
73
|
|
|
switch ($type) { |
74
|
|
|
case TraceableNetwork::TYPE_DATA: |
75
|
|
|
$this->traceData(func_get_args(), $type); |
76
|
|
|
break; |
77
|
|
|
case TraceableNetwork::TYPE_CONNECT: |
78
|
|
|
case TraceableNetwork::TYPE_DISCONNECT: |
79
|
|
|
$this->traceAction(func_get_args(), $type); |
80
|
|
|
break; |
81
|
|
|
} |
82
|
2 |
|
}; |
83
|
|
|
|
84
|
2 |
|
return $trace->bindTo($this); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param array $args |
89
|
|
|
* @param string $type |
90
|
|
|
*/ |
91
|
|
|
private function traceData(array $args, string $type) |
92
|
|
|
{ |
93
|
|
|
$data = $args[0]; |
94
|
|
|
$socket = $args[1]; |
95
|
|
|
|
96
|
|
|
if (!is_string($data)) { |
97
|
|
|
$data = serialize($data); |
98
|
|
|
} |
99
|
|
|
$to = $socket->to(); |
100
|
|
|
$message = "-> {$to['port']} {$to['process']['id']}"; |
101
|
|
|
|
102
|
|
|
$from = $socket->from(); |
103
|
|
View Code Duplication |
if (isset($from['process'])) { |
|
|
|
|
104
|
|
|
$message = " {$from['process']['id']} {$from['port']} {$message}"; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$this->logger->info("{$message} {$this->actions[$type]} {$data}"); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param array $args |
112
|
|
|
* @param string $type |
113
|
|
|
*/ |
114
|
|
|
private function traceAction(array $args, string $type) |
115
|
|
|
{ |
116
|
|
|
$socket = $args[0]; |
117
|
|
|
$to = $socket->to(); |
118
|
|
|
$message = "-> {$to['port']} {$to['process']['id']}"; |
119
|
|
|
|
120
|
|
|
$from = $socket->from(); |
121
|
|
View Code Duplication |
if (isset($from['process'])) { |
|
|
|
|
122
|
|
|
$message = " {$from['process']['id']} {$from['port']} {$message}"; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$this->logger->debug("{$message} {$this->actions[$type]}"); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.