Skip to content

Commit

Permalink
lazy AppConfig
Browse files Browse the repository at this point in the history
Signed-off-by: Maxence Lange <[email protected]>
  • Loading branch information
ArtificialOwl committed Jan 11, 2024
1 parent d8e625f commit d0c05cd
Show file tree
Hide file tree
Showing 24 changed files with 2,460 additions and 450 deletions.
65 changes: 19 additions & 46 deletions apps/provisioning_api/lib/Controller/AppConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
*/
namespace OCA\Provisioning_API\Controller;

use OC\AppConfig;
use OC\AppFramework\Middleware\Security\Exceptions\NotAdminException;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCSController;
use OCP\IAppConfig;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IL10N;
use OCP\IRequest;
Expand All @@ -42,46 +42,17 @@
use OCP\Settings\IManager;

class AppConfigController extends OCSController {

/** @var IConfig */
protected $config;

/** @var IAppConfig */
protected $appConfig;

/** @var IUserSession */
private $userSession;

/** @var IL10N */
private $l10n;

/** @var IGroupManager */
private $groupManager;

/** @var IManager */
private $settingManager;

/**
* @param string $appName
* @param IRequest $request
* @param IConfig $config
* @param IAppConfig $appConfig
*/
public function __construct(string $appName,
public function __construct(
string $appName,
IRequest $request,
IConfig $config,
IAppConfig $appConfig,
IUserSession $userSession,
IL10N $l10n,
IGroupManager $groupManager,
IManager $settingManager) {
/** @var AppConfig */
private IAppConfig $appConfig,
private IUserSession $userSession,
private IL10N $l10n,
private IGroupManager $groupManager,
private IManager $settingManager,
) {
parent::__construct($appName, $request);
$this->config = $config;
$this->appConfig = $appConfig;
$this->userSession = $userSession;
$this->l10n = $l10n;
$this->groupManager = $groupManager;
$this->settingManager = $settingManager;
}

/**
Expand Down Expand Up @@ -113,7 +84,7 @@ public function getKeys(string $app): DataResponse {
return new DataResponse(['data' => ['message' => $e->getMessage()]], Http::STATUS_FORBIDDEN);
}
return new DataResponse([
'data' => $this->config->getAppKeys($app),
'data' => $this->appConfig->getKeys($app),
]);
}

Expand All @@ -134,9 +105,10 @@ public function getValue(string $app, string $key, string $defaultValue = ''): D
} catch (\InvalidArgumentException $e) {
return new DataResponse(['data' => ['message' => $e->getMessage()]], Http::STATUS_FORBIDDEN);
}
return new DataResponse([
'data' => $this->config->getAppValue($app, $key, $defaultValue),
]);

/** @psalm-suppress InternalMethod */
$value = $this->appConfig->getValueMixed($app, $key, $defaultValue, null);
return new DataResponse(['data' => $value]);
}

/**
Expand Down Expand Up @@ -171,7 +143,8 @@ public function setValue(string $app, string $key, string $value): DataResponse
return new DataResponse(['data' => ['message' => $e->getMessage()]], Http::STATUS_FORBIDDEN);
}

$this->config->setAppValue($app, $key, $value);
/** @psalm-suppress InternalMethod */
$this->appConfig->setValueMixed($app, $key, $value);
return new DataResponse();
}

Expand All @@ -195,7 +168,7 @@ public function deleteKey(string $app, string $key): DataResponse {
return new DataResponse(['data' => ['message' => $e->getMessage()]], Http::STATUS_FORBIDDEN);
}

$this->config->deleteAppValue($app, $key);
$this->appConfig->deleteKey($app, $key);
return new DataResponse();
}

Expand Down Expand Up @@ -231,7 +204,7 @@ protected function verifyConfigKey(string $app, string $key, string $value) {
if ($app === 'files'
&& $key === 'default_quota'
&& $value === 'none'
&& $this->config->getAppValue('files', 'allow_unlimited_quota', '1') === '0') {
&& $this->appConfig->getValueInt('files', 'allow_unlimited_quota', 1) === 0) {
throw new \InvalidArgumentException('The given key can not be set, unlimited quota is forbidden on this instance');
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,15 @@ public function testGetKeys($app, $keys, $throws, $status) {
->with($app)
->willThrowException($throws);

$this->config->expects($this->never())
->method('getAppKeys');
$this->appConfig->expects($this->never())
->method('getKeys');
} else {
$api->expects($this->once())
->method('verifyAppId')
->with($app);

$this->config->expects($this->once())
->method('getAppKeys')
$this->appConfig->expects($this->once())
->method('getKeys')
->with($app)
->willReturn($keys);
}
Expand Down
42 changes: 35 additions & 7 deletions core/Command/Config/App/GetConfig.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<?php

declare(strict_types=1);
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author Joas Schilling <[email protected]>
* @author Maxence Lange <[email protected]>
*
* @license AGPL-3.0
*
Expand All @@ -21,15 +24,16 @@
*/
namespace OC\Core\Command\Config\App;

use OCP\IConfig;
use OCP\Exceptions\AppConfigUnknownKeyException;
use OCP\IAppConfig;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class GetConfig extends Base {
public function __construct(
protected IConfig $config,
protected IAppConfig $appConfig,
) {
parent::__construct();
}
Expand All @@ -50,6 +54,12 @@ protected function configure() {
InputArgument::REQUIRED,
'Name of the config to get'
)
->addOption(
'details',
null,
InputOption::VALUE_NONE,
'returns complete details about the app config value'
)
->addOption(
'default-value',
null,
Expand All @@ -71,14 +81,32 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$configName = $input->getArgument('name');
$defaultValue = $input->getOption('default-value');

if (!in_array($configName, $this->config->getAppKeys($appName)) && !$input->hasParameterOption('--default-value')) {
return 1;
if ($input->getOption('details')) {
$details = $this->appConfig->getDetails($appName, $configName);
$format = $input->getOption('output') ?? 'plain';
if ($format === 'json') {
$output->writeln(json_encode($details));
} elseif ($format === 'json_pretty') {
$output->writeln(json_encode($details, JSON_PRETTY_PRINT));
} else {
$output->writeln('App: ' . $details['app'] ?? '');
$output->writeln('Config Key: ' . $details['key'] ?? '');
$output->writeln('Config Value: ' . $details['value'] ?? '');
$output->writeln('Value type: ' . $details['typeString'] ?? '');
$output->writeln('Lazy loaded: ' . (($details['lazy'] ?? false) ? 'Yes' : 'No'));
$output->writeln('Sensitive: ' . (($details['sensitive'] ?? false) ? 'Yes' : 'No'));
}

return 0;
}

if (!in_array($configName, $this->config->getAppKeys($appName))) {
try {
$configValue = $this->appConfig->getDetails($appName, $configName)['value'];
} catch (AppConfigUnknownKeyException $e) {
if (!$input->hasParameterOption('--default-value')) {
return 1;
}
$configValue = $defaultValue;
} else {
$configValue = $this->config->getAppValue($appName, $configName);
}

$this->writeMixedInOutputFormat($input, $output, $configValue);
Expand Down
Loading

0 comments on commit d0c05cd

Please sign in to comment.