From bd675124096707a925faac2774516975ec7049c1 Mon Sep 17 00:00:00 2001
From: Robin Appelman <icewind@owncloud.com>
Date: Fri, 7 Jun 2013 17:07:13 +0200
Subject: [PATCH] manage creating and wrapping storages in it's own class

---
 lib/files/filesystem.php     | 11 +++++++--
 lib/files/mount/mount.php    | 45 +++++++++++++-----------------------
 lib/files/storage/loader.php | 27 +++++++++++++++++++---
 3 files changed, 49 insertions(+), 34 deletions(-)

diff --git a/lib/files/filesystem.php b/lib/files/filesystem.php
index eadd8a93fa..ce89c5c23f 100644
--- a/lib/files/filesystem.php
+++ b/lib/files/filesystem.php
@@ -30,6 +30,7 @@
 
 namespace OC\Files;
 
+use OC\Files\Storage\Loader;
 const FREE_SPACE_UNKNOWN = -2;
 const FREE_SPACE_UNLIMITED = -3;
 
@@ -142,6 +143,11 @@ class Filesystem {
 	 */
 	const signal_param_run = 'run';
 
+	/**
+	 * @var \OC\Files\Storage\Loader $loader
+	 */
+	private static $loader;
+
 	/**
 	 * get the mountpoint of the storage object for a path
 	 * ( note: because a storage is not always mounted inside the fakeroot, the
@@ -221,6 +227,7 @@ class Filesystem {
 		if (self::$defaultInstance) {
 			return false;
 		}
+		self::$loader = new Loader();
 		self::$defaultInstance = new View($root);
 		self::$mounts = new Mount\Manager();
 
@@ -232,7 +239,7 @@ class Filesystem {
 		return true;
 	}
 
-	static public function initMounts(){
+	static public function initMounts() {
 		self::$mounts = new Mount\Manager();
 	}
 
@@ -365,7 +372,7 @@ class Filesystem {
 	 * @param string $mountpoint
 	 */
 	static public function mount($class, $arguments, $mountpoint) {
-		$mount = new Mount\Mount($class, $mountpoint, $arguments);
+		$mount = new Mount\Mount($class, $mountpoint, $arguments, self::$loader);
 		self::$mounts->addMount($mount);
 	}
 
diff --git a/lib/files/mount/mount.php b/lib/files/mount/mount.php
index d25a7b3be6..17b0055ee8 100644
--- a/lib/files/mount/mount.php
+++ b/lib/files/mount/mount.php
@@ -9,10 +9,10 @@
 namespace OC\Files\Mount;
 
 use \OC\Files\Filesystem;
+use OC\Files\Storage\Loader;
+use OC\Files\Storage\Storage;
 
 class Mount {
-
-
 	/**
 	 * @var \OC\Files\Storage\Storage $storage
 	 */
@@ -23,24 +23,30 @@ class Mount {
 	private $mountPoint;
 
 	/**
-	 * @var callable[] $storageWrappers
+	 * @var \OC\Files\Storage\Loader $loader
 	 */
-	private $storageWrappers = array();
+	private $loader;
 
 	/**
-	 * @param string|\OC\Files\Storage\Storage $storage
+	 * @param string | \OC\Files\Storage\Storage $storage
 	 * @param string $mountpoint
-	 * @param array $arguments (optional)
+	 * @param array $arguments (optional)\
+	 * @param \OC\Files\Storage\Loader $loader
 	 */
-	public function __construct($storage, $mountpoint, $arguments = null) {
+	public function __construct($storage, $mountpoint, $arguments = null, $loader = null) {
 		if (is_null($arguments)) {
 			$arguments = array();
 		}
+		if (is_null($loader)) {
+			$this->loader = new Loader();
+		} else {
+			$this->loader = $loader;
+		}
 
 		$mountpoint = $this->formatPath($mountpoint);
-		if ($storage instanceof \OC\Files\Storage\Storage) {
+		if ($storage instanceof Storage) {
 			$this->class = get_class($storage);
-			$this->storage = $storage;
+			$this->storage = $this->loader->wrap($mountpoint, $storage);
 		} else {
 			// Update old classes to new namespace
 			if (strpos($storage, 'OC_Filestorage_') !== false) {
@@ -67,7 +73,7 @@ class Mount {
 	private function createStorage() {
 		if (class_exists($this->class)) {
 			try {
-				return $this->loadStorage($this->class, $this->arguments);
+				return $this->loader->load($this->mountPoint, $this->class, $this->arguments);
 			} catch (\Exception $exception) {
 				\OC_Log::write('core', $exception->getMessage(), \OC_Log::ERROR);
 				return null;
@@ -78,25 +84,6 @@ 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
index 7330cae4cc..2572ef443b 100644
--- a/lib/files/storage/loader.php
+++ b/lib/files/storage/loader.php
@@ -9,9 +9,30 @@
 namespace OC\Files\Storage;
 
 class Loader {
-	private function $wrappers
+	/**
+	 * @var callable[] $storageWrappers
+	 */
+	private $storageWrappers = array();
 
-	public function load($class, $arguments) {
-		return new $class($arguments);
+	/**
+	 * 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;
+	}
+
+	public function load($mountPoint, $class, $arguments) {
+		return $this->wrap($mountPoint, new $class($arguments));
+	}
+
+	public function wrap($mountPoint, $storage) {
+		foreach ($this->storageWrappers as $wrapper) {
+			$storage = $wrapper($mountPoint, $storage);
+		}
+		return $storage;
 	}
 }
-- 
GitLab