From d97ef0805ba5c5687e0642bb8f7c085966153ac9 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Wed, 8 May 2013 22:35:10 +0200 Subject: [PATCH] Add mechanism to allow apps to wraper storage classes --- lib/files/mount/mount.php | 26 +++++++++++++++++++++++++- lib/files/storage/loader.php | 17 +++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 lib/files/storage/loader.php diff --git a/lib/files/mount/mount.php b/lib/files/mount/mount.php index 69b8285ab4..d25a7b3be6 100644 --- a/lib/files/mount/mount.php +++ b/lib/files/mount/mount.php @@ -22,6 +22,11 @@ class Mount { private $arguments = array(); private $mountPoint; + /** + * @var callable[] $storageWrappers + */ + private $storageWrappers = array(); + /** * @param string|\OC\Files\Storage\Storage $storage * @param string $mountpoint @@ -62,7 +67,7 @@ class Mount { private function createStorage() { if (class_exists($this->class)) { try { - return new $this->class($this->arguments); + return $this->loadStorage($this->class, $this->arguments); } catch (\Exception $exception) { \OC_Log::write('core', $exception->getMessage(), \OC_Log::ERROR); return null; @@ -73,6 +78,25 @@ class Mount { } } + /** + * allow modifier storage behaviour by adding wrappers around storages + * + * $callback should be a function of type (string $mountPoint, Storage $storage) => Storage + * + * @param callable $callback + */ + public function addStorageWrapper($callback) { + $this->storageWrappers[] = $callback; + } + + private function loadStorage($class, $arguments) { + $storage = new $class($arguments); + foreach ($this->storageWrappers as $wrapper) { + $storage = $wrapper($this->mountPoint, $storage); + } + return $storage; + } + /** * @return \OC\Files\Storage\Storage */ diff --git a/lib/files/storage/loader.php b/lib/files/storage/loader.php new file mode 100644 index 0000000000..7330cae4cc --- /dev/null +++ b/lib/files/storage/loader.php @@ -0,0 +1,17 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\Files\Storage; + +class Loader { + private function $wrappers + + public function load($class, $arguments) { + return new $class($arguments); + } +}