InitialStateService.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC;
  25. use Closure;
  26. use OCP\IInitialStateService;
  27. use OCP\ILogger;
  28. class InitialStateService implements IInitialStateService {
  29. /** @var ILogger */
  30. private $logger;
  31. /** @var string[][] */
  32. private $states = [];
  33. /** @var Closure[][] */
  34. private $lazyStates = [];
  35. public function __construct(ILogger $logger) {
  36. $this->logger = $logger;
  37. }
  38. public function provideInitialState(string $appName, string $key, $data): void {
  39. // Scalars and JsonSerializable are fine
  40. if (is_scalar($data) || $data instanceof \JsonSerializable || is_array($data)) {
  41. if (!isset($this->states[$appName])) {
  42. $this->states[$appName] = [];
  43. }
  44. $this->states[$appName][$key] = json_encode($data);
  45. return;
  46. }
  47. $this->logger->warning('Invalid data provided to provideInitialState by ' . $appName);
  48. }
  49. public function provideLazyInitialState(string $appName, string $key, Closure $closure): void {
  50. if (!isset($this->lazyStates[$appName])) {
  51. $this->lazyStates[$appName] = [];
  52. }
  53. $this->lazyStates[$appName][$key] = $closure;
  54. }
  55. /**
  56. * Invoke all callbacks to populate the `states` property
  57. */
  58. private function invokeLazyStateCallbacks(): void {
  59. foreach ($this->lazyStates as $app => $lazyStates) {
  60. foreach ($lazyStates as $key => $lazyState) {
  61. $this->provideInitialState($app, $key, $lazyState());
  62. }
  63. }
  64. $this->lazyStates = [];
  65. }
  66. public function getInitialStates(): array {
  67. $this->invokeLazyStateCallbacks();
  68. $appStates = [];
  69. foreach ($this->states as $app => $states) {
  70. foreach ($states as $key => $value) {
  71. $appStates["$app-$key"] = $value;
  72. }
  73. }
  74. return $appStates;
  75. }
  76. }