2013-05-09 00:35:10 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Björn Schießle <schiessle@owncloud.com>
|
|
|
|
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
* @author Robin Appelman <icewind@owncloud.com>
|
|
|
|
* @author Scrutinizer Auto-Fixer <auto-fixer@scrutinizer-ci.com>
|
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc.
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
2013-05-09 00:35:10 +04:00
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2013-05-09 00:35:10 +04:00
|
|
|
namespace OC\Files\Storage;
|
|
|
|
|
2015-03-05 14:28:17 +03:00
|
|
|
use OCP\Files\Mount\IMountPoint;
|
2014-11-24 17:54:42 +03:00
|
|
|
use OCP\Files\Storage\IStorageFactory;
|
|
|
|
|
|
|
|
class StorageFactory implements IStorageFactory {
|
2013-06-07 19:07:13 +04:00
|
|
|
/**
|
|
|
|
* @var callable[] $storageWrappers
|
|
|
|
*/
|
|
|
|
private $storageWrappers = array();
|
2013-05-09 00:35:10 +04:00
|
|
|
|
2013-06-07 19:07:13 +04:00
|
|
|
/**
|
|
|
|
* allow modifier storage behaviour by adding wrappers around storages
|
|
|
|
*
|
|
|
|
* $callback should be a function of type (string $mountPoint, Storage $storage) => Storage
|
|
|
|
*
|
2015-01-21 23:54:43 +03:00
|
|
|
* @param string $wrapperName name of the wrapper
|
|
|
|
* @param callable $callback callback
|
2015-01-23 15:48:35 +03:00
|
|
|
* @param \OCP\Files\Mount\IMountPoint[] $existingMounts existing mount points to apply the wrapper to
|
2015-01-21 23:54:43 +03:00
|
|
|
* @return bool true if the wrapper was added, false if there was already a wrapper with this
|
2015-01-22 00:27:59 +03:00
|
|
|
* name registered
|
2013-06-07 19:07:13 +04:00
|
|
|
*/
|
2015-01-23 15:48:35 +03:00
|
|
|
public function addStorageWrapper($wrapperName, $callback, $existingMounts = []) {
|
2015-01-22 00:27:59 +03:00
|
|
|
if (isset($this->storageWrappers[$wrapperName])) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-01-23 15:48:35 +03:00
|
|
|
|
|
|
|
// apply to existing mounts before registering it to prevent applying it double in MountPoint::createStorage
|
|
|
|
foreach ($existingMounts as $mount) {
|
|
|
|
$mount->wrapStorage($callback);
|
|
|
|
}
|
|
|
|
|
2014-06-02 17:06:15 +04:00
|
|
|
$this->storageWrappers[$wrapperName] = $callback;
|
2015-01-22 00:27:59 +03:00
|
|
|
return true;
|
2013-06-07 19:07:13 +04:00
|
|
|
}
|
|
|
|
|
2015-01-21 23:54:43 +03:00
|
|
|
/**
|
|
|
|
* Remove a storage wrapper by name.
|
|
|
|
* Note: internal method only to be used for cleanup
|
|
|
|
*
|
|
|
|
* @param string $wrapperName name of the wrapper
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
public function removeStorageWrapper($wrapperName) {
|
|
|
|
unset($this->storageWrappers[$wrapperName]);
|
|
|
|
}
|
|
|
|
|
2014-02-06 19:30:58 +04:00
|
|
|
/**
|
2014-11-24 17:54:42 +03:00
|
|
|
* Create an instance of a storage and apply the registered storage wrappers
|
|
|
|
*
|
2015-03-05 14:28:17 +03:00
|
|
|
* @param \OCP\Files\Mount\IMountPoint $mountPoint
|
2014-02-06 19:30:58 +04:00
|
|
|
* @param string $class
|
2014-11-24 17:54:42 +03:00
|
|
|
* @param array $arguments
|
|
|
|
* @return \OCP\Files\Storage
|
2014-02-06 19:30:58 +04:00
|
|
|
*/
|
2015-03-05 14:28:17 +03:00
|
|
|
public function getInstance(IMountPoint $mountPoint, $class, $arguments) {
|
2013-06-07 19:07:13 +04:00
|
|
|
return $this->wrap($mountPoint, new $class($arguments));
|
|
|
|
}
|
|
|
|
|
2014-02-19 12:31:54 +04:00
|
|
|
/**
|
2015-03-05 14:28:17 +03:00
|
|
|
* @param \OCP\Files\Mount\IMountPoint $mountPoint
|
2014-11-24 17:54:42 +03:00
|
|
|
* @param \OCP\Files\Storage $storage
|
|
|
|
* @return \OCP\Files\Storage
|
2014-02-19 12:31:54 +04:00
|
|
|
*/
|
2015-03-05 14:28:17 +03:00
|
|
|
public function wrap(IMountPoint $mountPoint, $storage) {
|
2013-06-07 19:07:13 +04:00
|
|
|
foreach ($this->storageWrappers as $wrapper) {
|
2015-03-05 14:28:17 +03:00
|
|
|
$storage = $wrapper($mountPoint->getMountPoint(), $storage, $mountPoint);
|
2013-06-07 19:07:13 +04:00
|
|
|
}
|
|
|
|
return $storage;
|
2013-05-09 00:35:10 +04:00
|
|
|
}
|
|
|
|
}
|