1
0

InitialStateService.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 Joas Schilling <coding@schilljs.com>
  8. * @author John Molakvoæ <skjnldsv@protonmail.com>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license GNU AGPL version 3 or any later version
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. namespace OC;
  28. use Closure;
  29. use OC\AppFramework\Bootstrap\Coordinator;
  30. use OCP\AppFramework\QueryException;
  31. use OCP\AppFramework\Services\InitialStateProvider;
  32. use OCP\IInitialStateService;
  33. use OCP\IServerContainer;
  34. use Psr\Log\LoggerInterface;
  35. class InitialStateService implements IInitialStateService {
  36. /** @var LoggerInterface */
  37. private $logger;
  38. /** @var string[][] */
  39. private $states = [];
  40. /** @var Closure[][] */
  41. private $lazyStates = [];
  42. /** @var Coordinator */
  43. private $bootstrapCoordinator;
  44. /** @var IServerContainer */
  45. private $container;
  46. public function __construct(LoggerInterface $logger, Coordinator $bootstrapCoordinator, IServerContainer $container) {
  47. $this->logger = $logger;
  48. $this->bootstrapCoordinator = $bootstrapCoordinator;
  49. $this->container = $container;
  50. }
  51. public function provideInitialState(string $appName, string $key, $data): void {
  52. // Scalars and JsonSerializable are fine
  53. if (is_scalar($data) || $data instanceof \JsonSerializable || is_array($data)) {
  54. if (!isset($this->states[$appName])) {
  55. $this->states[$appName] = [];
  56. }
  57. try {
  58. $this->states[$appName][$key] = json_encode($data, JSON_THROW_ON_ERROR);
  59. } catch (\JsonException $e) {
  60. $this->logger->error('Invalid '. $key . ' data provided to provideInitialState by ' . $appName, ['exception' => $e]);
  61. }
  62. return;
  63. }
  64. $this->logger->warning('Invalid '. $key . ' data provided to provideInitialState by ' . $appName);
  65. }
  66. public function provideLazyInitialState(string $appName, string $key, Closure $closure): void {
  67. if (!isset($this->lazyStates[$appName])) {
  68. $this->lazyStates[$appName] = [];
  69. }
  70. $this->lazyStates[$appName][$key] = $closure;
  71. }
  72. /**
  73. * Invoke all callbacks to populate the `states` property
  74. */
  75. private function invokeLazyStateCallbacks(): void {
  76. foreach ($this->lazyStates as $app => $lazyStates) {
  77. foreach ($lazyStates as $key => $lazyState) {
  78. $startTime = microtime(true);
  79. $this->provideInitialState($app, $key, $lazyState());
  80. $endTime = microtime(true);
  81. $duration = $endTime - $startTime;
  82. if ($duration > 1) {
  83. $this->logger->warning('Lazy initial state provider for {key} took {duration} seconds.', [
  84. 'app' => $app,
  85. 'key' => $key,
  86. 'duration' => round($duration, 2),
  87. ]);
  88. }
  89. }
  90. }
  91. $this->lazyStates = [];
  92. }
  93. /**
  94. * Load the lazy states via the IBootstrap mechanism
  95. */
  96. private function loadLazyStates(): void {
  97. $context = $this->bootstrapCoordinator->getRegistrationContext();
  98. if ($context === null) {
  99. // To early, nothing to do yet
  100. return;
  101. }
  102. $initialStates = $context->getInitialStates();
  103. foreach ($initialStates as $initialState) {
  104. try {
  105. $provider = $this->container->query($initialState->getService());
  106. } catch (QueryException $e) {
  107. // Log an continue. We can be fault tolerant here.
  108. $this->logger->error('Could not load initial state provider dynamically: ' . $e->getMessage(), [
  109. 'exception' => $e,
  110. 'app' => $initialState->getAppId(),
  111. ]);
  112. continue;
  113. }
  114. if (!($provider instanceof InitialStateProvider)) {
  115. // Log an continue. We can be fault tolerant here.
  116. $this->logger->error('Initial state provider is not an InitialStateProvider instance: ' . $initialState->getService(), [
  117. 'app' => $initialState->getAppId(),
  118. ]);
  119. }
  120. $this->provideInitialState($initialState->getAppId(), $provider->getKey(), $provider);
  121. }
  122. }
  123. public function getInitialStates(): array {
  124. $this->invokeLazyStateCallbacks();
  125. $this->loadLazyStates();
  126. $appStates = [];
  127. foreach ($this->states as $app => $states) {
  128. foreach ($states as $key => $value) {
  129. $appStates["$app-$key"] = $value;
  130. }
  131. }
  132. return $appStates;
  133. }
  134. }