Clean native stream wrapping

This commit is contained in:
Robin Appelman 2014-07-26 18:41:58 +02:00
commit 8d41d7a97b
3 changed files with 26 additions and 59 deletions

View file

@ -25,11 +25,6 @@ class NativeShare implements IShare {
*/ */
private $state; private $state;
/**
* @var bool
*/
private static $registed = false;
/** /**
* @param Server $server * @param Server $server
* @param string $name * @param string $name
@ -38,15 +33,6 @@ class NativeShare implements IShare {
public function __construct($server, $name) { public function __construct($server, $name) {
$this->server = $server; $this->server = $server;
$this->name = $name; $this->name = $name;
self::registerHandlers();
}
private static function registerHandlers() {
if (self::$registed) {
return;
}
self::$registed = true;
stream_wrapper_register('nativesmb', '\Icewind\SMB\NativeStream');
} }
public static function registerErrorHandler() { public static function registerErrorHandler() {
@ -316,13 +302,7 @@ class NativeShare implements IShare {
*/ */
public function read($source) { public function read($source) {
$handle = $this->fopen($source, 'r'); $handle = $this->fopen($source, 'r');
$context = stream_context_create(array( return NativeStream::wrap($this->state, $handle, 'r');
'nativesmb' => array(
'state' => $this->state,
'handle' => $handle
)
));
return fopen('nativesmb://dummy', 'r', false, $context);
} }
/** /**
@ -336,12 +316,6 @@ class NativeShare implements IShare {
*/ */
public function write($source) { public function write($source) {
$handle = $this->create($source); $handle = $this->create($source);
$context = stream_context_create(array( return NativeStream::wrap($this->state, $handle, 'w');
'nativesmb' => array(
'state' => $this->state,
'handle' => $handle
)
));
return fopen('nativesmb://dummy', 'w', false, $context);
} }
} }

View file

@ -14,6 +14,27 @@ class NativeStream {
private $handle; private $handle;
/**
* Wrap a stream from libsmbclient-php into a regular php stream
*
* @param resource $state
* @param resource $smbStream
* @param string $mode
* @return resource
*/
public static function wrap($state, $smbStream, $mode) {
stream_wrapper_register('nativesmb', '\Icewind\SMB\NativeStream');
$context = stream_context_create(array(
'nativesmb' => array(
'state' => $state,
'handle' => $smbStream
)
));
$fh = fopen('nativesmb://', $mode, false, $context);
stream_wrapper_unregister('nativesmb');
return $fh;
}
public function stream_close() { public function stream_close() {
return smbclient_close($this->state, $this->handle); return smbclient_close($this->state, $this->handle);
} }
@ -27,18 +48,9 @@ class NativeStream {
public function stream_open() { public function stream_open() {
$context = stream_context_get_options($this->context); $context = stream_context_get_options($this->context);
if (isset($context['nativesmb'])) { $this->state = $context['nativesmb']['state'];
$context = $context['nativesmb']; $this->handle = $context['nativesmb']['handle'];
} else {
throw new Exception('Invalid context');
}
if (isset($context['state']) and isset($context['handle'])) {
$this->state = $context['state'];
$this->handle = $context['handle'];
return true; return true;
} else {
throw new Exception('Invalid context');
}
} }
public function stream_read($count) { public function stream_read($count) {

View file

@ -102,23 +102,4 @@ class NativeStream extends \PHPUnit_Framework_TestCase {
} }
$this->share->rmdir($dir); $this->share->rmdir($dir);
} }
/**
* @expectedException \Icewind\SMB\Exception
*/
public function testNoContext() {
return fopen('nativesmb://dummy', 'r');
}
/**
* @expectedException \Icewind\SMB\Exception
*/
public function testInvalidContext() {
$context = stream_context_create(array(
'nativesmb' => array(
'foo' => 'bar'
)
));
return fopen('nativesmb://dummy', 'r', false, $context);
}
} }