Skip to content

Commit d0c05cd

Browse files
committed
lazy AppConfig
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
1 parent d8e625f commit d0c05cd

24 files changed

Lines changed: 2460 additions & 450 deletions

apps/provisioning_api/lib/Controller/AppConfigController.php

Lines changed: 19 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@
2727
*/
2828
namespace OCA\Provisioning_API\Controller;
2929

30+
use OC\AppConfig;
3031
use OC\AppFramework\Middleware\Security\Exceptions\NotAdminException;
3132
use OCP\AppFramework\Http;
3233
use OCP\AppFramework\Http\DataResponse;
3334
use OCP\AppFramework\OCSController;
3435
use OCP\IAppConfig;
35-
use OCP\IConfig;
3636
use OCP\IGroupManager;
3737
use OCP\IL10N;
3838
use OCP\IRequest;
@@ -42,46 +42,17 @@
4242
use OCP\Settings\IManager;
4343

4444
class AppConfigController extends OCSController {
45-
46-
/** @var IConfig */
47-
protected $config;
48-
49-
/** @var IAppConfig */
50-
protected $appConfig;
51-
52-
/** @var IUserSession */
53-
private $userSession;
54-
55-
/** @var IL10N */
56-
private $l10n;
57-
58-
/** @var IGroupManager */
59-
private $groupManager;
60-
61-
/** @var IManager */
62-
private $settingManager;
63-
64-
/**
65-
* @param string $appName
66-
* @param IRequest $request
67-
* @param IConfig $config
68-
* @param IAppConfig $appConfig
69-
*/
70-
public function __construct(string $appName,
45+
public function __construct(
46+
string $appName,
7147
IRequest $request,
72-
IConfig $config,
73-
IAppConfig $appConfig,
74-
IUserSession $userSession,
75-
IL10N $l10n,
76-
IGroupManager $groupManager,
77-
IManager $settingManager) {
48+
/** @var AppConfig */
49+
private IAppConfig $appConfig,
50+
private IUserSession $userSession,
51+
private IL10N $l10n,
52+
private IGroupManager $groupManager,
53+
private IManager $settingManager,
54+
) {
7855
parent::__construct($appName, $request);
79-
$this->config = $config;
80-
$this->appConfig = $appConfig;
81-
$this->userSession = $userSession;
82-
$this->l10n = $l10n;
83-
$this->groupManager = $groupManager;
84-
$this->settingManager = $settingManager;
8556
}
8657

8758
/**
@@ -113,7 +84,7 @@ public function getKeys(string $app): DataResponse {
11384
return new DataResponse(['data' => ['message' => $e->getMessage()]], Http::STATUS_FORBIDDEN);
11485
}
11586
return new DataResponse([
116-
'data' => $this->config->getAppKeys($app),
87+
'data' => $this->appConfig->getKeys($app),
11788
]);
11889
}
11990

@@ -134,9 +105,10 @@ public function getValue(string $app, string $key, string $defaultValue = ''): D
134105
} catch (\InvalidArgumentException $e) {
135106
return new DataResponse(['data' => ['message' => $e->getMessage()]], Http::STATUS_FORBIDDEN);
136107
}
137-
return new DataResponse([
138-
'data' => $this->config->getAppValue($app, $key, $defaultValue),
139-
]);
108+
109+
/** @psalm-suppress InternalMethod */
110+
$value = $this->appConfig->getValueMixed($app, $key, $defaultValue, null);
111+
return new DataResponse(['data' => $value]);
140112
}
141113

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

174-
$this->config->setAppValue($app, $key, $value);
146+
/** @psalm-suppress InternalMethod */
147+
$this->appConfig->setValueMixed($app, $key, $value);
175148
return new DataResponse();
176149
}
177150

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

198-
$this->config->deleteAppValue($app, $key);
171+
$this->appConfig->deleteKey($app, $key);
199172
return new DataResponse();
200173
}
201174

@@ -231,7 +204,7 @@ protected function verifyConfigKey(string $app, string $key, string $value) {
231204
if ($app === 'files'
232205
&& $key === 'default_quota'
233206
&& $value === 'none'
234-
&& $this->config->getAppValue('files', 'allow_unlimited_quota', '1') === '0') {
207+
&& $this->appConfig->getValueInt('files', 'allow_unlimited_quota', 1) === 0) {
235208
throw new \InvalidArgumentException('The given key can not be set, unlimited quota is forbidden on this instance');
236209
}
237210
}

