@@ -11,40 +11,40 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class MissingDependency { |
| 13 | 13 | |
| 14 | - /** @var string|null Custom message */ |
|
| 15 | - private ?string $message = null; |
|
| 16 | - private bool $optional = false; |
|
| 17 | - |
|
| 18 | - /** |
|
| 19 | - * @param string $dependency |
|
| 20 | - */ |
|
| 21 | - public function __construct( |
|
| 22 | - private readonly string $dependency, |
|
| 23 | - ) { |
|
| 24 | - } |
|
| 25 | - |
|
| 26 | - public function getDependency(): string { |
|
| 27 | - return $this->dependency; |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - public function getMessage(): ?string { |
|
| 31 | - return $this->message; |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * @param string $message |
|
| 36 | - * @return self |
|
| 37 | - */ |
|
| 38 | - public function setMessage($message) { |
|
| 39 | - $this->message = $message; |
|
| 40 | - return $this; |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - public function isOptional(): bool { |
|
| 44 | - return $this->optional; |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - public function setOptional(bool $optional): void { |
|
| 48 | - $this->optional = $optional; |
|
| 49 | - } |
|
| 14 | + /** @var string|null Custom message */ |
|
| 15 | + private ?string $message = null; |
|
| 16 | + private bool $optional = false; |
|
| 17 | + |
|
| 18 | + /** |
|
| 19 | + * @param string $dependency |
|
| 20 | + */ |
|
| 21 | + public function __construct( |
|
| 22 | + private readonly string $dependency, |
|
| 23 | + ) { |
|
| 24 | + } |
|
| 25 | + |
|
| 26 | + public function getDependency(): string { |
|
| 27 | + return $this->dependency; |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + public function getMessage(): ?string { |
|
| 31 | + return $this->message; |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * @param string $message |
|
| 36 | + * @return self |
|
| 37 | + */ |
|
| 38 | + public function setMessage($message) { |
|
| 39 | + $this->message = $message; |
|
| 40 | + return $this; |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + public function isOptional(): bool { |
|
| 44 | + return $this->optional; |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + public function setOptional(bool $optional): void { |
|
| 48 | + $this->optional = $optional; |
|
| 49 | + } |
|
| 50 | 50 | } |
@@ -24,119 +24,119 @@ |
||
| 24 | 24 | use OCP\IUser; |
| 25 | 25 | |
| 26 | 26 | class SMB extends Backend { |
| 27 | - public function __construct(IL10N $l, Password $legacyAuth) { |
|
| 28 | - $this |
|
| 29 | - ->setIdentifier('smb') |
|
| 30 | - ->addIdentifierAlias('\OC\Files\Storage\SMB')// legacy compat |
|
| 31 | - ->setStorageClass('\OCA\Files_External\Lib\Storage\SMB') |
|
| 32 | - ->setText($l->t('SMB/CIFS')) |
|
| 33 | - ->addParameters([ |
|
| 34 | - new DefinitionParameter('host', $l->t('Host')), |
|
| 35 | - new DefinitionParameter('share', $l->t('Share')), |
|
| 36 | - (new DefinitionParameter('root', $l->t('Remote subfolder'))) |
|
| 37 | - ->setFlag(DefinitionParameter::FLAG_OPTIONAL), |
|
| 38 | - (new DefinitionParameter('domain', $l->t('Domain'))) |
|
| 39 | - ->setFlag(DefinitionParameter::FLAG_OPTIONAL), |
|
| 40 | - (new DefinitionParameter('show_hidden', $l->t('Show hidden files'))) |
|
| 41 | - ->setType(DefinitionParameter::VALUE_BOOLEAN) |
|
| 42 | - ->setFlag(DefinitionParameter::FLAG_OPTIONAL), |
|
| 43 | - (new DefinitionParameter('case_sensitive', $l->t('Case sensitive file system'))) |
|
| 44 | - ->setType(DefinitionParameter::VALUE_BOOLEAN) |
|
| 45 | - ->setFlag(DefinitionParameter::FLAG_OPTIONAL) |
|
| 46 | - ->setDefaultValue(true) |
|
| 47 | - ->setTooltip($l->t('Disabling it will allow to use a case insensitive file system, but comes with a performance penalty')), |
|
| 48 | - (new DefinitionParameter('check_acl', $l->t('Verify ACL access when listing files'))) |
|
| 49 | - ->setType(DefinitionParameter::VALUE_BOOLEAN) |
|
| 50 | - ->setFlag(DefinitionParameter::FLAG_OPTIONAL) |
|
| 51 | - ->setTooltip($l->t("Check the ACL's of each file or folder inside a directory to filter out items where the account has no read permissions, comes with a performance penalty")), |
|
| 52 | - (new DefinitionParameter('timeout', $l->t('Timeout'))) |
|
| 53 | - ->setType(DefinitionParameter::VALUE_TEXT) |
|
| 54 | - ->setFlag(DefinitionParameter::FLAG_OPTIONAL) |
|
| 55 | - ->setFlag(DefinitionParameter::FLAG_HIDDEN), |
|
| 56 | - ]) |
|
| 57 | - ->addAuthScheme(AuthMechanism::SCHEME_PASSWORD) |
|
| 58 | - ->addAuthScheme(AuthMechanism::SCHEME_SMB) |
|
| 59 | - ->setLegacyAuthMechanism($legacyAuth); |
|
| 60 | - } |
|
| 27 | + public function __construct(IL10N $l, Password $legacyAuth) { |
|
| 28 | + $this |
|
| 29 | + ->setIdentifier('smb') |
|
| 30 | + ->addIdentifierAlias('\OC\Files\Storage\SMB')// legacy compat |
|
| 31 | + ->setStorageClass('\OCA\Files_External\Lib\Storage\SMB') |
|
| 32 | + ->setText($l->t('SMB/CIFS')) |
|
| 33 | + ->addParameters([ |
|
| 34 | + new DefinitionParameter('host', $l->t('Host')), |
|
| 35 | + new DefinitionParameter('share', $l->t('Share')), |
|
| 36 | + (new DefinitionParameter('root', $l->t('Remote subfolder'))) |
|
| 37 | + ->setFlag(DefinitionParameter::FLAG_OPTIONAL), |
|
| 38 | + (new DefinitionParameter('domain', $l->t('Domain'))) |
|
| 39 | + ->setFlag(DefinitionParameter::FLAG_OPTIONAL), |
|
| 40 | + (new DefinitionParameter('show_hidden', $l->t('Show hidden files'))) |
|
| 41 | + ->setType(DefinitionParameter::VALUE_BOOLEAN) |
|
| 42 | + ->setFlag(DefinitionParameter::FLAG_OPTIONAL), |
|
| 43 | + (new DefinitionParameter('case_sensitive', $l->t('Case sensitive file system'))) |
|
| 44 | + ->setType(DefinitionParameter::VALUE_BOOLEAN) |
|
| 45 | + ->setFlag(DefinitionParameter::FLAG_OPTIONAL) |
|
| 46 | + ->setDefaultValue(true) |
|
| 47 | + ->setTooltip($l->t('Disabling it will allow to use a case insensitive file system, but comes with a performance penalty')), |
|
| 48 | + (new DefinitionParameter('check_acl', $l->t('Verify ACL access when listing files'))) |
|
| 49 | + ->setType(DefinitionParameter::VALUE_BOOLEAN) |
|
| 50 | + ->setFlag(DefinitionParameter::FLAG_OPTIONAL) |
|
| 51 | + ->setTooltip($l->t("Check the ACL's of each file or folder inside a directory to filter out items where the account has no read permissions, comes with a performance penalty")), |
|
| 52 | + (new DefinitionParameter('timeout', $l->t('Timeout'))) |
|
| 53 | + ->setType(DefinitionParameter::VALUE_TEXT) |
|
| 54 | + ->setFlag(DefinitionParameter::FLAG_OPTIONAL) |
|
| 55 | + ->setFlag(DefinitionParameter::FLAG_HIDDEN), |
|
| 56 | + ]) |
|
| 57 | + ->addAuthScheme(AuthMechanism::SCHEME_PASSWORD) |
|
| 58 | + ->addAuthScheme(AuthMechanism::SCHEME_SMB) |
|
| 59 | + ->setLegacyAuthMechanism($legacyAuth); |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * @return void |
|
| 64 | - */ |
|
| 65 | - public function manipulateStorageConfig(StorageConfig &$storage, ?IUser $user = null) { |
|
| 66 | - $auth = $storage->getAuthMechanism(); |
|
| 67 | - if ($auth->getScheme() === AuthMechanism::SCHEME_PASSWORD) { |
|
| 68 | - if (!is_string($storage->getBackendOption('user')) || !is_string($storage->getBackendOption('password'))) { |
|
| 69 | - throw new \InvalidArgumentException('user or password is not set'); |
|
| 70 | - } |
|
| 62 | + /** |
|
| 63 | + * @return void |
|
| 64 | + */ |
|
| 65 | + public function manipulateStorageConfig(StorageConfig &$storage, ?IUser $user = null) { |
|
| 66 | + $auth = $storage->getAuthMechanism(); |
|
| 67 | + if ($auth->getScheme() === AuthMechanism::SCHEME_PASSWORD) { |
|
| 68 | + if (!is_string($storage->getBackendOption('user')) || !is_string($storage->getBackendOption('password'))) { |
|
| 69 | + throw new \InvalidArgumentException('user or password is not set'); |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - $smbAuth = new BasicAuth( |
|
| 73 | - $storage->getBackendOption('user'), |
|
| 74 | - $storage->getBackendOption('domain'), |
|
| 75 | - $storage->getBackendOption('password') |
|
| 76 | - ); |
|
| 77 | - } else { |
|
| 78 | - switch ($auth->getIdentifier()) { |
|
| 79 | - case 'smb::kerberos': |
|
| 80 | - $smbAuth = new KerberosAuth(); |
|
| 81 | - break; |
|
| 82 | - case 'smb::kerberosapache': |
|
| 83 | - if (!$auth instanceof KerberosApacheAuthMechanism) { |
|
| 84 | - throw new \InvalidArgumentException('invalid authentication backend'); |
|
| 85 | - } |
|
| 86 | - $credentialsStore = $auth->getCredentialsStore(); |
|
| 87 | - $kerbAuth = new KerberosApacheAuth(); |
|
| 88 | - // check if a kerberos ticket is available, else fallback to session credentials |
|
| 89 | - if ($kerbAuth->checkTicket()) { |
|
| 90 | - $smbAuth = $kerbAuth; |
|
| 91 | - } else { |
|
| 92 | - try { |
|
| 93 | - $credentials = $credentialsStore->getLoginCredentials(); |
|
| 94 | - $user = $credentials->getLoginName(); |
|
| 95 | - $pass = $credentials->getPassword(); |
|
| 96 | - preg_match('/(.*)@(.*)/', $user, $matches); |
|
| 97 | - $realm = $storage->getBackendOption('default_realm'); |
|
| 98 | - if (empty($realm)) { |
|
| 99 | - $realm = 'WORKGROUP'; |
|
| 100 | - } |
|
| 101 | - if (count($matches) === 0) { |
|
| 102 | - $username = $user; |
|
| 103 | - $workgroup = $realm; |
|
| 104 | - } else { |
|
| 105 | - $username = $matches[1]; |
|
| 106 | - $workgroup = $matches[2]; |
|
| 107 | - } |
|
| 108 | - $smbAuth = new BasicAuth( |
|
| 109 | - $username, |
|
| 110 | - $workgroup, |
|
| 111 | - $pass |
|
| 112 | - ); |
|
| 113 | - } catch (\Exception $e) { |
|
| 114 | - throw new InsufficientDataForMeaningfulAnswerException('No session credentials saved'); |
|
| 115 | - } |
|
| 116 | - } |
|
| 72 | + $smbAuth = new BasicAuth( |
|
| 73 | + $storage->getBackendOption('user'), |
|
| 74 | + $storage->getBackendOption('domain'), |
|
| 75 | + $storage->getBackendOption('password') |
|
| 76 | + ); |
|
| 77 | + } else { |
|
| 78 | + switch ($auth->getIdentifier()) { |
|
| 79 | + case 'smb::kerberos': |
|
| 80 | + $smbAuth = new KerberosAuth(); |
|
| 81 | + break; |
|
| 82 | + case 'smb::kerberosapache': |
|
| 83 | + if (!$auth instanceof KerberosApacheAuthMechanism) { |
|
| 84 | + throw new \InvalidArgumentException('invalid authentication backend'); |
|
| 85 | + } |
|
| 86 | + $credentialsStore = $auth->getCredentialsStore(); |
|
| 87 | + $kerbAuth = new KerberosApacheAuth(); |
|
| 88 | + // check if a kerberos ticket is available, else fallback to session credentials |
|
| 89 | + if ($kerbAuth->checkTicket()) { |
|
| 90 | + $smbAuth = $kerbAuth; |
|
| 91 | + } else { |
|
| 92 | + try { |
|
| 93 | + $credentials = $credentialsStore->getLoginCredentials(); |
|
| 94 | + $user = $credentials->getLoginName(); |
|
| 95 | + $pass = $credentials->getPassword(); |
|
| 96 | + preg_match('/(.*)@(.*)/', $user, $matches); |
|
| 97 | + $realm = $storage->getBackendOption('default_realm'); |
|
| 98 | + if (empty($realm)) { |
|
| 99 | + $realm = 'WORKGROUP'; |
|
| 100 | + } |
|
| 101 | + if (count($matches) === 0) { |
|
| 102 | + $username = $user; |
|
| 103 | + $workgroup = $realm; |
|
| 104 | + } else { |
|
| 105 | + $username = $matches[1]; |
|
| 106 | + $workgroup = $matches[2]; |
|
| 107 | + } |
|
| 108 | + $smbAuth = new BasicAuth( |
|
| 109 | + $username, |
|
| 110 | + $workgroup, |
|
| 111 | + $pass |
|
| 112 | + ); |
|
| 113 | + } catch (\Exception $e) { |
|
| 114 | + throw new InsufficientDataForMeaningfulAnswerException('No session credentials saved'); |
|
| 115 | + } |
|
| 116 | + } |
|
| 117 | 117 | |
| 118 | - break; |
|
| 119 | - default: |
|
| 120 | - throw new \InvalidArgumentException('unknown authentication backend'); |
|
| 121 | - } |
|
| 122 | - } |
|
| 118 | + break; |
|
| 119 | + default: |
|
| 120 | + throw new \InvalidArgumentException('unknown authentication backend'); |
|
| 121 | + } |
|
| 122 | + } |
|
| 123 | 123 | |
| 124 | - $storage->setBackendOption('auth', $smbAuth); |
|
| 125 | - } |
|
| 124 | + $storage->setBackendOption('auth', $smbAuth); |
|
| 125 | + } |
|
| 126 | 126 | |
| 127 | - public function checkDependencies() { |
|
| 128 | - $system = \OCP\Server::get(SystemBridge::class); |
|
| 129 | - if (NativeServer::available($system)) { |
|
| 130 | - return []; |
|
| 131 | - } elseif (Server::available($system)) { |
|
| 132 | - $missing = new MissingDependency('php-smbclient'); |
|
| 133 | - $missing->setOptional(true); |
|
| 134 | - $missing->setMessage('The php-smbclient library provides improved compatibility and performance for SMB storages.'); |
|
| 135 | - return [$missing]; |
|
| 136 | - } else { |
|
| 137 | - $missing = new MissingDependency('php-smbclient'); |
|
| 138 | - $missing->setMessage('Either the php-smbclient library (preferred) or the smbclient binary is required for SMB storages.'); |
|
| 139 | - return [$missing, new MissingDependency('smbclient')]; |
|
| 140 | - } |
|
| 141 | - } |
|
| 127 | + public function checkDependencies() { |
|
| 128 | + $system = \OCP\Server::get(SystemBridge::class); |
|
| 129 | + if (NativeServer::available($system)) { |
|
| 130 | + return []; |
|
| 131 | + } elseif (Server::available($system)) { |
|
| 132 | + $missing = new MissingDependency('php-smbclient'); |
|
| 133 | + $missing->setOptional(true); |
|
| 134 | + $missing->setMessage('The php-smbclient library provides improved compatibility and performance for SMB storages.'); |
|
| 135 | + return [$missing]; |
|
| 136 | + } else { |
|
| 137 | + $missing = new MissingDependency('php-smbclient'); |
|
| 138 | + $missing->setMessage('Either the php-smbclient library (preferred) or the smbclient binary is required for SMB storages.'); |
|
| 139 | + return [$missing, new MissingDependency('smbclient')]; |
|
| 140 | + } |
|
| 141 | + } |
|
| 142 | 142 | } |
@@ -11,46 +11,46 @@ |
||
| 11 | 11 | use Symfony\Component\Console\Output\OutputInterface; |
| 12 | 12 | |
| 13 | 13 | class Dependencies extends Base { |
| 14 | - public function __construct( |
|
| 15 | - private readonly BackendService $backendService, |
|
| 16 | - ) { |
|
| 17 | - parent::__construct(); |
|
| 18 | - } |
|
| 19 | - |
|
| 20 | - protected function configure(): void { |
|
| 21 | - $this |
|
| 22 | - ->setName('files_external:dependencies') |
|
| 23 | - ->setDescription('Show information about the backend dependencies'); |
|
| 24 | - parent::configure(); |
|
| 25 | - } |
|
| 26 | - |
|
| 27 | - protected function execute(InputInterface $input, OutputInterface $output): int { |
|
| 28 | - $storageBackends = $this->backendService->getBackends(); |
|
| 29 | - |
|
| 30 | - $anyMissing = false; |
|
| 31 | - |
|
| 32 | - foreach ($storageBackends as $backend) { |
|
| 33 | - if ($backend->getDeprecateTo() !== null) { |
|
| 34 | - continue; |
|
| 35 | - } |
|
| 36 | - $missingDependencies = $backend->checkDependencies(); |
|
| 37 | - if ($missingDependencies) { |
|
| 38 | - $anyMissing = true; |
|
| 39 | - $output->writeln($backend->getText() . ':'); |
|
| 40 | - foreach ($missingDependencies as $missingDependency) { |
|
| 41 | - if ($missingDependency->getMessage()) { |
|
| 42 | - $output->writeln(" - <comment>{$missingDependency->getDependency()}</comment>: {$missingDependency->getMessage()}"); |
|
| 43 | - } else { |
|
| 44 | - $output->writeln(" - <comment>{$missingDependency->getDependency()}</comment>"); |
|
| 45 | - } |
|
| 46 | - } |
|
| 47 | - } |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - if (!$anyMissing) { |
|
| 51 | - $output->writeln('<info>All dependencies are met</info>'); |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - return self::SUCCESS; |
|
| 55 | - } |
|
| 14 | + public function __construct( |
|
| 15 | + private readonly BackendService $backendService, |
|
| 16 | + ) { |
|
| 17 | + parent::__construct(); |
|
| 18 | + } |
|
| 19 | + |
|
| 20 | + protected function configure(): void { |
|
| 21 | + $this |
|
| 22 | + ->setName('files_external:dependencies') |
|
| 23 | + ->setDescription('Show information about the backend dependencies'); |
|
| 24 | + parent::configure(); |
|
| 25 | + } |
|
| 26 | + |
|
| 27 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
|
| 28 | + $storageBackends = $this->backendService->getBackends(); |
|
| 29 | + |
|
| 30 | + $anyMissing = false; |
|
| 31 | + |
|
| 32 | + foreach ($storageBackends as $backend) { |
|
| 33 | + if ($backend->getDeprecateTo() !== null) { |
|
| 34 | + continue; |
|
| 35 | + } |
|
| 36 | + $missingDependencies = $backend->checkDependencies(); |
|
| 37 | + if ($missingDependencies) { |
|
| 38 | + $anyMissing = true; |
|
| 39 | + $output->writeln($backend->getText() . ':'); |
|
| 40 | + foreach ($missingDependencies as $missingDependency) { |
|
| 41 | + if ($missingDependency->getMessage()) { |
|
| 42 | + $output->writeln(" - <comment>{$missingDependency->getDependency()}</comment>: {$missingDependency->getMessage()}"); |
|
| 43 | + } else { |
|
| 44 | + $output->writeln(" - <comment>{$missingDependency->getDependency()}</comment>"); |
|
| 45 | + } |
|
| 46 | + } |
|
| 47 | + } |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + if (!$anyMissing) { |
|
| 51 | + $output->writeln('<info>All dependencies are met</info>'); |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + return self::SUCCESS; |
|
| 55 | + } |
|
| 56 | 56 | } |
@@ -23,319 +23,319 @@ |
||
| 23 | 23 | */ |
| 24 | 24 | class BackendService { |
| 25 | 25 | |
| 26 | - /** Visibility constants for VisibilityTrait */ |
|
| 27 | - public const VISIBILITY_NONE = 0; |
|
| 28 | - public const VISIBILITY_PERSONAL = 1; |
|
| 29 | - public const VISIBILITY_ADMIN = 2; |
|
| 30 | - //const VISIBILITY_ALIENS = 4; |
|
| 31 | - |
|
| 32 | - public const VISIBILITY_DEFAULT = 3; // PERSONAL | ADMIN |
|
| 33 | - |
|
| 34 | - /** Priority constants for PriorityTrait */ |
|
| 35 | - public const PRIORITY_DEFAULT = 100; |
|
| 36 | - |
|
| 37 | - /** @var bool */ |
|
| 38 | - private $userMountingAllowed = true; |
|
| 39 | - |
|
| 40 | - /** @var string[] */ |
|
| 41 | - private $userMountingBackends = []; |
|
| 42 | - |
|
| 43 | - /** @var Backend[] */ |
|
| 44 | - private $backends = []; |
|
| 45 | - |
|
| 46 | - /** @var IBackendProvider[] */ |
|
| 47 | - private $backendProviders = []; |
|
| 48 | - |
|
| 49 | - /** @var AuthMechanism[] */ |
|
| 50 | - private $authMechanisms = []; |
|
| 51 | - |
|
| 52 | - /** @var IAuthMechanismProvider[] */ |
|
| 53 | - private $authMechanismProviders = []; |
|
| 54 | - |
|
| 55 | - /** @var callable[] */ |
|
| 56 | - private $configHandlerLoaders = []; |
|
| 57 | - |
|
| 58 | - private $configHandlers = []; |
|
| 59 | - |
|
| 60 | - public function __construct( |
|
| 61 | - protected IAppConfig $appConfig, |
|
| 62 | - ) { |
|
| 63 | - // Load config values |
|
| 64 | - $this->userMountingAllowed = $appConfig->getValueBool('files_external', ConfigLexicon::ALLOW_USER_MOUNTING); |
|
| 65 | - $this->userMountingBackends = explode(',', $appConfig->getValueString('files_external', ConfigLexicon::USER_MOUNTING_BACKENDS)); |
|
| 66 | - |
|
| 67 | - // if no backend is in the list an empty string is in the array and user mounting is disabled |
|
| 68 | - if ($this->userMountingBackends === ['']) { |
|
| 69 | - $this->userMountingAllowed = false; |
|
| 70 | - } |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Register a backend provider |
|
| 75 | - * |
|
| 76 | - * @since 9.1.0 |
|
| 77 | - * @param IBackendProvider $provider |
|
| 78 | - */ |
|
| 79 | - public function registerBackendProvider(IBackendProvider $provider) { |
|
| 80 | - $this->backendProviders[] = $provider; |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - private function callForRegistrations() { |
|
| 84 | - static $eventSent = false; |
|
| 85 | - if (!$eventSent) { |
|
| 86 | - Server::get(IEventDispatcher::class)->dispatch( |
|
| 87 | - 'OCA\\Files_External::loadAdditionalBackends', |
|
| 88 | - new GenericEvent() |
|
| 89 | - ); |
|
| 90 | - $eventSent = true; |
|
| 91 | - } |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - private function loadBackendProviders() { |
|
| 95 | - $this->callForRegistrations(); |
|
| 96 | - foreach ($this->backendProviders as $provider) { |
|
| 97 | - $this->registerBackends($provider->getBackends()); |
|
| 98 | - } |
|
| 99 | - $this->backendProviders = []; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Register an auth mechanism provider |
|
| 104 | - * |
|
| 105 | - * @since 9.1.0 |
|
| 106 | - * @param IAuthMechanismProvider $provider |
|
| 107 | - */ |
|
| 108 | - public function registerAuthMechanismProvider(IAuthMechanismProvider $provider) { |
|
| 109 | - $this->authMechanismProviders[] = $provider; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - private function loadAuthMechanismProviders() { |
|
| 113 | - $this->callForRegistrations(); |
|
| 114 | - foreach ($this->authMechanismProviders as $provider) { |
|
| 115 | - $this->registerAuthMechanisms($provider->getAuthMechanisms()); |
|
| 116 | - } |
|
| 117 | - $this->authMechanismProviders = []; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * Register a backend |
|
| 122 | - * |
|
| 123 | - * @deprecated 9.1.0 use registerBackendProvider() |
|
| 124 | - * @param Backend $backend |
|
| 125 | - */ |
|
| 126 | - public function registerBackend(Backend $backend) { |
|
| 127 | - if (!$this->isAllowedUserBackend($backend)) { |
|
| 128 | - $backend->removeVisibility(BackendService::VISIBILITY_PERSONAL); |
|
| 129 | - } |
|
| 130 | - foreach ($backend->getIdentifierAliases() as $alias) { |
|
| 131 | - $this->backends[$alias] = $backend; |
|
| 132 | - } |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * @deprecated 9.1.0 use registerBackendProvider() |
|
| 137 | - * @param Backend[] $backends |
|
| 138 | - */ |
|
| 139 | - public function registerBackends(array $backends) { |
|
| 140 | - foreach ($backends as $backend) { |
|
| 141 | - $this->registerBackend($backend); |
|
| 142 | - } |
|
| 143 | - } |
|
| 144 | - /** |
|
| 145 | - * Register an authentication mechanism |
|
| 146 | - * |
|
| 147 | - * @deprecated 9.1.0 use registerAuthMechanismProvider() |
|
| 148 | - * @param AuthMechanism $authMech |
|
| 149 | - */ |
|
| 150 | - public function registerAuthMechanism(AuthMechanism $authMech) { |
|
| 151 | - if (!$this->isAllowedAuthMechanism($authMech)) { |
|
| 152 | - $authMech->removeVisibility(BackendService::VISIBILITY_PERSONAL); |
|
| 153 | - } |
|
| 154 | - foreach ($authMech->getIdentifierAliases() as $alias) { |
|
| 155 | - $this->authMechanisms[$alias] = $authMech; |
|
| 156 | - } |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * @deprecated 9.1.0 use registerAuthMechanismProvider() |
|
| 161 | - * @param AuthMechanism[] $mechanisms |
|
| 162 | - */ |
|
| 163 | - public function registerAuthMechanisms(array $mechanisms) { |
|
| 164 | - foreach ($mechanisms as $mechanism) { |
|
| 165 | - $this->registerAuthMechanism($mechanism); |
|
| 166 | - } |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * Get all backends |
|
| 171 | - * |
|
| 172 | - * @return Backend[] |
|
| 173 | - */ |
|
| 174 | - public function getBackends() { |
|
| 175 | - $this->loadBackendProviders(); |
|
| 176 | - // only return real identifiers, no aliases |
|
| 177 | - $backends = []; |
|
| 178 | - foreach ($this->backends as $backend) { |
|
| 179 | - $backends[$backend->getIdentifier()] = $backend; |
|
| 180 | - } |
|
| 181 | - return $backends; |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - /** |
|
| 185 | - * Get all available backends |
|
| 186 | - * |
|
| 187 | - * @return Backend[] |
|
| 188 | - */ |
|
| 189 | - public function getAvailableBackends() { |
|
| 190 | - return array_filter($this->getBackends(), function ($backend) { |
|
| 191 | - $missing = array_filter($backend->checkDependencies(), fn (MissingDependency $dependency) => !$dependency->isOptional()); |
|
| 192 | - return count($missing) === 0; |
|
| 193 | - }); |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - /** |
|
| 197 | - * @param string $identifier |
|
| 198 | - * @return Backend|null |
|
| 199 | - */ |
|
| 200 | - public function getBackend($identifier) { |
|
| 201 | - $this->loadBackendProviders(); |
|
| 202 | - if (isset($this->backends[$identifier])) { |
|
| 203 | - return $this->backends[$identifier]; |
|
| 204 | - } |
|
| 205 | - return null; |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - /** |
|
| 209 | - * Get all authentication mechanisms |
|
| 210 | - * |
|
| 211 | - * @return AuthMechanism[] |
|
| 212 | - */ |
|
| 213 | - public function getAuthMechanisms() { |
|
| 214 | - $this->loadAuthMechanismProviders(); |
|
| 215 | - // only return real identifiers, no aliases |
|
| 216 | - $mechanisms = []; |
|
| 217 | - foreach ($this->authMechanisms as $mechanism) { |
|
| 218 | - $mechanisms[$mechanism->getIdentifier()] = $mechanism; |
|
| 219 | - } |
|
| 220 | - return $mechanisms; |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - /** |
|
| 224 | - * Get all authentication mechanisms for schemes |
|
| 225 | - * |
|
| 226 | - * @param string[] $schemes |
|
| 227 | - * @return AuthMechanism[] |
|
| 228 | - */ |
|
| 229 | - public function getAuthMechanismsByScheme(array $schemes) { |
|
| 230 | - return array_filter($this->getAuthMechanisms(), function ($authMech) use ($schemes) { |
|
| 231 | - return in_array($authMech->getScheme(), $schemes, true); |
|
| 232 | - }); |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - /** |
|
| 236 | - * @param string $identifier |
|
| 237 | - * @return AuthMechanism|null |
|
| 238 | - */ |
|
| 239 | - public function getAuthMechanism($identifier) { |
|
| 240 | - $this->loadAuthMechanismProviders(); |
|
| 241 | - if (isset($this->authMechanisms[$identifier])) { |
|
| 242 | - return $this->authMechanisms[$identifier]; |
|
| 243 | - } |
|
| 244 | - return null; |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - /** |
|
| 248 | - * @return bool |
|
| 249 | - */ |
|
| 250 | - public function isUserMountingAllowed() { |
|
| 251 | - return $this->userMountingAllowed; |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - /** |
|
| 255 | - * Check a backend if a user is allowed to mount it |
|
| 256 | - * |
|
| 257 | - * @param Backend $backend |
|
| 258 | - * @return bool |
|
| 259 | - */ |
|
| 260 | - protected function isAllowedUserBackend(Backend $backend) { |
|
| 261 | - if ($this->userMountingAllowed && |
|
| 262 | - array_intersect($backend->getIdentifierAliases(), $this->userMountingBackends) |
|
| 263 | - ) { |
|
| 264 | - return true; |
|
| 265 | - } |
|
| 266 | - return false; |
|
| 267 | - } |
|
| 268 | - |
|
| 269 | - /** |
|
| 270 | - * Check an authentication mechanism if a user is allowed to use it |
|
| 271 | - * |
|
| 272 | - * @param AuthMechanism $authMechanism |
|
| 273 | - * @return bool |
|
| 274 | - */ |
|
| 275 | - protected function isAllowedAuthMechanism(AuthMechanism $authMechanism) { |
|
| 276 | - return true; // not implemented |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - /** |
|
| 280 | - * registers a configuration handler |
|
| 281 | - * |
|
| 282 | - * The function of the provided $placeholder is mostly to act a sorting |
|
| 283 | - * criteria, so longer placeholders are replaced first. This avoids |
|
| 284 | - * "$user" overwriting parts of "$userMail" and "$userLang", for example. |
|
| 285 | - * The provided value should not contain the $ prefix, only a-z0-9 are |
|
| 286 | - * allowed. Upper case letters are lower cased, the replacement is case- |
|
| 287 | - * insensitive. |
|
| 288 | - * |
|
| 289 | - * The configHandlerLoader should just instantiate the handler on demand. |
|
| 290 | - * For now all handlers are instantiated when a mount is loaded, independent |
|
| 291 | - * of whether the placeholder is present or not. This may change in future. |
|
| 292 | - * |
|
| 293 | - * @since 16.0.0 |
|
| 294 | - */ |
|
| 295 | - public function registerConfigHandler(string $placeholder, callable $configHandlerLoader) { |
|
| 296 | - $placeholder = trim(strtolower($placeholder)); |
|
| 297 | - if (!(bool)\preg_match('/^[a-z0-9]*$/', $placeholder)) { |
|
| 298 | - throw new \RuntimeException(sprintf( |
|
| 299 | - 'Invalid placeholder %s, only [a-z0-9] are allowed', $placeholder |
|
| 300 | - )); |
|
| 301 | - } |
|
| 302 | - if ($placeholder === '') { |
|
| 303 | - throw new \RuntimeException('Invalid empty placeholder'); |
|
| 304 | - } |
|
| 305 | - if (isset($this->configHandlerLoaders[$placeholder]) || isset($this->configHandlers[$placeholder])) { |
|
| 306 | - throw new \RuntimeException(sprintf('A handler is already registered for %s', $placeholder)); |
|
| 307 | - } |
|
| 308 | - $this->configHandlerLoaders[$placeholder] = $configHandlerLoader; |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - protected function loadConfigHandlers():void { |
|
| 312 | - $this->callForRegistrations(); |
|
| 313 | - $newLoaded = false; |
|
| 314 | - foreach ($this->configHandlerLoaders as $placeholder => $loader) { |
|
| 315 | - $handler = $loader(); |
|
| 316 | - if (!$handler instanceof IConfigHandler) { |
|
| 317 | - throw new \RuntimeException(sprintf( |
|
| 318 | - 'Handler for %s is not an instance of IConfigHandler', $placeholder |
|
| 319 | - )); |
|
| 320 | - } |
|
| 321 | - $this->configHandlers[$placeholder] = $handler; |
|
| 322 | - $newLoaded = true; |
|
| 323 | - } |
|
| 324 | - $this->configHandlerLoaders = []; |
|
| 325 | - if ($newLoaded) { |
|
| 326 | - // ensure those with longest placeholders come first, |
|
| 327 | - // to avoid substring matches |
|
| 328 | - uksort($this->configHandlers, function ($phA, $phB) { |
|
| 329 | - return strlen($phB) <=> strlen($phA); |
|
| 330 | - }); |
|
| 331 | - } |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - /** |
|
| 335 | - * @since 16.0.0 |
|
| 336 | - */ |
|
| 337 | - public function getConfigHandlers() { |
|
| 338 | - $this->loadConfigHandlers(); |
|
| 339 | - return $this->configHandlers; |
|
| 340 | - } |
|
| 26 | + /** Visibility constants for VisibilityTrait */ |
|
| 27 | + public const VISIBILITY_NONE = 0; |
|
| 28 | + public const VISIBILITY_PERSONAL = 1; |
|
| 29 | + public const VISIBILITY_ADMIN = 2; |
|
| 30 | + //const VISIBILITY_ALIENS = 4; |
|
| 31 | + |
|
| 32 | + public const VISIBILITY_DEFAULT = 3; // PERSONAL | ADMIN |
|
| 33 | + |
|
| 34 | + /** Priority constants for PriorityTrait */ |
|
| 35 | + public const PRIORITY_DEFAULT = 100; |
|
| 36 | + |
|
| 37 | + /** @var bool */ |
|
| 38 | + private $userMountingAllowed = true; |
|
| 39 | + |
|
| 40 | + /** @var string[] */ |
|
| 41 | + private $userMountingBackends = []; |
|
| 42 | + |
|
| 43 | + /** @var Backend[] */ |
|
| 44 | + private $backends = []; |
|
| 45 | + |
|
| 46 | + /** @var IBackendProvider[] */ |
|
| 47 | + private $backendProviders = []; |
|
| 48 | + |
|
| 49 | + /** @var AuthMechanism[] */ |
|
| 50 | + private $authMechanisms = []; |
|
| 51 | + |
|
| 52 | + /** @var IAuthMechanismProvider[] */ |
|
| 53 | + private $authMechanismProviders = []; |
|
| 54 | + |
|
| 55 | + /** @var callable[] */ |
|
| 56 | + private $configHandlerLoaders = []; |
|
| 57 | + |
|
| 58 | + private $configHandlers = []; |
|
| 59 | + |
|
| 60 | + public function __construct( |
|
| 61 | + protected IAppConfig $appConfig, |
|
| 62 | + ) { |
|
| 63 | + // Load config values |
|
| 64 | + $this->userMountingAllowed = $appConfig->getValueBool('files_external', ConfigLexicon::ALLOW_USER_MOUNTING); |
|
| 65 | + $this->userMountingBackends = explode(',', $appConfig->getValueString('files_external', ConfigLexicon::USER_MOUNTING_BACKENDS)); |
|
| 66 | + |
|
| 67 | + // if no backend is in the list an empty string is in the array and user mounting is disabled |
|
| 68 | + if ($this->userMountingBackends === ['']) { |
|
| 69 | + $this->userMountingAllowed = false; |
|
| 70 | + } |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Register a backend provider |
|
| 75 | + * |
|
| 76 | + * @since 9.1.0 |
|
| 77 | + * @param IBackendProvider $provider |
|
| 78 | + */ |
|
| 79 | + public function registerBackendProvider(IBackendProvider $provider) { |
|
| 80 | + $this->backendProviders[] = $provider; |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + private function callForRegistrations() { |
|
| 84 | + static $eventSent = false; |
|
| 85 | + if (!$eventSent) { |
|
| 86 | + Server::get(IEventDispatcher::class)->dispatch( |
|
| 87 | + 'OCA\\Files_External::loadAdditionalBackends', |
|
| 88 | + new GenericEvent() |
|
| 89 | + ); |
|
| 90 | + $eventSent = true; |
|
| 91 | + } |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + private function loadBackendProviders() { |
|
| 95 | + $this->callForRegistrations(); |
|
| 96 | + foreach ($this->backendProviders as $provider) { |
|
| 97 | + $this->registerBackends($provider->getBackends()); |
|
| 98 | + } |
|
| 99 | + $this->backendProviders = []; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Register an auth mechanism provider |
|
| 104 | + * |
|
| 105 | + * @since 9.1.0 |
|
| 106 | + * @param IAuthMechanismProvider $provider |
|
| 107 | + */ |
|
| 108 | + public function registerAuthMechanismProvider(IAuthMechanismProvider $provider) { |
|
| 109 | + $this->authMechanismProviders[] = $provider; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + private function loadAuthMechanismProviders() { |
|
| 113 | + $this->callForRegistrations(); |
|
| 114 | + foreach ($this->authMechanismProviders as $provider) { |
|
| 115 | + $this->registerAuthMechanisms($provider->getAuthMechanisms()); |
|
| 116 | + } |
|
| 117 | + $this->authMechanismProviders = []; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * Register a backend |
|
| 122 | + * |
|
| 123 | + * @deprecated 9.1.0 use registerBackendProvider() |
|
| 124 | + * @param Backend $backend |
|
| 125 | + */ |
|
| 126 | + public function registerBackend(Backend $backend) { |
|
| 127 | + if (!$this->isAllowedUserBackend($backend)) { |
|
| 128 | + $backend->removeVisibility(BackendService::VISIBILITY_PERSONAL); |
|
| 129 | + } |
|
| 130 | + foreach ($backend->getIdentifierAliases() as $alias) { |
|
| 131 | + $this->backends[$alias] = $backend; |
|
| 132 | + } |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * @deprecated 9.1.0 use registerBackendProvider() |
|
| 137 | + * @param Backend[] $backends |
|
| 138 | + */ |
|
| 139 | + public function registerBackends(array $backends) { |
|
| 140 | + foreach ($backends as $backend) { |
|
| 141 | + $this->registerBackend($backend); |
|
| 142 | + } |
|
| 143 | + } |
|
| 144 | + /** |
|
| 145 | + * Register an authentication mechanism |
|
| 146 | + * |
|
| 147 | + * @deprecated 9.1.0 use registerAuthMechanismProvider() |
|
| 148 | + * @param AuthMechanism $authMech |
|
| 149 | + */ |
|
| 150 | + public function registerAuthMechanism(AuthMechanism $authMech) { |
|
| 151 | + if (!$this->isAllowedAuthMechanism($authMech)) { |
|
| 152 | + $authMech->removeVisibility(BackendService::VISIBILITY_PERSONAL); |
|
| 153 | + } |
|
| 154 | + foreach ($authMech->getIdentifierAliases() as $alias) { |
|
| 155 | + $this->authMechanisms[$alias] = $authMech; |
|
| 156 | + } |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * @deprecated 9.1.0 use registerAuthMechanismProvider() |
|
| 161 | + * @param AuthMechanism[] $mechanisms |
|
| 162 | + */ |
|
| 163 | + public function registerAuthMechanisms(array $mechanisms) { |
|
| 164 | + foreach ($mechanisms as $mechanism) { |
|
| 165 | + $this->registerAuthMechanism($mechanism); |
|
| 166 | + } |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * Get all backends |
|
| 171 | + * |
|
| 172 | + * @return Backend[] |
|
| 173 | + */ |
|
| 174 | + public function getBackends() { |
|
| 175 | + $this->loadBackendProviders(); |
|
| 176 | + // only return real identifiers, no aliases |
|
| 177 | + $backends = []; |
|
| 178 | + foreach ($this->backends as $backend) { |
|
| 179 | + $backends[$backend->getIdentifier()] = $backend; |
|
| 180 | + } |
|
| 181 | + return $backends; |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + /** |
|
| 185 | + * Get all available backends |
|
| 186 | + * |
|
| 187 | + * @return Backend[] |
|
| 188 | + */ |
|
| 189 | + public function getAvailableBackends() { |
|
| 190 | + return array_filter($this->getBackends(), function ($backend) { |
|
| 191 | + $missing = array_filter($backend->checkDependencies(), fn (MissingDependency $dependency) => !$dependency->isOptional()); |
|
| 192 | + return count($missing) === 0; |
|
| 193 | + }); |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + /** |
|
| 197 | + * @param string $identifier |
|
| 198 | + * @return Backend|null |
|
| 199 | + */ |
|
| 200 | + public function getBackend($identifier) { |
|
| 201 | + $this->loadBackendProviders(); |
|
| 202 | + if (isset($this->backends[$identifier])) { |
|
| 203 | + return $this->backends[$identifier]; |
|
| 204 | + } |
|
| 205 | + return null; |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + /** |
|
| 209 | + * Get all authentication mechanisms |
|
| 210 | + * |
|
| 211 | + * @return AuthMechanism[] |
|
| 212 | + */ |
|
| 213 | + public function getAuthMechanisms() { |
|
| 214 | + $this->loadAuthMechanismProviders(); |
|
| 215 | + // only return real identifiers, no aliases |
|
| 216 | + $mechanisms = []; |
|
| 217 | + foreach ($this->authMechanisms as $mechanism) { |
|
| 218 | + $mechanisms[$mechanism->getIdentifier()] = $mechanism; |
|
| 219 | + } |
|
| 220 | + return $mechanisms; |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + /** |
|
| 224 | + * Get all authentication mechanisms for schemes |
|
| 225 | + * |
|
| 226 | + * @param string[] $schemes |
|
| 227 | + * @return AuthMechanism[] |
|
| 228 | + */ |
|
| 229 | + public function getAuthMechanismsByScheme(array $schemes) { |
|
| 230 | + return array_filter($this->getAuthMechanisms(), function ($authMech) use ($schemes) { |
|
| 231 | + return in_array($authMech->getScheme(), $schemes, true); |
|
| 232 | + }); |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + /** |
|
| 236 | + * @param string $identifier |
|
| 237 | + * @return AuthMechanism|null |
|
| 238 | + */ |
|
| 239 | + public function getAuthMechanism($identifier) { |
|
| 240 | + $this->loadAuthMechanismProviders(); |
|
| 241 | + if (isset($this->authMechanisms[$identifier])) { |
|
| 242 | + return $this->authMechanisms[$identifier]; |
|
| 243 | + } |
|
| 244 | + return null; |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + /** |
|
| 248 | + * @return bool |
|
| 249 | + */ |
|
| 250 | + public function isUserMountingAllowed() { |
|
| 251 | + return $this->userMountingAllowed; |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + /** |
|
| 255 | + * Check a backend if a user is allowed to mount it |
|
| 256 | + * |
|
| 257 | + * @param Backend $backend |
|
| 258 | + * @return bool |
|
| 259 | + */ |
|
| 260 | + protected function isAllowedUserBackend(Backend $backend) { |
|
| 261 | + if ($this->userMountingAllowed && |
|
| 262 | + array_intersect($backend->getIdentifierAliases(), $this->userMountingBackends) |
|
| 263 | + ) { |
|
| 264 | + return true; |
|
| 265 | + } |
|
| 266 | + return false; |
|
| 267 | + } |
|
| 268 | + |
|
| 269 | + /** |
|
| 270 | + * Check an authentication mechanism if a user is allowed to use it |
|
| 271 | + * |
|
| 272 | + * @param AuthMechanism $authMechanism |
|
| 273 | + * @return bool |
|
| 274 | + */ |
|
| 275 | + protected function isAllowedAuthMechanism(AuthMechanism $authMechanism) { |
|
| 276 | + return true; // not implemented |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + /** |
|
| 280 | + * registers a configuration handler |
|
| 281 | + * |
|
| 282 | + * The function of the provided $placeholder is mostly to act a sorting |
|
| 283 | + * criteria, so longer placeholders are replaced first. This avoids |
|
| 284 | + * "$user" overwriting parts of "$userMail" and "$userLang", for example. |
|
| 285 | + * The provided value should not contain the $ prefix, only a-z0-9 are |
|
| 286 | + * allowed. Upper case letters are lower cased, the replacement is case- |
|
| 287 | + * insensitive. |
|
| 288 | + * |
|
| 289 | + * The configHandlerLoader should just instantiate the handler on demand. |
|
| 290 | + * For now all handlers are instantiated when a mount is loaded, independent |
|
| 291 | + * of whether the placeholder is present or not. This may change in future. |
|
| 292 | + * |
|
| 293 | + * @since 16.0.0 |
|
| 294 | + */ |
|
| 295 | + public function registerConfigHandler(string $placeholder, callable $configHandlerLoader) { |
|
| 296 | + $placeholder = trim(strtolower($placeholder)); |
|
| 297 | + if (!(bool)\preg_match('/^[a-z0-9]*$/', $placeholder)) { |
|
| 298 | + throw new \RuntimeException(sprintf( |
|
| 299 | + 'Invalid placeholder %s, only [a-z0-9] are allowed', $placeholder |
|
| 300 | + )); |
|
| 301 | + } |
|
| 302 | + if ($placeholder === '') { |
|
| 303 | + throw new \RuntimeException('Invalid empty placeholder'); |
|
| 304 | + } |
|
| 305 | + if (isset($this->configHandlerLoaders[$placeholder]) || isset($this->configHandlers[$placeholder])) { |
|
| 306 | + throw new \RuntimeException(sprintf('A handler is already registered for %s', $placeholder)); |
|
| 307 | + } |
|
| 308 | + $this->configHandlerLoaders[$placeholder] = $configHandlerLoader; |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + protected function loadConfigHandlers():void { |
|
| 312 | + $this->callForRegistrations(); |
|
| 313 | + $newLoaded = false; |
|
| 314 | + foreach ($this->configHandlerLoaders as $placeholder => $loader) { |
|
| 315 | + $handler = $loader(); |
|
| 316 | + if (!$handler instanceof IConfigHandler) { |
|
| 317 | + throw new \RuntimeException(sprintf( |
|
| 318 | + 'Handler for %s is not an instance of IConfigHandler', $placeholder |
|
| 319 | + )); |
|
| 320 | + } |
|
| 321 | + $this->configHandlers[$placeholder] = $handler; |
|
| 322 | + $newLoaded = true; |
|
| 323 | + } |
|
| 324 | + $this->configHandlerLoaders = []; |
|
| 325 | + if ($newLoaded) { |
|
| 326 | + // ensure those with longest placeholders come first, |
|
| 327 | + // to avoid substring matches |
|
| 328 | + uksort($this->configHandlers, function ($phA, $phB) { |
|
| 329 | + return strlen($phB) <=> strlen($phA); |
|
| 330 | + }); |
|
| 331 | + } |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + /** |
|
| 335 | + * @since 16.0.0 |
|
| 336 | + */ |
|
| 337 | + public function getConfigHandlers() { |
|
| 338 | + $this->loadConfigHandlers(); |
|
| 339 | + return $this->configHandlers; |
|
| 340 | + } |
|
| 341 | 341 | } |