Skip to content

Instantly share code, notes, and snippets.

@jk
Created January 1, 2015 00:38
Show Gist options
  • Save jk/a69261c7f2aa0134b4ff to your computer and use it in GitHub Desktop.
Save jk/a69261c7f2aa0134b4ff to your computer and use it in GitHub Desktop.
<?php
class MockPhpStream
{
protected $index = 0;
protected $length = null;
protected $data = '';
public $context;
function __construct()
{
if (file_exists($this->buffer_filename())) {
$this->data = file_get_contents($this->buffer_filename());
} else {
$this->data = '';
}
$this->index = 0;
$this->length = strlen($this->data);
}
protected function buffer_filename()
{
return sys_get_temp_dir() . '\php_input.txt';
}
function stream_open($path, $mode, $options, &$opened_path)
{
return true;
}
function stream_close()
{
}
function stream_stat()
{
return array();
}
function stream_flush()
{
return true;
}
function stream_read($count)
{
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;
}
function stream_eof()
{
return ($this->index >= $this->length ? TRUE : FALSE);
}
function stream_write($data)
{
return file_put_contents($this->buffer_filename(), $data);
}
function unlink()
{
if (file_exists($this->buffer_filename())) {
unlink($this->buffer_filename());
}
$this->data = '';
$this->index = 0;
$this->length = 0;
}
}
$existed = in_array('php', stream_get_wrappers());
if ($existed) {
stream_wrapper_unregister('php');
}
stream_wrapper_register('php', 'MockPhpStream');
$input_data = 'test';
file_put_contents('php://input', $input_data);
$read_data = file_get_contents('php://input');
stream_wrapper_restore('php');
var_dump($read_data);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment