2013-05-08 16:35:10 -04:00
< ? php
2024-05-23 03:26:56 -04:00
2013-05-08 16:35:10 -04:00
/**
2024-05-23 03:26:56 -04:00
* SPDX - FileCopyrightText : 2016 - 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX - FileCopyrightText : 2016 ownCloud , Inc .
* SPDX - License - Identifier : AGPL - 3.0 - only
2013-05-08 16:35:10 -04:00
*/
namespace OC\Files\Storage ;
2015-03-05 06:28:17 -05:00
use OCP\Files\Mount\IMountPoint ;
2024-09-16 15:48:18 -04:00
use OCP\Files\Storage\IConstructableStorage ;
2024-09-15 09:38:25 -04:00
use OCP\Files\Storage\IStorage ;
2014-11-24 09:54:42 -05:00
use OCP\Files\Storage\IStorageFactory ;
2024-09-16 15:48:18 -04:00
use Psr\Log\LoggerInterface ;
2014-11-24 09:54:42 -05:00
class StorageFactory implements IStorageFactory {
2013-06-07 11:07:13 -04:00
/**
2015-03-26 14:24:49 -04:00
* @ var array [] [ $name => [ 'priority' => $priority , 'wrapper' => $callable ] $storageWrappers
2013-06-07 11:07:13 -04:00
*/
2015-03-26 14:24:49 -04:00
private $storageWrappers = [];
2013-05-08 16:35:10 -04:00
2024-10-01 10:12:30 -04:00
public function addStorageWrapper ( string $wrapperName , callable $callback , int $priority = 50 , array $existingMounts = []) : bool {
2015-01-21 16:27:59 -05:00
if ( isset ( $this -> storageWrappers [ $wrapperName ])) {
return false ;
}
2015-01-23 07:48:35 -05: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 14:24:49 -04:00
$this -> storageWrappers [ $wrapperName ] = [ 'wrapper' => $callback , 'priority' => $priority ];
2015-01-21 16:27:59 -05:00
return true ;
2013-06-07 11:07:13 -04:00
}
2015-01-21 15:54:43 -05:00
/**
* Remove a storage wrapper by name .
* Note : internal method only to be used for cleanup
*
* @ internal
*/
2024-10-01 10:12:30 -04:00
public function removeStorageWrapper ( string $wrapperName ) : void {
2015-01-21 15:54:43 -05:00
unset ( $this -> storageWrappers [ $wrapperName ]);
}
2014-02-06 10:30:58 -05:00
/**
2014-11-24 09:54:42 -05:00
* Create an instance of a storage and apply the registered storage wrappers
2014-02-06 10:30:58 -05:00
*/
2024-10-01 10:12:30 -04:00
public function getInstance ( IMountPoint $mountPoint , string $class , array $arguments ) : IStorage {
2024-09-30 05:19:53 -04:00
if ( ! is_a ( $class , IConstructableStorage :: class , true )) {
2024-09-16 15:48:18 -04:00
\OCP\Server :: get ( LoggerInterface :: class ) -> warning ( 'Building a storage not implementing IConstructableStorage is deprecated since 31.0.0' , [ 'class' => $class ]);
}
2013-06-07 11:07:13 -04:00
return $this -> wrap ( $mountPoint , new $class ( $arguments ));
}
2024-10-01 10:12:30 -04:00
public function wrap ( IMountPoint $mountPoint , IStorage $storage ) : IStorage {
2015-03-26 14:24:49 -04: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 06:28:17 -05:00
$storage = $wrapper ( $mountPoint -> getMountPoint (), $storage , $mountPoint );
2024-09-15 09:38:25 -04:00
if ( ! ( $storage instanceof IStorage )) {
2015-08-28 14:48:42 -04:00
throw new \Exception ( 'Invalid result from storage wrapper' );
}
2013-06-07 11:07:13 -04:00
}
return $storage ;
2013-05-08 16:35:10 -04:00
}
}