You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

InitialStateService.php 4.4KB

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 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 OC\AppFramework\Bootstrap\Coordinator;
  28. use OCP\AppFramework\QueryException;
  29. use OCP\AppFramework\Services\InitialStateProvider;
  30. use OCP\IInitialStateService;
  31. use OCP\ILogger;
  32. use OCP\IServerContainer;
  33. class InitialStateService implements IInitialStateService {
  34. /** @var ILogger */
  35. private $logger;
  36. /** @var string[][] */
  37. private $states = [];
  38. /** @var Closure[][] */
  39. private $lazyStates = [];
  40. /** @var Coordinator */
  41. private $bootstrapCoordinator;
  42. /** @var IServerContainer */
  43. private $container;
  44. public function __construct(ILogger $logger, Coordinator $bootstrapCoordinator, IServerContainer $container) {
  45. $this->logger = $logger;
  46. $this->bootstrapCoordinator = $bootstrapCoordinator;
  47. $this->container = $container;
  48. }
  49. public function provideInitialState(string $appName, string $key, $data): void {
  50. // Scalars and JsonSerializable are fine
  51. if (is_scalar($data) || $data instanceof \JsonSerializable || is_array($data)) {
  52. if (!isset($this->states[$appName])) {
  53. $this->states[$appName] = [];
  54. }
  55. $this->states[$appName][$key] = json_encode($data);
  56. return;
  57. }
  58. $this->logger->warning('Invalid data provided to provideInitialState by ' . $appName);
  59. }
  60. public function provideLazyInitialState(string $appName, string $key, Closure $closure): void {
  61. if (!isset($this->lazyStates[$appName])) {
  62. $this->lazyStates[$appName] = [];
  63. }
  64. $this->lazyStates[$appName][$key] = $closure;
  65. }
  66. /**
  67. * Invoke all callbacks to populate the `states` property
  68. */
  69. private function invokeLazyStateCallbacks(): void {
  70. foreach ($this->lazyStates as $app => $lazyStates) {
  71. foreach ($lazyStates as $key => $lazyState) {
  72. $startTime = microtime(true);
  73. $this->provideInitialState($app, $key, $lazyState());
  74. $endTime = microtime(true);
  75. $duration = $endTime - $startTime;
  76. if ($duration > 1) {
  77. $this->logger->warning('Lazy initial state provider for {key} took {duration} seconds.', [
  78. 'app' => $app,
  79. 'key' => $key,
  80. 'duration' => round($duration, 2),
  81. ]);
  82. }
  83. }
  84. }
  85. $this->lazyStates = [];
  86. }
  87. /**
  88. * Load the lazy states via the IBootstrap mechanism
  89. */
  90. private function loadLazyStates(): void {
  91. $context = $this->bootstrapCoordinator->getRegistrationContext();
  92. if ($context === null) {
  93. // To early, nothing to do yet
  94. return;
  95. }
  96. $initialStates = $context->getInitialStates();
  97. foreach ($initialStates as $initialState) {
  98. try {
  99. $provider = $this->container->query($initialState['class']);
  100. } catch (QueryException $e) {
  101. // Log an continue. We can be fault tolerant here.
  102. $this->logger->logException($e, [
  103. 'message' => 'Could not load initial state provider dynamically: ' . $e->getMessage(),
  104. 'level' => ILogger::ERROR,
  105. 'app' => $initialState['appId'],
  106. ]);
  107. continue;
  108. }
  109. if (!($provider instanceof InitialStateProvider)) {
  110. // Log an continue. We can be fault tolerant here.
  111. $this->logger->error('Initial state provider is not an InitialStateProvider instance: ' . $initialState['class'], [
  112. 'app' => $initialState['appId'],
  113. ]);
  114. }
  115. $this->provideInitialState($initialState['appId'], $provider->getKey(), $provider);
  116. }
  117. }
  118. public function getInitialStates(): array {
  119. $this->invokeLazyStateCallbacks();
  120. $this->loadLazyStates();
  121. $appStates = [];
  122. foreach ($this->states as $app => $states) {
  123. foreach ($states as $key => $value) {
  124. $appStates["$app-$key"] = $value;
  125. }
  126. }
  127. return $appStates;
  128. }
  129. }