InitialStateService.php 2.5 KB

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