autodetect class and protocol

This commit is contained in:
Robin Appelman 2019-03-11 15:33:30 +01:00
commit b58f2f2065
12 changed files with 28 additions and 15 deletions

View file

@ -72,7 +72,7 @@ class CallbackWrapper extends Wrapper {
'preClose' => $preClose, 'preClose' => $preClose,
) )
)); ));
return Wrapper::wrapSource($source, $context, 'callback', '\Icewind\Streams\CallbackWrapper'); return self::wrapSource($source, $context);
} }
protected function open() { protected function open() {

View file

@ -69,7 +69,7 @@ class CountWrapper extends Wrapper {
'callback' => $callback 'callback' => $callback
) )
)); ));
return Wrapper::wrapSource($source, $context, 'callback', '\Icewind\Streams\CountWrapper'); return self::wrapSource($source, $context);
} }
protected function open() { protected function open() {

View file

@ -55,6 +55,6 @@ class DirectoryFilter extends DirectoryWrapper {
'filter' => $filter 'filter' => $filter
) )
); );
return self::wrapWithOptions($options, '\Icewind\Streams\DirectoryFilter'); return self::wrapWithOptions($options);
} }
} }

View file

@ -75,10 +75,11 @@ class DirectoryWrapper implements Directory {
/** /**
* @param array $options the options for the context to wrap the stream with * @param array $options the options for the context to wrap the stream with
* @param string $class * @param null $class deprecated, class is now automatically generated
* @return resource * @return resource
*/ */
protected static function wrapWithOptions($options, $class) { protected static function wrapWithOptions($options, $class = null) {
$class = static::class;
$context = stream_context_create($options); $context = stream_context_create($options);
stream_wrapper_register('dirwrapper', $class); stream_wrapper_register('dirwrapper', $class);
$wrapped = opendir('dirwrapper://', $context); $wrapped = opendir('dirwrapper://', $context);

View file

@ -115,7 +115,7 @@ class IteratorDirectory implements Directory {
} else { } else {
throw new \BadMethodCallException('$source should be an Iterator or array'); throw new \BadMethodCallException('$source should be an Iterator or array');
} }
stream_wrapper_register('iterator', '\Icewind\Streams\IteratorDirectory'); stream_wrapper_register('iterator', static::class);
$wrapped = opendir('iterator://', $context); $wrapped = opendir('iterator://', $context);
stream_wrapper_unregister('iterator'); stream_wrapper_unregister('iterator');
return $wrapped; return $wrapped;

View file

@ -24,7 +24,7 @@ class NullWrapper extends Wrapper {
'null' => array( 'null' => array(
'source' => $source) 'source' => $source)
)); ));
return Wrapper::wrapSource($source, $context, 'null', '\Icewind\Streams\NullWrapper'); return self::wrapSource($source, $context);
} }
public function stream_open($path, $mode, $options, &$opened_path) { public function stream_open($path, $mode, $options, &$opened_path) {

View file

@ -24,7 +24,7 @@ class RetryWrapper extends Wrapper {
'source' => $source 'source' => $source
) )
)); ));
return Wrapper::wrapSource($source, $context, 'retry', '\Icewind\Streams\RetryWrapper'); return self::wrapSource($source, $context);
} }
protected function open() { protected function open() {

View file

@ -39,7 +39,7 @@ class SeekableWrapper extends Wrapper {
'source' => $source 'source' => $source
) )
)); ));
return Wrapper::wrapSource($source, $context, 'callback', '\Icewind\Streams\SeekableWrapper'); return self::wrapSource($source, $context);
} }
public function dir_opendir($path, $options) { public function dir_opendir($path, $options) {

View file

@ -61,7 +61,7 @@ class UrlCallback extends Wrapper implements Url {
'unlink' => $unlink, 'unlink' => $unlink,
'stat' => $stat 'stat' => $stat
); );
return new Path('\Icewind\Streams\UrlCallBack', $options); return new Path(static::class, $options);
} }
protected function loadContext($url) { protected function loadContext($url) {

View file

@ -25,7 +25,19 @@ abstract class Wrapper implements File, Directory {
*/ */
protected $source; protected $source;
protected static function wrapSource($source, $context, $protocol, $class) { /**
* @param $source
* @param $context
* @param null $protocol deprecated, protocol is now automatically generated
* @param null $class deprecated, class is now automatically generated
* @return bool|resource
*/
protected static function wrapSource($source, $context, $protocol = null, $class = null) {
if ($class === null) {
$class = static::class;
}
$parts = explode('\\', $class);
$protocol = strtolower(array_pop($parts));
if (!is_resource($source)) { if (!is_resource($source)) {
throw new \BadMethodCallException(); throw new \BadMethodCallException();
} }

View file

@ -13,7 +13,7 @@ class DirectoryWrapperNull extends \Icewind\Streams\DirectoryWrapper {
'dir' => array( 'dir' => array(
'source' => $source) 'source' => $source)
); );
return self::wrapWithOptions($options, '\Icewind\Streams\Tests\DirectoryWrapperNull'); return self::wrapWithOptions($options);
} }
} }
@ -23,7 +23,7 @@ class DirectoryWrapperDummy extends \Icewind\Streams\DirectoryWrapper {
'dir' => array( 'dir' => array(
'source' => $source) 'source' => $source)
); );
return self::wrapWithOptions($options, '\Icewind\Streams\Tests\DirectoryWrapperDummy'); return self::wrapWithOptions($options);
} }
public function dir_readdir() { public function dir_readdir() {

View file

@ -21,7 +21,7 @@ class PartialWrapper extends \Icewind\Streams\NullWrapper {
'null' => array( 'null' => array(
'source' => $source) 'source' => $source)
)); ));
return self::wrapSource($source, $context, 'partial', '\Icewind\Streams\Tests\PartialWrapper'); return self::wrapSource($source, $context);
} }
public function stream_read($count) { public function stream_read($count) {
@ -49,7 +49,7 @@ class FailWrapper extends \Icewind\Streams\NullWrapper {
'null' => array( 'null' => array(
'source' => $source) 'source' => $source)
)); ));
return self::wrapSource($source, $context, 'fail', '\Icewind\Streams\Tests\FailWrapper'); return self::wrapSource($source, $context);
} }
public function stream_read($count) { public function stream_read($count) {