Skip to content

Commit

Permalink
Add Wrapper::wrap() convenience method
Browse files Browse the repository at this point in the history
also fix some test class names
  • Loading branch information
icewind1991 committed Jul 23, 2014
1 parent acb8b69 commit a19beaf
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 50 deletions.
29 changes: 14 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,20 @@ stream_wrapper_register('callback', '\Icewind\Streams\CallbackWrapper');
// get an existing stream to wrap
$source = fopen('php://temp', 'r+');

$context = stream_context_create(array(
'callback' => array(
'source' => $source,
'read' => function ($count) {
echo "read " . $count . "bytes\n";
},
'write' => function ($data) {
echo "wrote '" . $data . "'\n";
},
'close' => function () {
echo "stream closed\n";
}
)
));
$stream = fopen('callback://', 'r+', false, $context);
// register the callbacks
$stream = CallbackWrapper::wrap($source,
// read callback
function ($count) {
echo "read " . $count . "bytes\n";
},
// write callback
function ($data) {
echo "wrote '" . $data . "'\n";
},
// close callback
function () {
echo "stream closed\n";
});

fwrite($stream, 'some dummy data');

Expand Down
24 changes: 24 additions & 0 deletions src/CallbackWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,30 @@ class CallBackWrapper extends Wrapper {
*/
protected $closeCallback;

/**
* Wraps a stream with the provided callbacks
*
* @param resource $source
* @param callable $read (optional)
* @param callable $write (optional)
* @param callable $close (optional)
* @return resource
*/
public static function wrap($source, $read = null, $write = null, $close = null) {
stream_wrapper_register('callback', '\Icewind\Streams\CallbackWrapper');
$context = stream_context_create(array(
'callback' => array(
'source' => $source,
'read' => $read,
'write' => $write,
'close' => $close
)
));
$wrapped = fopen('callback://', 'r+', false, $context);
stream_wrapper_unregister('callback');
return $wrapped;
}

public function stream_open() {
$context = $this->loadContext('callback');

Expand Down
20 changes: 20 additions & 0 deletions src/NullWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,27 @@

namespace Icewind\Streams;

/**
* Stream wrapper that does nothing, used for tests
*/
class NullWrapper extends Wrapper {
/**
* Wraps a stream with the provided callbacks
*
* @param resource $source
* @return resource
*/
public static function wrap($source) {
stream_wrapper_register('null', '\Icewind\Streams\NullWrapper');
$context = stream_context_create(array(
'null' => array(
'source' => $source)
));
$wrapped = fopen('null://', 'r+', false, $context);
stream_wrapper_unregister('null');
return $wrapped;
}

public function stream_open() {
$this->loadContext('null');
return true;
Expand Down
20 changes: 2 additions & 18 deletions tests/CallbackWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,7 @@

namespace Icewind\Streams\Tests;

class CallableWrapper extends Wrapper {

public function setUp() {
stream_wrapper_register('callback', '\Icewind\Streams\CallbackWrapper');
}

public function tearDown() {
stream_wrapper_unregister('callback');
}
class CallBackWrapper extends Wrapper {

/**
* @param resource $source
Expand All @@ -25,15 +17,7 @@ public function tearDown() {
* @return resource
*/
protected function wrapSource($source, $read = null, $write = null, $close = null) {
$context = stream_context_create(array(
'callback' => array(
'source' => $source,
'read' => $read,
'write' => $write,
'close' => $close
)
));
return fopen('callback://', 'r+', false, $context);
return \Icewind\Streams\CallbackWrapper::wrap($source, $read, $write, $close);
}

public function testReadCallback() {
Expand Down
34 changes: 17 additions & 17 deletions tests/NullWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,46 @@

namespace Icewind\Streams\Tests;

class NullWrapperTest extends Wrapper {

public function setUp() {
stream_wrapper_register('null', '\Icewind\Streams\NullWrapper');
}

public function tearDown() {
stream_wrapper_unregister('null');
}
class NullWrapper extends Wrapper {

/**
* @param resource $source
* @return resource
*/
protected function wrapSource($source) {
$context = stream_context_create(array(
'null' => array(
'source' => $source
)
));
return fopen('null://', 'r+', false, $context);
return \Icewind\Streams\NullWrapper::wrap($source);
}

/**
* @expectedException \BadMethodCallException
*/
public function testNoContext() {
stream_wrapper_register('null', '\Icewind\Streams\NullWrapper');
$context = stream_context_create(array());
fopen('null://', 'r+', false, $context);
try {
fopen('null://', 'r+', false, $context);
stream_wrapper_unregister('null');
} catch (\Exception $e) {
stream_wrapper_unregister('null');
throw $e;
}
}

/**
* @expectedException \BadMethodCallException
*/
public function testNoSource() {
stream_wrapper_register('null', '\Icewind\Streams\NullWrapper');
$context = stream_context_create(array(
'null' => array(
'source' => 'bar'
)
));
fopen('null://', 'r+', false, $context);
try {
fopen('null://', 'r+', false, $context);
} catch (\Exception $e) {
stream_wrapper_unregister('null');
throw $e;
}
}
}

0 comments on commit a19beaf

Please sign in to comment.