Manager.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020 Julius Härtl <jus@bitgrid.net>
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Julius Härtl <jus@bitgrid.net>
  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\Dashboard;
  26. use InvalidArgumentException;
  27. use OCP\App\IAppManager;
  28. use OCP\Dashboard\IConditionalWidget;
  29. use OCP\Dashboard\IManager;
  30. use OCP\Dashboard\IWidget;
  31. use Psr\Container\ContainerExceptionInterface;
  32. use Psr\Container\ContainerInterface;
  33. use Psr\Log\LoggerInterface;
  34. use Throwable;
  35. class Manager implements IManager {
  36. /** @var array */
  37. private $lazyWidgets = [];
  38. /** @var array<string, IWidget> */
  39. private array $widgets = [];
  40. private ContainerInterface $serverContainer;
  41. private ?IAppManager $appManager = null;
  42. public function __construct(ContainerInterface $serverContainer) {
  43. $this->serverContainer = $serverContainer;
  44. }
  45. private function registerWidget(IWidget $widget): void {
  46. if (array_key_exists($widget->getId(), $this->widgets)) {
  47. throw new InvalidArgumentException('Dashboard widget with this id has already been registered');
  48. }
  49. $this->widgets[$widget->getId()] = $widget;
  50. }
  51. public function lazyRegisterWidget(string $widgetClass, string $appId): void {
  52. $this->lazyWidgets[] = ['class' => $widgetClass, 'appId' => $appId];
  53. }
  54. public function loadLazyPanels(): void {
  55. if ($this->appManager === null) {
  56. $this->appManager = $this->serverContainer->get(IAppManager::class);
  57. }
  58. $services = $this->lazyWidgets;
  59. foreach ($services as $service) {
  60. /** @psalm-suppress InvalidCatch */
  61. try {
  62. if (!$this->appManager->isEnabledForUser($service['appId'])) {
  63. // all apps are registered, but some may not be enabled for the user
  64. continue;
  65. }
  66. /** @var IWidget $widget */
  67. $widget = $this->serverContainer->get($service['class']);
  68. } catch (ContainerExceptionInterface $e) {
  69. /*
  70. * There is a circular dependency between the logger and the registry, so
  71. * we can not inject it. Thus the static call.
  72. */
  73. \OC::$server->get(LoggerInterface::class)->critical(
  74. 'Could not load lazy dashboard widget: ' . $service['class'],
  75. ['exception' => $e]
  76. );
  77. continue;
  78. }
  79. /**
  80. * Try to register the loaded reporter. Theoretically it could be of a wrong
  81. * type, so we might get a TypeError here that we should catch.
  82. */
  83. try {
  84. if ($widget instanceof IConditionalWidget && !$widget->isEnabled()) {
  85. continue;
  86. }
  87. $this->registerWidget($widget);
  88. } catch (Throwable $e) {
  89. /*
  90. * There is a circular dependency between the logger and the registry, so
  91. * we can not inject it. Thus the static call.
  92. */
  93. \OC::$server->get(LoggerInterface::class)->critical(
  94. 'Could not register lazy dashboard widget: ' . $service['class'],
  95. ['exception' => $e]
  96. );
  97. continue;
  98. }
  99. try {
  100. $startTime = microtime(true);
  101. $widget->load();
  102. $endTime = microtime(true);
  103. $duration = $endTime - $startTime;
  104. if ($duration > 1) {
  105. \OC::$server->get(LoggerInterface::class)->info(
  106. 'Dashboard widget {widget} took {duration} seconds to load.',
  107. [
  108. 'widget' => $widget->getId(),
  109. 'duration' => round($duration, 2),
  110. ]
  111. );
  112. }
  113. } catch (Throwable $e) {
  114. \OC::$server->get(LoggerInterface::class)->critical(
  115. 'Error during dashboard widget loading: ' . $service['class'],
  116. ['exception' => $e]
  117. );
  118. continue;
  119. }
  120. }
  121. $this->lazyWidgets = [];
  122. }
  123. /**
  124. * @return array<string, IWidget>
  125. */
  126. public function getWidgets(): array {
  127. $this->loadLazyPanels();
  128. return $this->widgets;
  129. }
  130. }