Add a key parameter to the new initial state API

Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
This commit is contained in:
Christoph Wurst 2019-01-30 12:10:10 +01:00
parent 49ae3a3daa
commit 0e6cb988a1
No known key found for this signature in database
GPG key ID: CC42AC2A7F0E56D8
12 changed files with 154 additions and 40 deletions

View file

@ -157,7 +157,7 @@ class BackupCodesProvider implements IProvider, IProvidesPersonalSettings {
*/
public function getPersonalSettings(IUser $user): IPersonalProviderSettings {
$state = $this->storage->getBackupCodesState($user);
$this->initialStateService->provideInitialState($this->appName, $state);
$this->initialStateService->provideInitialState($this->appName, 'state', $state);
return new Personal();
}

View file

@ -4,7 +4,7 @@ import store from './store';
Vue.prototype.t = t;
const initialState = OCP.InitialState.loadState('twofactor_backupcodes');
const initialState = OCP.InitialState.loadState('twofactor_backupcodes', 'state');
store.replaceState(
initialState
)

View file

@ -4,6 +4,4 @@ script('twofactor_backupcodes', 'settings');
?>
<input type="hidden" id="twofactor-backupcodes-initial-state" value="<?php p($_['state']); ?>">
<div id="twofactor-backupcodes-settings"></div>

BIN
core/js/dist/main.js vendored

Binary file not shown.

Binary file not shown.

View file

@ -3,7 +3,7 @@
*/
import * as AppConfig from './appconfig'
import * as Comments from './comments'
import initialState from './initialstate'
import * as InitialState from './initialstate'
import Loader from './loader'
import * as WhatsNew from './whatsnew'
@ -11,7 +11,7 @@ import * as WhatsNew from './whatsnew'
export default {
AppConfig,
Comments,
InitialState: initialState,
InitialState,
Loader,
WhatsNew,
};

View file

@ -21,12 +21,11 @@
*/
/**
* @namespace OCP
* @class InitialState
* @namespace OCP.InitialState
*/
export default {
loadState: function(app) {
const elem = document.querySelector('#initial-state-' + app);
export function loadState (app, key) {
const elem = document.querySelector(`#initial-state-${app}-${key}`);
if (elem === null) {
console.error('Could not find initial state of ' + app);
throw new Error('Could not find initial state of ' + app);
@ -38,5 +37,4 @@ export default {
console.error('Could not parse initial state of ' + app);
throw new Error('Could not parse initial state of ' + app);
}
},
}

View file

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace OC;
use Closure;
use OCP\IInitialStateService;
use OCP\ILogger;
@ -32,43 +33,58 @@ class InitialStateService implements IInitialStateService {
/** @var ILogger */
private $logger;
/** @var array */
/** @var string[][] */
private $states = [];
/** @var array */
/** @var Closure[][] */
private $lazyStates = [];
public function __construct(ILogger $logger) {
$this->logger = $logger;
}
public function provideInitialState(string $appName, $data) {
public function provideInitialState(string $appName, string $key, $data): void {
// Scalars and JsonSerializable are fine
if (is_scalar($data) || $data instanceof \JsonSerializable || is_array($data)) {
$this->states[$appName] = json_encode($data);
if (!isset($this->states[$appName])) {
$this->states[$appName] = [];
}
$this->states[$appName][$key] = json_encode($data);
return;
}
$this->logger->warning('Invalid data provided to provideInitialState by ' . $appName);
}
public function provideLazyInitialState(string $appName, \Closure $closure) {
$this->lazyStates[$appName] = $closure;
public function provideLazyInitialState(string $appName, string $key, Closure $closure): void {
if (!isset($this->lazyStates[$appName])) {
$this->lazyStates[$appName] = [];
}
$this->lazyStates[$appName][$key] = $closure;
}
/**
* Invoke all callbacks to populate the `states` property
*/
private function invokeLazyStateCallbacks(): void {
foreach ($this->lazyStates as $app => $lazyStates) {
foreach ($lazyStates as $key => $lazyState) {
$this->provideInitialState($app, $key, $lazyState());
}
}
$this->lazyStates = [];
}
public function getInitialStates(): array {
$states = $this->states;
foreach ($this->lazyStates as $app => $lazyState) {
$state = $lazyState();
$this->invokeLazyStateCallbacks();
if (!($lazyState instanceof \JsonSerializable)) {
$this->logger->warning($app . ' provided an invalid lazy state');
$appStates = [];
foreach ($this->states as $app => $states) {
foreach ($states as $key => $value) {
$appStates["$app-$key"] = $value;
}
$states[$app] = json_encode($state);
}
return $states;
return $appStates;
}
}

View file

@ -24,6 +24,8 @@ declare(strict_types=1);
namespace OCP;
use Closure;
/**
* @since 16.0.0
*/
@ -36,9 +38,10 @@ interface IInitialStateService {
* @since 16.0.0
*
* @param string $appName
* @param string $key
* @param bool|int|float|string|array|\JsonSerializable $data
*/
public function provideInitialState(string $appName, $data);
public function provideInitialState(string $appName, string $key, $data): void;
/**
* Allows an app to provide its initial state via a lazy method.
@ -50,7 +53,8 @@ interface IInitialStateService {
* @since 16.0.0
*
* @param string $appName
* @param \Closure $closure Has to return an object that implements JsonSerializable
* @param string $key
* @param Closure $closure returns a primitive or an object that implements JsonSerializable
*/
public function provideLazyInitialState(string $appName, \Closure $closure);
public function provideLazyInitialState(string $appName, string $key, Closure $closure): void;
}

View file

@ -0,0 +1,98 @@
<?php
declare(strict_types=1);
/**
* @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* 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
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Test;
use function json_encode;
use JsonSerializable;
use OC\InitialStateService;
use OCP\ILogger;
use stdClass;
class InitialStateServiceTest extends TestCase {
/** @var InitialStateService */
private $service;
protected function setUp() {
parent::setUp();
$this->service = new InitialStateService(
$this->createMock(ILogger::class)
);
}
public function staticData() {
return [
['string'],
[23],
[2.3],
[new class implements JsonSerializable {
public function jsonSerialize() {
return 3;
}
}],
];
}
/**
* @dataProvider staticData
*/
public function testStaticData($value) {
$this->service->provideInitialState('test', 'key', $value);
$data = $this->service->getInitialStates();
$this->assertEquals(
['test-key' => json_encode($value)],
$data
);
}
public function testStaticButInvalidData() {
$this->service->provideInitialState('test', 'key', new stdClass());
$data = $this->service->getInitialStates();
$this->assertEquals(
[],
$data
);
}
/**
* @dataProvider staticData
*/
public function testLazyData($value) {
$this->service->provideLazyInitialState('test', 'key', function() use ($value) {
return $value;
});
$data = $this->service->getInitialStates();
$this->assertEquals(
['test-key' => json_encode($value)],
$data
);
}
}