diff options
author | Christoph Wurst <christoph@winzerhof-wurst.at> | 2019-01-30 12:10:10 +0100 |
---|---|---|
committer | Christoph Wurst <christoph@winzerhof-wurst.at> | 2019-01-30 16:08:08 +0100 |
commit | 0e6cb988a1cb6e0bc94053816ebb7bdc66e871af (patch) | |
tree | a7abf657c978cb6faedc918e41d3b6b8e64c26b6 /lib/private/InitialStateService.php | |
parent | 49ae3a3daa3ea479902386792b34359cbc8a8fdf (diff) | |
download | nextcloud-server-0e6cb988a1cb6e0bc94053816ebb7bdc66e871af.tar.gz nextcloud-server-0e6cb988a1cb6e0bc94053816ebb7bdc66e871af.zip |
Add a key parameter to the new initial state API
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Diffstat (limited to 'lib/private/InitialStateService.php')
-rw-r--r-- | lib/private/InitialStateService.php | 46 |
1 files changed, 31 insertions, 15 deletions
diff --git a/lib/private/InitialStateService.php b/lib/private/InitialStateService.php index df7f94fbda1..6db264bb981 100644 --- a/lib/private/InitialStateService.php +++ b/lib/private/InitialStateService.php @@ -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; } } |