apps/provisioning_api/tests/Controller/AppConfigControllerTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,15 @@ public function testGetKeys($app, $keys, $throws, $status) {
137137
->with($app)
138138
->willThrowException($throws);
139139

140-
$this->config->expects($this->never())
141-
->method('getAppKeys');
140+
$this->appConfig->expects($this->never())
141+
->method('getKeys');
142142
} else {
143143
$api->expects($this->once())
144144
->method('verifyAppId')
145145
->with($app);
146146

147-
$this->config->expects($this->once())
148-
->method('getAppKeys')
147+
$this->appConfig->expects($this->once())
148+
->method('getKeys')
149149
->with($app)
150150
->willReturn($keys);
151151
}

core/Command/Config/App/GetConfig.php

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
<?php
2+
3+
declare(strict_types=1);
24
/**
35
* @copyright Copyright (c) 2016, ownCloud, Inc.
46
*
57
* @author Joas Schilling <coding@schilljs.com>
8+
* @author Maxence Lange <maxence@artificial-owl.com>
69
*
710
* @license AGPL-3.0
811
*
@@ -21,15 +24,16 @@
2124
*/
2225
namespace OC\Core\Command\Config\App;
2326

24-
use OCP\IConfig;
27+
use OCP\Exceptions\AppConfigUnknownKeyException;
28+
use OCP\IAppConfig;
2529
use Symfony\Component\Console\Input\InputArgument;
2630
use Symfony\Component\Console\Input\InputInterface;
2731
use Symfony\Component\Console\Input\InputOption;
2832
use Symfony\Component\Console\Output\OutputInterface;
2933

3034
class GetConfig extends Base {
3135
public function __construct(
32-
protected IConfig $config,
36+
protected IAppConfig $appConfig,
3337
) {
3438
parent::__construct();
3539
}
@@ -50,6 +54,12 @@ protected function configure() {
5054
InputArgument::REQUIRED,
5155
'Name of the config to get'
5256
)
57+
->addOption(
58+
'details',
59+
null,
60+
InputOption::VALUE_NONE,
61+
'returns complete details about the app config value'
62+
)
5363
->addOption(
5464
'default-value',
5565
null,
@@ -71,14 +81,32 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7181
$configName = $input->getArgument('name');
7282
$defaultValue = $input->getOption('default-value');
7383

74-
if (!in_array($configName, $this->config->getAppKeys($appName)) && !$input->hasParameterOption('--default-value')) {
75-
return 1;
84+
if ($input->getOption('details')) {
85+
$details = $this->appConfig->getDetails($appName, $configName);
86+
$format = $input->getOption('output') ?? 'plain';
87+
if ($format === 'json') {
88+
$output->writeln(json_encode($details));
89+
} elseif ($format === 'json_pretty') {
90+
$output->writeln(json_encode($details, JSON_PRETTY_PRINT));
91+
} else {
92+
$output->writeln('App: ' . $details['app'] ?? '');
93+
$output->writeln('Config Key: ' . $details['key'] ?? '');
94+
$output->writeln('Config Value: ' . $details['value'] ?? '');
95+
$output->writeln('Value type: ' . $details['typeString'] ?? '');
96+
$output->writeln('Lazy loaded: ' . (($details['lazy'] ?? false) ? 'Yes' : 'No'));
97+
$output->writeln('Sensitive: ' . (($details['sensitive'] ?? false) ? 'Yes' : 'No'));
98+
}
99+
100+
return 0;
76101
}
77102

78-
if (!in_array($configName, $this->config->getAppKeys($appName))) {
103+
try {
104+
$configValue = $this->appConfig->getDetails($appName, $configName)['value'];
105+
} catch (AppConfigUnknownKeyException $e) {
106+
if (!$input->hasParameterOption('--default-value')) {
107+
return 1;
108+
}
79109
$configValue = $defaultValue;
80-
} else {
81-
$configValue = $this->config->getAppValue($appName, $configName);
82110
}
83111

84112
$this->writeMixedInOutputFormat($input, $output, $configValue);

0 commit comments

Comments
 (0)