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.

152 lines
4.4 KiB

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