diff --git a/phpseclib/Net/SFTP/Stream.php b/phpseclib/Net/SFTP/Stream.php index e8bd23a1..64126b2d 100644 --- a/phpseclib/Net/SFTP/Stream.php +++ b/phpseclib/Net/SFTP/Stream.php @@ -126,6 +126,22 @@ class Net_SFTP_Stream */ var $notification; + /** + * Registers this class as a URL wrapper. + * + * @param optional String $protocol The wrapper name to be registered. + * @return Boolean True on success, false otherwise. + * @access public + */ + static function register($protocol = 'sftp') + { + if (in_array($protocol, stream_get_wrappers(), true)) { + return false; + } + $class = function_exists('get_called_class') ? get_called_class() : __CLASS__; + return stream_wrapper_register($protocol, $class); + } + /** * The Constructor * @@ -783,6 +799,4 @@ class Net_SFTP_Stream } } -if (function_exists('stream_wrapper_register')) { - stream_wrapper_register('sftp', 'Net_SFTP_Stream'); -} +Net_SFTP_Stream::register(); diff --git a/tests/Unit/Net/SFTPStreamTest.php b/tests/Unit/Net/SFTPStreamTest.php new file mode 100644 index 00000000..f54f79f8 --- /dev/null +++ b/tests/Unit/Net/SFTPStreamTest.php @@ -0,0 +1,33 @@ + + * @copyright MMXIV Andreas Fischer + * @license http://www.opensource.org/licenses/mit-license.html MIT License + */ + +require_once 'Net/SFTP/Stream.php'; + +class Unit_Net_SFTPStreamTest extends PhpseclibTestCase +{ + protected $protocol = 'sftptest'; + + public function setUp() + { + parent::setUp(); + if (in_array($this->protocol, stream_get_wrappers())) { + stream_wrapper_unregister($this->protocol); + } + } + + public function testRegisterFromSideEffect() + { + // Including the file registers 'sftp' as a stream. + $this->assertContains('sftp', stream_get_wrappers()); + } + + public function testRegisterWithArgument() + { + Net_SFTP_Stream::register($this->protocol); + $this->assertContains($this->protocol, stream_get_wrappers()); + } +}