|
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
|
|
|
|
|
11
|
|
|
namespace PhpFlo; |
|
12
|
|
|
|
|
13
|
|
|
use PhpFlo\Common\AbstractNetworkAdapter; |
|
14
|
|
|
use PhpFlo\Common\HookableNetworkInterface; |
|
15
|
|
|
use PhpFlo\Common\Network; |
|
|
|
|
|
|
16
|
|
|
use PhpFlo\Common\NetworkAdapterinterface; |
|
17
|
|
|
use PhpFlo\Common\NetworkInterface; |
|
18
|
|
|
use PhpFlo\Exception\FlowException; |
|
19
|
|
|
use PhpFlo\Exception\InvalidDefinitionException; |
|
20
|
|
|
use PhpFlo\Exception\InvalidTypeException; |
|
21
|
|
|
use Psr\Log\LoggerInterface; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Network for tracing events. |
|
25
|
|
|
* |
|
26
|
|
|
* @package PhpFlo |
|
27
|
|
|
* @author Marc Aschmann <[email protected]> |
|
28
|
|
|
*/ |
|
29
|
|
|
class TraceableNetwork extends AbstractNetworkAdapter implements NetworkAdapterinterface |
|
30
|
|
|
{ |
|
31
|
|
|
const TYPE_DATA = 'data'; |
|
32
|
|
|
const TYPE_CONNECT = 'connect'; |
|
33
|
|
|
const TYPE_DISCONNECT = 'disconnect'; |
|
34
|
|
|
const TYPE_BEGIN_GROUP = 'begin.group'; |
|
35
|
|
|
const TYPE_END_GROUP = 'end.group'; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var array |
|
39
|
|
|
*/ |
|
40
|
|
|
private $actions; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var LoggerInterface |
|
44
|
|
|
*/ |
|
45
|
|
|
private $logger; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* TraceableNetwork constructor. |
|
49
|
|
|
* |
|
50
|
|
|
* @param NetworkInterface $network |
|
51
|
|
|
* @param LoggerInterface $logger |
|
52
|
|
|
*/ |
|
53
|
2 |
|
public function __construct(NetworkInterface $network, LoggerInterface $logger) |
|
54
|
|
|
{ |
|
55
|
2 |
|
parent::__construct($network); |
|
56
|
|
|
|
|
57
|
2 |
|
$this->actions = [ |
|
58
|
2 |
|
self::TYPE_DATA => 'DATA', |
|
59
|
2 |
|
self::TYPE_CONNECT => 'CONN', |
|
60
|
2 |
|
self::TYPE_DISCONNECT => 'DISC' |
|
61
|
2 |
|
]; |
|
62
|
|
|
|
|
63
|
2 |
|
$this->logger = $logger; |
|
64
|
2 |
|
$this->network->hook(self::TYPE_DATA, 'flowtrace', $this->trace(self::TYPE_DATA)); |
|
65
|
2 |
|
$this->network->hook(self::TYPE_CONNECT, 'flowtrace', $this->trace(self::TYPE_CONNECT)); |
|
66
|
2 |
|
$this->network->hook(self::TYPE_DISCONNECT, 'flowtrace', $this->trace(self::TYPE_DISCONNECT)); |
|
67
|
2 |
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Wrap the creation of the callback |
|
71
|
|
|
* |
|
72
|
|
|
* @return \Closure |
|
73
|
|
|
*/ |
|
74
|
|
|
private function trace($type) |
|
75
|
|
|
{ |
|
76
|
2 |
|
$trace = function () use ($type) { |
|
77
|
|
|
switch ($type) { |
|
78
|
|
|
case TraceableNetwork::TYPE_DATA: |
|
79
|
|
|
$this->traceData(func_get_args(), $type); |
|
80
|
|
|
break; |
|
81
|
|
|
case TraceableNetwork::TYPE_CONNECT: |
|
82
|
|
|
case TraceableNetwork::TYPE_DISCONNECT: |
|
83
|
|
|
$this->traceAction(func_get_args(), $type); |
|
84
|
|
|
break; |
|
85
|
|
|
} |
|
86
|
2 |
|
}; |
|
87
|
|
|
|
|
88
|
2 |
|
return $trace->bindTo($this); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param array $args |
|
93
|
|
|
* @param string $type |
|
94
|
|
|
*/ |
|
95
|
|
|
private function traceData(array $args, $type) |
|
96
|
|
|
{ |
|
97
|
|
|
$data = $args[0]; |
|
98
|
|
|
$socket = $args[1]; |
|
99
|
|
|
|
|
100
|
|
|
if (!is_string($data)) { |
|
101
|
|
|
$data = serialize($data); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
$to = $socket->to(); |
|
105
|
|
|
$message = "-> {$to['port']} {$to['process']['id']} "; |
|
106
|
|
|
|
|
107
|
|
|
$from = $socket->from(); |
|
108
|
|
View Code Duplication |
if (isset($from['process'])) { |
|
|
|
|
|
|
109
|
|
|
$message = "{$from['process']['id']} {$from['port']} {$message}"; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
$this->logger->info("{$message} {$this->actions[$type]} {$data}"); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @param array $args |
|
117
|
|
|
* @param string $type |
|
118
|
|
|
*/ |
|
119
|
|
|
private function traceAction(array $args, $type) |
|
120
|
|
|
{ |
|
121
|
|
|
$socket = $args[0]; |
|
122
|
|
|
|
|
123
|
|
|
$to = $socket->to(); |
|
124
|
|
|
$message = "-> {$to['port']} {$to['process']['id']} "; |
|
125
|
|
|
|
|
126
|
|
|
$from = $socket->from(); |
|
127
|
|
View Code Duplication |
if (isset($from['process'])) { |
|
|
|
|
|
|
128
|
|
|
$message = "{$from['process']['id']} {$from['port']} {$message}"; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
$this->logger->debug("{$message} {$this->actions[$type]}"); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes 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: