2013-05-08 20:35:10 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 15:07:57 +00:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 16:13:36 +00:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
|
|
|
*
|
|
|
|
* @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-08 20:35:10 +00:00
|
|
|
*/
|
2015-02-26 10:37:37 +00:00
|
|
|
|
2013-05-08 20:35:10 +00:00
|
|
|
namespace OC\Files\Storage;
|
|
|
|
|
2015-03-05 11:28:17 +00:00
|
|
|
use OCP\Files\Mount\IMountPoint;
|
2014-11-24 14:54:42 +00:00
|
|
|
use OCP\Files\Storage\IStorageFactory;
|
|
|
|
|
|
|
|
class StorageFactory implements IStorageFactory {
|
2013-06-07 15:07:13 +00:00
|
|
|
/**
|
2015-03-26 18:24:49 +00:00
|
|
|
* @var array[] [$name=>['priority'=>$priority, 'wrapper'=>$callable] $storageWrappers
|
2013-06-07 15:07:13 +00:00
|
|
|
*/
|
2015-03-26 18:24:49 +00:00
|
|
|
private $storageWrappers = [];
|
2013-05-08 20:35:10 +00:00
|
|
|
|
2013-06-07 15:07:13 +00: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 20:54:43 +00:00
|
|
|
* @param string $wrapperName name of the wrapper
|
|
|
|
* @param callable $callback callback
|
2015-03-26 18:24:49 +00:00
|
|
|
* @param int $priority wrappers with the lower priority are applied last (meaning they get called first)
|
2015-01-23 12:48:35 +00:00
|
|
|
* @param \OCP\Files\Mount\IMountPoint[] $existingMounts existing mount points to apply the wrapper to
|
2015-01-21 20:54:43 +00:00
|
|
|
* @return bool true if the wrapper was added, false if there was already a wrapper with this
|
2015-01-21 21:27:59 +00:00
|
|
|
* name registered
|
2013-06-07 15:07:13 +00:00
|
|
|
*/
|
2015-03-26 18:24:49 +00:00
|
|
|
public function addStorageWrapper($wrapperName, $callback, $priority = 50, $existingMounts = []) {
|
2015-01-21 21:27:59 +00:00
|
|
|
if (isset($this->storageWrappers[$wrapperName])) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-01-23 12:48:35 +00:00
|
|
|
|
|
|
|
// apply to existing mounts before registering it to prevent applying it double in MountPoint::createStorage
|
|
|
|
foreach ($existingMounts as $mount) {
|
|
|
|
$mount->wrapStorage($callback);
|
|
|
|
}
|
|
|
|
|
2015-03-26 18:24:49 +00:00
|
|
|
$this->storageWrappers[$wrapperName] = ['wrapper' => $callback, 'priority' => $priority];
|
2015-01-21 21:27:59 +00:00
|
|
|
return true;
|
2013-06-07 15:07:13 +00:00
|
|
|
}
|
|
|
|
|
2015-01-21 20:54:43 +00: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 15:30:58 +00:00
|
|
|
/**
|
2014-11-24 14:54:42 +00:00
|
|
|
* Create an instance of a storage and apply the registered storage wrappers
|
|
|
|
*
|
2015-03-05 11:28:17 +00:00
|
|
|
* @param \OCP\Files\Mount\IMountPoint $mountPoint
|
2014-02-06 15:30:58 +00:00
|
|
|
* @param string $class
|
2014-11-24 14:54:42 +00:00
|
|
|
* @param array $arguments
|
|
|
|
* @return \OCP\Files\Storage
|
2014-02-06 15:30:58 +00:00
|
|
|
*/
|
2015-03-05 11:28:17 +00:00
|
|
|
public function getInstance(IMountPoint $mountPoint, $class, $arguments) {
|
2013-06-07 15:07:13 +00:00
|
|
|
return $this->wrap($mountPoint, new $class($arguments));
|
|
|
|
}
|
|
|
|
|
2014-02-19 08:31:54 +00:00
|
|
|
/**
|
2015-03-05 11:28:17 +00:00
|
|
|
* @param \OCP\Files\Mount\IMountPoint $mountPoint
|
2014-11-24 14:54:42 +00:00
|
|
|
* @param \OCP\Files\Storage $storage
|
|
|
|
* @return \OCP\Files\Storage
|
2014-02-19 08:31:54 +00:00
|
|
|
*/
|
2015-03-05 11:28:17 +00:00
|
|
|
public function wrap(IMountPoint $mountPoint, $storage) {
|
2015-03-26 18:24:49 +00:00
|
|
|
$wrappers = array_values($this->storageWrappers);
|
|
|
|
usort($wrappers, function ($a, $b) {
|
|
|
|
return $b['priority'] - $a['priority'];
|
|
|
|
});
|
|
|
|
/** @var callable[] $wrappers */
|
|
|
|
$wrappers = array_map(function ($wrapper) {
|
|
|
|
return $wrapper['wrapper'];
|
|
|
|
}, $wrappers);
|
|
|
|
foreach ($wrappers as $wrapper) {
|
2015-03-05 11:28:17 +00:00
|
|
|
$storage = $wrapper($mountPoint->getMountPoint(), $storage, $mountPoint);
|
2015-08-28 18:48:42 +00:00
|
|
|
if (!($storage instanceof \OCP\Files\Storage)) {
|
|
|
|
throw new \Exception('Invalid result from storage wrapper');
|
|
|
|
}
|
2013-06-07 15:07:13 +00:00
|
|
|
}
|
|
|
|
return $storage;
|
2013-05-08 20:35:10 +00:00
|
|
|
}
|
|
|
|
}
|