From 8c5521fdfc2e9e85037c61cd77a254e3b7cd1b94 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Thu, 29 May 2014 13:45:50 +0200 Subject: [PATCH] Add $storage->instanceOfStorage to handle instanceof for storage wrappers --- lib/private/files/storage/common.php | 14 ++++++++++++-- lib/private/files/storage/wrapper/wrapper.php | 10 ++++++++++ lib/public/files/storage.php | 8 ++++++++ tests/lib/files/storage/storage.php | 6 ++++++ tests/lib/files/storage/wrapper/quota.php | 6 ++++++ tests/lib/files/storage/wrapper/wrapper.php | 5 +++++ 6 files changed, 47 insertions(+), 2 deletions(-) diff --git a/lib/private/files/storage/common.php b/lib/private/files/storage/common.php index 1ed0d79817..6b11603323 100644 --- a/lib/private/files/storage/common.php +++ b/lib/private/files/storage/common.php @@ -7,6 +7,7 @@ */ namespace OC\Files\Storage; + use OC\Files\Filesystem; use OC\Files\Cache\Watcher; @@ -21,7 +22,6 @@ use OC\Files\Cache\Watcher; * Some \OC\Files\Storage\Common methods call functions which are first defined * in classes which extend it, e.g. $this->stat() . */ - abstract class Common implements \OC\Files\Storage\Storage { protected $cache; protected $scanner; @@ -46,7 +46,7 @@ abstract class Common implements \OC\Files\Storage\Storage { protected function remove($path) { if ($this->is_dir($path)) { return $this->rmdir($path); - } else if($this->is_file($path)) { + } else if ($this->is_file($path)) { return $this->unlink($path); } else { return false; @@ -412,4 +412,14 @@ abstract class Common implements \OC\Files\Storage\Storage { protected function removeCachedFile($path) { unset($this->cachedFiles[$path]); } + + /** + * Check if the storage is an instance of $class or is a wrapper for a storage that is an instance of $class + * + * @param string $class + * @return bool + */ + public function instanceOfStorage($class) { + return is_a($this, $class); + } } diff --git a/lib/private/files/storage/wrapper/wrapper.php b/lib/private/files/storage/wrapper/wrapper.php index 11ea9f71da..dd2f76e05b 100644 --- a/lib/private/files/storage/wrapper/wrapper.php +++ b/lib/private/files/storage/wrapper/wrapper.php @@ -440,4 +440,14 @@ class Wrapper implements \OC\Files\Storage\Storage { public function isLocal() { return $this->storage->isLocal(); } + + /** + * Check if the storage is an instance of $class or is a wrapper for a storage that is an instance of $class + * + * @param string $class + * @return bool + */ + public function instanceOfStorage($class) { + return is_a($this, $class) or $this->storage->instanceOfStorage($class); + } } diff --git a/lib/public/files/storage.php b/lib/public/files/storage.php index 5ec8ac6245..323d20db56 100644 --- a/lib/public/files/storage.php +++ b/lib/public/files/storage.php @@ -327,4 +327,12 @@ interface Storage { * @return bool true if the files are stored locally, false otherwise */ public function isLocal(); + + /** + * Check if the storage is an instance of $class or is a wrapper for a storage that is an instance of $class + * + * @param string $class + * @return bool + */ + public function instanceOfStorage($class); } diff --git a/tests/lib/files/storage/storage.php b/tests/lib/files/storage/storage.php index 24390f0536..dd73491d7e 100644 --- a/tests/lib/files/storage/storage.php +++ b/tests/lib/files/storage/storage.php @@ -464,4 +464,10 @@ abstract class Storage extends \PHPUnit_Framework_TestCase { $this->assertEquals('foo', $this->instance->file_get_contents('target/test1.txt')); } + + public function testInstanceOfStorage() { + $this->assertTrue($this->instance->instanceOfStorage('\OCP\Files\Storage')); + $this->assertTrue($this->instance->instanceOfStorage(get_class($this->instance))); + $this->assertFalse($this->instance->instanceOfStorage('\OC')); + } } diff --git a/tests/lib/files/storage/wrapper/quota.php b/tests/lib/files/storage/wrapper/quota.php index 777529fd85..954fe199cc 100644 --- a/tests/lib/files/storage/wrapper/quota.php +++ b/tests/lib/files/storage/wrapper/quota.php @@ -155,4 +155,10 @@ class Quota extends \Test\Files\Storage\Storage { $this->assertEquals(1024 - 50, $instance->free_space('')); } + + public function testInstanceOfStorageWrapper() { + $this->assertTrue($this->instance->instanceOfStorage('\OC\Files\Storage\Local')); + $this->assertTrue($this->instance->instanceOfStorage('\OC\Files\Storage\Wrapper\Wrapper')); + $this->assertTrue($this->instance->instanceOfStorage('\OC\Files\Storage\Wrapper\Quota')); + } } diff --git a/tests/lib/files/storage/wrapper/wrapper.php b/tests/lib/files/storage/wrapper/wrapper.php index e31abfc732..8bcf42035d 100644 --- a/tests/lib/files/storage/wrapper/wrapper.php +++ b/tests/lib/files/storage/wrapper/wrapper.php @@ -23,4 +23,9 @@ class Wrapper extends \Test\Files\Storage\Storage { public function tearDown() { \OC_Helper::rmdirr($this->tmpDir); } + + public function testInstanceOfStorageWrapper() { + $this->assertTrue($this->instance->instanceOfStorage('\OC\Files\Storage\Local')); + $this->assertTrue($this->instance->instanceOfStorage('\OC\Files\Storage\Wrapper\Wrapper')); + } }