InitialStateService.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC;
  8. use Closure;
  9. use OC\AppFramework\Bootstrap\Coordinator;
  10. use OCP\AppFramework\QueryException;
  11. use OCP\AppFramework\Services\InitialStateProvider;
  12. use OCP\IInitialStateService;
  13. use OCP\IServerContainer;
  14. use Psr\Log\LoggerInterface;
  15. class InitialStateService implements IInitialStateService {
  16. /** @var LoggerInterface */
  17. private $logger;
  18. /** @var string[][] */
  19. private $states = [];
  20. /** @var Closure[][] */
  21. private $lazyStates = [];
  22. /** @var Coordinator */
  23. private $bootstrapCoordinator;
  24. /** @var IServerContainer */
  25. private $container;
  26. public function __construct(LoggerInterface $logger, Coordinator $bootstrapCoordinator, IServerContainer $container) {
  27. $this->logger = $logger;
  28. $this->bootstrapCoordinator = $bootstrapCoordinator;
  29. $this->container = $container;
  30. }
  31. public function provideInitialState(string $appName, string $key, $data): void {
  32. // Scalars and JsonSerializable are fine
  33. if (is_scalar($data) || $data instanceof \JsonSerializable || is_array($data)) {
  34. if (!isset($this->states[$appName])) {
  35. $this->states[$appName] = [];
  36. }
  37. try {
  38. $this->states[$appName][$key] = json_encode($data, JSON_THROW_ON_ERROR);
  39. } catch (\JsonException $e) {
  40. $this->logger->error('Invalid '. $key . ' data provided to provideInitialState by ' . $appName, ['exception' => $e]);
  41. }
  42. return;
  43. }
  44. $this->logger->warning('Invalid '. $key . ' data provided to provideInitialState by ' . $appName);
  45. }
  46. public function provideLazyInitialState(string $appName, string $key, Closure $closure): void {
  47. if (!isset($this->lazyStates[$appName])) {
  48. $this->lazyStates[$appName] = [];
  49. }
  50. $this->lazyStates[$appName][$key] = $closure;
  51. }
  52. /**
  53. * Invoke all callbacks to populate the `states` property
  54. */
  55. private function invokeLazyStateCallbacks(): void {
  56. foreach ($this->lazyStates as $app => $lazyStates) {
  57. foreach ($lazyStates as $key => $lazyState) {
  58. $startTime = microtime(true);
  59. $this->provideInitialState($app, $key, $lazyState());
  60. $endTime = microtime(true);
  61. $duration = $endTime - $startTime;
  62. if ($duration > 1) {
  63. $this->logger->warning('Lazy initial state provider for {key} took {duration} seconds.', [
  64. 'app' => $app,
  65. 'key' => $key,
  66. 'duration' => round($duration, 2),
  67. ]);
  68. }
  69. }
  70. }
  71. $this->lazyStates = [];
  72. }
  73. /**
  74. * Load the lazy states via the IBootstrap mechanism
  75. */
  76. private function loadLazyStates(): void {
  77. $context = $this->bootstrapCoordinator->getRegistrationContext();
  78. if ($context === null) {
  79. // To early, nothing to do yet
  80. return;
  81. }
  82. $initialStates = $context->getInitialStates();
  83. foreach ($initialStates as $initialState) {
  84. try {
  85. $provider = $this->container->query($initialState->getService());
  86. } catch (QueryException $e) {
  87. // Log an continue. We can be fault tolerant here.
  88. $this->logger->error('Could not load initial state provider dynamically: ' . $e->getMessage(), [
  89. 'exception' => $e,
  90. 'app' => $initialState->getAppId(),
  91. ]);
  92. continue;
  93. }
  94. if (!($provider instanceof InitialStateProvider)) {
  95. // Log an continue. We can be fault tolerant here.
  96. $this->logger->error('Initial state provider is not an InitialStateProvider instance: ' . $initialState->getService(), [
  97. 'app' => $initialState->getAppId(),
  98. ]);
  99. }
  100. $this->provideInitialState($initialState->getAppId(), $provider->getKey(), $provider);
  101. }
  102. }
  103. public function getInitialStates(): array {
  104. $this->invokeLazyStateCallbacks();
  105. $this->loadLazyStates();
  106. $appStates = [];
  107. foreach ($this->states as $app => $states) {
  108. foreach ($states as $key => $value) {
  109. $appStates["$app-$key"] = $value;
  110. }
  111. }
  112. return $appStates;
  113. }
  114. }