-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
StreamWrapperPHP.php
89 lines (75 loc) · 1.97 KB
/
StreamWrapperPHP.php
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
<?php
namespace Tests\Integration\Behaviour\Features\Context\Util;
/**
* Allows to mock the php://input stream
* Without this stream, we can't write on the php://input stream because it's readyonly
*
* @example
* stream_wrapper_unregister('php');
* stream_register_wrapper('php', StreamWrapperPHP::class);
* file_put_contents('php://input', 'xml=' . $xmlVariable);
*/
class StreamWrapperPHP
{
/**
* @var int
*/
protected $index = 0;
protected $length = null;
protected $data = '';
public $context;
public function __construct()
{
if (file_exists($this->buffer_filename())) {
$this->data = file_get_contents($this->buffer_filename());
}
$this->index = 0;
$this->length = strlen($this->data);
}
protected function buffer_filename(): string
{
return sys_get_temp_dir() . '/php_input.txt';
}
public function stream_open($path, $mode, $options, &$opened_path): bool
{
return true;
}
public function stream_close()
{
}
public function stream_stat(): array
{
return [];
}
public function stream_flush(): bool
{
return true;
}
public function stream_read(int $count): string
{
if (is_null($this->length) === true) {
$this->length = strlen($this->data);
}
$length = min($count, $this->length - $this->index);
$data = substr($this->data, $this->index);
$this->index = $this->index + $length;
return $data;
}
public function stream_eof()
{
return $this->index >= $this->length;
}
public function stream_write($data)
{
return file_put_contents($this->buffer_filename(), $data);
}
public function unlink()
{
if (file_exists($this->buffer_filename())) {
unlink($this->buffer_filename());
}
$this->data = '';
$this->index = 0;
$this->length = 0;
}
}