2013-05-09 00:35:10 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2015-03-26 13:44:34 +03:00
|
|
|
* @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
|
|
|
/**
|
2015-03-26 21:24:49 +03:00
|
|
|
* @var array[] [$name=>['priority'=>$priority, 'wrapper'=>$callable] $storageWrappers
|
2013-06-07 19:07:13 +04:00
|
|
|
*/
|
2015-03-26 21:24:49 +03:00
|
|
|
private $storageWrappers = [];
|
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-03-26 21:24:49 +03:00
|
|
|
* @param int $priority wrappers with the lower priority are applied last (meaning they get called first)
|
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-03-26 21:24:49 +03:00
|
|
|
public function addStorageWrapper($wrapperName, $callback, $priority = 50, $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);
|
|
|
|
}
|
|
|
|
|
2015-03-26 21:24:49 +03:00
|
|
|
$this->storageWrappers[$wrapperName] = ['wrapper' => $callback, 'priority' => $priority];
|
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) {
|
2015-03-26 21:24:49 +03: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 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
|
|
|
}
|
|
|
|
}
|