Repository navigation
Expand file tree
/
Copy pathEncoder.php
More file actions
144 lines (120 loc) · 4.38 KB
/
Copy pathEncoder.php
File metadata and controls
144 lines (120 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
namespace Clue\React\NDJson;
use Evenement\EventEmitter;
use React\Stream\WritableStreamInterface;
/**
* The Encoder / Serializer can be used to write any value, encode it as a JSON text and forward it to an output stream
*/
class Encoder extends EventEmitter implements WritableStreamInterface
{
private $output;
private $options;
private $depth;
private $closed = false;
/**
* @param WritableStreamInterface $output
* @param int $options
* @param int $depth (requires PHP 5.5+)
* @throws \InvalidArgumentException
* @throws \BadMethodCallException
*/
public function __construct(WritableStreamInterface $output, $options = 0, $depth = 512)
{
// @codeCoverageIgnoreStart
if (\defined('JSON_PRETTY_PRINT') && $options & \JSON_PRETTY_PRINT) {
throw new \InvalidArgumentException('Pretty printing not available for NDJSON');
}
if ($depth !== 512 && \PHP_VERSION < 5.5) {
throw new \BadMethodCallException('Depth parameter is only supported on PHP 5.5+');
}
if (\defined('JSON_THROW_ON_ERROR')) {
$options = $options & ~\JSON_THROW_ON_ERROR;
}
// @codeCoverageIgnoreEnd
$this->output = $output;
if (!$output->isWritable()) {
$this->close();
return;
}
$this->options = $options;
$this->depth = $depth;
$this->output->on('drain', array($this, 'handleDrain'));
$this->output->on('error', array($this, 'handleError'));
$this->output->on('close', array($this, 'close'));
}
public function write($data)
{
if ($this->closed) {
return false;
}
// we have to handle PHP warnings for legacy PHP < 5.5
// certain values (such as INF etc.) emit a warning, but still encode successfully
// @codeCoverageIgnoreStart
if (\PHP_VERSION_ID < 50500) {
$errstr = null;
\set_error_handler(function ($_, $error) use (&$errstr) {
$errstr = $error;
});
// encode data with options given in ctor (depth not supported)
$data = \json_encode($data, $this->options);
// always check error code and match missing error messages
\restore_error_handler();
$errno = \json_last_error();
if (\defined('JSON_ERROR_UTF8') && $errno === \JSON_ERROR_UTF8) {
// const JSON_ERROR_UTF8 added in PHP 5.3.3, but no error message assigned in legacy PHP < 5.5
// this overrides PHP 5.3.14 only: https://3v4l.org/IGP8Z#v5314
$errstr = 'Malformed UTF-8 characters, possibly incorrectly encoded';
} elseif ($errno !== \JSON_ERROR_NONE && $errstr === null) {
// error number present, but no error message applicable
$errstr = 'Unknown error';
}
// abort stream if encoding fails
if ($errno !== \JSON_ERROR_NONE || $errstr !== null) {
$this->handleError(new \RuntimeException('Unable to encode JSON: ' . $errstr, $errno));
return false;
}
} else {
// encode data with options given in ctor
$data = \json_encode($data, $this->options, $this->depth);
// abort stream if encoding fails
if ($data === false && \json_last_error() !== \JSON_ERROR_NONE) {
$this->handleError(new \RuntimeException('Unable to encode JSON: ' . \json_last_error_msg(), \json_last_error()));
return false;
}
}
// @codeCoverageIgnoreEnd
return $this->output->write($data . "\n");
}
public function end($data = null)
{
if ($data !== null) {
$this->write($data);
}
$this->output->end();
}
public function isWritable()
{
return !$this->closed;
}
public function close()
{
if ($this->closed) {
return;
}
$this->closed = true;
$this->output->close();
$this->emit('close');
$this->removeAllListeners();
}
/** @internal */
public function handleDrain()
{
$this->emit('drain');
}
/** @internal */
public function handleError(\Exception $error)
{
$this->emit('error', array($error));
$this->close();
}
}