|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\VarDumper\Handler; |
|
6
|
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
use RuntimeException; |
|
9
|
|
|
use Socket; |
|
10
|
|
|
use Yiisoft\VarDumper\HandlerInterface; |
|
11
|
|
|
|
|
12
|
|
|
use function fsockopen; |
|
13
|
|
|
use function fwrite; |
|
14
|
|
|
use function get_debug_type; |
|
15
|
|
|
use function is_resource; |
|
16
|
|
|
use function is_string; |
|
17
|
|
|
|
|
18
|
|
|
final class StreamHandler implements HandlerInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var callable|null |
|
22
|
|
|
*/ |
|
23
|
|
|
private $encoder = null; |
|
24
|
|
|
/** |
|
25
|
|
|
* @var resource|Socket|null |
|
26
|
|
|
*/ |
|
27
|
|
|
private $stream = null; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var resource|Socket|string|null |
|
31
|
|
|
*/ |
|
32
|
|
|
private $uri = null; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param mixed|resource|string $uri |
|
36
|
|
|
*/ |
|
37
|
16 |
|
public function __construct( |
|
38
|
|
|
mixed $uri = 'udp://127.0.0.1:8890' |
|
39
|
|
|
) { |
|
40
|
16 |
|
if (!is_string($uri) && !is_resource($uri) && !$uri instanceof Socket) { |
|
41
|
1 |
|
throw new InvalidArgumentException( |
|
42
|
1 |
|
sprintf( |
|
43
|
1 |
|
'Argument $uri must be a string or a resource, "%s" given.', |
|
44
|
1 |
|
get_debug_type($uri) |
|
45
|
1 |
|
) |
|
46
|
1 |
|
); |
|
47
|
|
|
} |
|
48
|
15 |
|
$this->uri = $uri; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Encodes with {@see self::$encoder} {@param $variable} and sends the result to the stream. |
|
|
|
|
|
|
53
|
|
|
*/ |
|
54
|
14 |
|
public function handle(mixed $variable, int $depth, bool $highlight = false): void |
|
55
|
|
|
{ |
|
56
|
14 |
|
$data = ($this->encoder ?? '\json_encode')($variable); |
|
57
|
14 |
|
if (!is_string($data)) { |
|
58
|
1 |
|
throw new RuntimeException( |
|
59
|
1 |
|
sprintf( |
|
60
|
1 |
|
'Encoder must return a string, "%s" returned.', |
|
61
|
1 |
|
get_debug_type($data) |
|
62
|
1 |
|
) |
|
63
|
1 |
|
); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
13 |
|
$this->initializeStream(); |
|
67
|
|
|
|
|
68
|
13 |
|
if (!$this->writeToStream($data)) { |
|
69
|
|
|
$this->initializeStream(); |
|
70
|
|
|
|
|
71
|
|
|
$this->writeToStream($data); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
3 |
|
public function withEncoder(callable $encoder): HandlerInterface |
|
76
|
|
|
{ |
|
77
|
3 |
|
$new = clone $this; |
|
78
|
3 |
|
$new->encoder = $encoder; |
|
79
|
3 |
|
return $new; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
13 |
|
private function initializeStream(): void |
|
83
|
|
|
{ |
|
84
|
13 |
|
if (is_string($this->uri)) { |
|
85
|
|
|
if ( |
|
86
|
3 |
|
str_starts_with($this->uri, 'udp://') || |
|
87
|
3 |
|
str_starts_with($this->uri, 'udg://') || |
|
88
|
1 |
|
str_starts_with($this->uri, 'tcp://') || |
|
89
|
3 |
|
str_starts_with($this->uri, 'unix://') |
|
90
|
|
|
) { |
|
91
|
2 |
|
$this->stream = fsockopen($this->uri); |
|
92
|
|
|
} else { |
|
93
|
3 |
|
$this->stream = fopen($this->uri, 'w+'); |
|
94
|
|
|
} |
|
95
|
|
|
} else { |
|
96
|
10 |
|
$this->stream = $this->uri; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
13 |
|
if (!is_resource($this->stream)) { |
|
100
|
1 |
|
throw new RuntimeException('Cannot initialize a stream.'); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
13 |
|
private function writeToStream(string $data): bool |
|
105
|
|
|
{ |
|
106
|
13 |
|
if (!is_resource($this->stream)) { |
|
107
|
|
|
return false; |
|
108
|
|
|
} |
|
109
|
13 |
|
return @fwrite($this->stream, $data) !== false; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
15 |
|
public function __destruct() |
|
113
|
|
|
{ |
|
114
|
15 |
|
if (!is_string($this->uri) || !is_resource($this->stream)) { |
|
115
|
13 |
|
return; |
|
116
|
|
|
} |
|
117
|
3 |
|
fclose($this->stream); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths