|
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
|
|
|
private $logger; |
|
32
|
|
|
|
|
33
|
2 |
|
public function __construct(NetworkInterface $network, LoggerInterface $logger) |
|
34
|
|
|
{ |
|
35
|
2 |
|
parent::__construct($network); |
|
36
|
|
|
|
|
37
|
2 |
|
$this->logger = $logger; |
|
38
|
2 |
|
$this->network->hook('data', 'flowtrace', $this->trace()); |
|
39
|
2 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Wrap the creation of the callback |
|
43
|
|
|
* |
|
44
|
|
|
* @return \Closure |
|
45
|
|
|
*/ |
|
46
|
|
|
private function trace() |
|
47
|
|
|
{ |
|
48
|
2 |
|
$trace = function ($data, $socket) { |
|
49
|
|
|
if (!is_string($data)) { |
|
50
|
|
|
$data = serialize($data); |
|
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$to = $socket->to(); |
|
54
|
|
|
$message = "-> {$to['port']} {$to['process']}"; |
|
55
|
|
|
|
|
56
|
|
|
$from = $socket->from(); |
|
57
|
|
|
if (isset($from['process'])) { |
|
58
|
|
|
$message = "{$from['process']} {$from['port']} " . $message; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$this->logger->debug($message); |
|
62
|
2 |
|
}; |
|
63
|
|
|
|
|
64
|
2 |
|
return $trace->bindTo($this); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
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: