InitialStateService.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. $this->states[$appName][$key] = json_encode($data);
  58. return;
  59. }
  60. $this->logger->warning('Invalid '. $key . ' data provided to provideInitialState by ' . $appName);
  61. }
  62. public function provideLazyInitialState(string $appName, string $key, Closure $closure): void {
  63. if (!isset($this->lazyStates[$appName])) {
  64. $this->lazyStates[$appName] = [];
  65. }
  66. $this->lazyStates[$appName][$key] = $closure;
  67. }
  68. /**
  69. * Invoke all callbacks to populate the `states` property
  70. */
  71. private function invokeLazyStateCallbacks(): void {
  72. foreach ($this->lazyStates as $app => $lazyStates) {
  73. foreach ($lazyStates as $key => $lazyState) {
  74. $startTime = microtime(true);
  75. $this->provideInitialState($app, $key, $lazyState());
  76. $endTime = microtime(true);
  77. $duration = $endTime - $startTime;
  78. if ($duration > 1) {
  79. $this->logger->warning('Lazy initial state provider for {key} took {duration} seconds.', [
  80. 'app' => $app,
  81. 'key' => $key,
  82. 'duration' => round($duration, 2),
  83. ]);
  84. }
  85. }
  86. }
  87. $this->lazyStates = [];
  88. }
  89. /**
  90. * Load the lazy states via the IBootstrap mechanism
  91. */
  92. private function loadLazyStates(): void {
  93. $context = $this->bootstrapCoordinator->getRegistrationContext();
  94. if ($context === null) {
  95. // To early, nothing to do yet
  96. return;
  97. }
  98. $initialStates = $context->getInitialStates();
  99. foreach ($initialStates as $initialState) {
  100. try {
  101. $provider = $this->container->query($initialState->getService());
  102. } catch (QueryException $e) {
  103. // Log an continue. We can be fault tolerant here.
  104. $this->logger->error('Could not load initial state provider dynamically: ' . $e->getMessage(), [
  105. 'exception' => $e,
  106. 'app' => $initialState->getAppId(),
  107. ]);
  108. continue;
  109. }
  110. if (!($provider instanceof InitialStateProvider)) {
  111. // Log an continue. We can be fault tolerant here.
  112. $this->logger->error('Initial state provider is not an InitialStateProvider instance: ' . $initialState->getService(), [
  113. 'app' => $initialState->getAppId(),
  114. ]);
  115. }
  116. $this->provideInitialState($initialState->getAppId(), $provider->getKey(), $provider);
  117. }
  118. }
  119. public function getInitialStates(): array {
  120. $this->invokeLazyStateCallbacks();
  121. $this->loadLazyStates();
  122. $appStates = [];
  123. foreach ($this->states as $app => $states) {
  124. foreach ($states as $key => $value) {
  125. $appStates["$app-$key"] = $value;
  126. }
  127. }
  128. return $appStates;
  129. }
  130. }