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.

102 lines
2.8 KiB

3 years ago
  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 OCP\IInitialStateService;
  28. use OCP\ILogger;
  29. class InitialStateService implements IInitialStateService {
  30. /** @var ILogger */
  31. private $logger;
  32. /** @var string[][] */
  33. private $states = [];
  34. /** @var Closure[][] */
  35. private $lazyStates = [];
  36. public function __construct(ILogger $logger) {
  37. $this->logger = $logger;
  38. }
  39. public function provideInitialState(string $appName, string $key, $data): void {
  40. // Scalars and JsonSerializable are fine
  41. if (is_scalar($data) || $data instanceof \JsonSerializable || is_array($data)) {
  42. if (!isset($this->states[$appName])) {
  43. $this->states[$appName] = [];
  44. }
  45. $this->states[$appName][$key] = json_encode($data);
  46. return;
  47. }
  48. $this->logger->warning('Invalid data provided to provideInitialState by ' . $appName);
  49. }
  50. public function provideLazyInitialState(string $appName, string $key, Closure $closure): void {
  51. if (!isset($this->lazyStates[$appName])) {
  52. $this->lazyStates[$appName] = [];
  53. }
  54. $this->lazyStates[$appName][$key] = $closure;
  55. }
  56. /**
  57. * Invoke all callbacks to populate the `states` property
  58. */
  59. private function invokeLazyStateCallbacks(): void {
  60. foreach ($this->lazyStates as $app => $lazyStates) {
  61. foreach ($lazyStates as $key => $lazyState) {
  62. $startTime = microtime(true);
  63. $this->provideInitialState($app, $key, $lazyState());
  64. $endTime = microtime(true);
  65. $duration = $endTime - $startTime;
  66. if ($duration > 1) {
  67. $this->logger->warning('Lazy initial state provider for {key} took {duration} seconds.', [
  68. 'app' => $app,
  69. 'key' => $key,
  70. 'duration' => round($duration, 2),
  71. ]);
  72. }
  73. }
  74. }
  75. $this->lazyStates = [];
  76. }
  77. public function getInitialStates(): array {
  78. $this->invokeLazyStateCallbacks();
  79. $appStates = [];
  80. foreach ($this->states as $app => $states) {
  81. foreach ($states as $key => $value) {
  82. $appStates["$app-$key"] = $value;
  83. }
  84. }
  85. return $appStates;
  86. }
  87. }