1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\VarDumper\Handler; |
6
|
|
|
|
7
|
|
|
use RuntimeException; |
8
|
|
|
use Yiisoft\VarDumper\HandlerInterface; |
9
|
|
|
|
10
|
|
|
final class StreamHandler implements HandlerInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var callable|null |
14
|
|
|
*/ |
15
|
|
|
private $encoder = null; |
16
|
|
|
/** |
17
|
|
|
* @var resource|null |
18
|
|
|
*/ |
19
|
|
|
private $stream = null; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var resource|string|null |
23
|
|
|
*/ |
24
|
|
|
private $uri = null; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param mixed|resource|string $uri |
28
|
|
|
*/ |
29
|
10 |
|
public function __construct( |
30
|
|
|
$uri = 'udp://127.0.0.1:8890' |
31
|
|
|
) { |
32
|
10 |
|
if (!is_string($uri) && !is_resource($uri)) { |
33
|
|
|
throw new \InvalidArgumentException( |
34
|
|
|
sprintf( |
35
|
|
|
'Argument $uri must be a string or a resource, "%s" given.', |
36
|
|
|
gettype($uri) |
37
|
|
|
) |
38
|
|
|
); |
39
|
|
|
} |
40
|
10 |
|
$this->uri = $uri; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Encodes with {@see self::$encoder} {@param $variable} and sends the result to the stream. |
|
|
|
|
45
|
|
|
*/ |
46
|
10 |
|
public function handle(mixed $variable, int $depth, bool $highlight = false): void |
47
|
|
|
{ |
48
|
10 |
|
$data = ($this->encoder ?? 'json_encode')($variable); |
49
|
10 |
|
if (!is_string($data)) { |
50
|
|
|
throw new RuntimeException( |
51
|
|
|
sprintf( |
52
|
|
|
'Encoder must return a string, %s returned.', |
53
|
|
|
gettype($data) |
54
|
|
|
) |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
10 |
|
$this->initializeStream(); |
59
|
|
|
|
60
|
10 |
|
if (!$this->writeToStream($data)) { |
61
|
|
|
$this->initializeStream(); |
62
|
|
|
|
63
|
|
|
$this->writeToStream($data); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
public function withEncoder(callable $encoder): HandlerInterface |
68
|
|
|
{ |
69
|
1 |
|
$new = clone $this; |
70
|
1 |
|
$new->encoder = $encoder; |
71
|
1 |
|
return $new; |
72
|
|
|
} |
73
|
|
|
|
74
|
10 |
|
private function initializeStream(): void |
75
|
|
|
{ |
76
|
10 |
|
if (is_string($this->uri)) { |
77
|
1 |
|
$this->stream = fsockopen($this->uri); |
78
|
|
|
} else { |
79
|
9 |
|
$this->stream = $this->uri; |
80
|
|
|
} |
81
|
|
|
|
82
|
10 |
|
if (!is_resource($this->stream)) { |
83
|
1 |
|
throw new RuntimeException('Cannot initialize a stream.'); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
10 |
|
private function writeToStream(string $data): bool |
88
|
|
|
{ |
89
|
10 |
|
if ($this->stream === null) { |
90
|
|
|
return false; |
91
|
|
|
} |
92
|
10 |
|
return @fwrite($this->stream, $data) !== false; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
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