Coordinator.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\AppFramework\Bootstrap;
  8. use OC\Support\CrashReport\Registry;
  9. use OC_App;
  10. use OCP\App\AppPathNotFoundException;
  11. use OCP\App\IAppManager;
  12. use OCP\AppFramework\App;
  13. use OCP\AppFramework\Bootstrap\IBootstrap;
  14. use OCP\AppFramework\QueryException;
  15. use OCP\Dashboard\IManager;
  16. use OCP\Diagnostics\IEventLogger;
  17. use OCP\EventDispatcher\IEventDispatcher;
  18. use OCP\IServerContainer;
  19. use Psr\Log\LoggerInterface;
  20. use Throwable;
  21. use function class_exists;
  22. use function class_implements;
  23. use function in_array;
  24. class Coordinator {
  25. /** @var RegistrationContext|null */
  26. private $registrationContext;
  27. /** @var string[] */
  28. private $bootedApps = [];
  29. public function __construct(
  30. private IServerContainer $serverContainer,
  31. private Registry $registry,
  32. private IManager $dashboardManager,
  33. private IEventDispatcher $eventDispatcher,
  34. private IEventLogger $eventLogger,
  35. private IAppManager $appManager,
  36. private LoggerInterface $logger,
  37. ) {
  38. }
  39. public function runInitialRegistration(): void {
  40. $this->registerApps(OC_App::getEnabledApps());
  41. }
  42. public function runLazyRegistration(string $appId): void {
  43. $this->registerApps([$appId]);
  44. }
  45. /**
  46. * @param string[] $appIds
  47. */
  48. private function registerApps(array $appIds): void {
  49. $this->eventLogger->start('bootstrap:register_apps', '');
  50. if ($this->registrationContext === null) {
  51. $this->registrationContext = new RegistrationContext($this->logger);
  52. }
  53. $apps = [];
  54. foreach ($appIds as $appId) {
  55. $this->eventLogger->start("bootstrap:register_app:$appId", "Register $appId");
  56. $this->eventLogger->start("bootstrap:register_app:$appId:autoloader", "Setup autoloader for $appId");
  57. /*
  58. * First, we have to enable the app's autoloader
  59. */
  60. try {
  61. $path = $this->appManager->getAppPath($appId);
  62. } catch (AppPathNotFoundException) {
  63. // Ignore
  64. continue;
  65. }
  66. OC_App::registerAutoloading($appId, $path);
  67. $this->eventLogger->end("bootstrap:register_app:$appId:autoloader");
  68. /*
  69. * Next we check if there is an application class, and it implements
  70. * the \OCP\AppFramework\Bootstrap\IBootstrap interface
  71. */
  72. $appNameSpace = App::buildAppNamespace($appId);
  73. $applicationClassName = $appNameSpace . '\\AppInfo\\Application';
  74. try {
  75. if (class_exists($applicationClassName) && in_array(IBootstrap::class, class_implements($applicationClassName), true)) {
  76. $this->eventLogger->start("bootstrap:register_app:$appId:application", "Load `Application` instance for $appId");
  77. try {
  78. /** @var IBootstrap|App $application */
  79. $apps[$appId] = $application = $this->serverContainer->query($applicationClassName);
  80. } catch (QueryException $e) {
  81. // Weird, but ok
  82. $this->eventLogger->end("bootstrap:register_app:$appId");
  83. continue;
  84. }
  85. $this->eventLogger->end("bootstrap:register_app:$appId:application");
  86. $this->eventLogger->start("bootstrap:register_app:$appId:register", "`Application::register` for $appId");
  87. $application->register($this->registrationContext->for($appId));
  88. $this->eventLogger->end("bootstrap:register_app:$appId:register");
  89. }
  90. } catch (Throwable $e) {
  91. $this->logger->emergency('Error during app service registration: ' . $e->getMessage(), [
  92. 'exception' => $e,
  93. 'app' => $appId,
  94. ]);
  95. $this->eventLogger->end("bootstrap:register_app:$appId");
  96. continue;
  97. }
  98. $this->eventLogger->end("bootstrap:register_app:$appId");
  99. }
  100. $this->eventLogger->start('bootstrap:register_apps:apply', 'Apply all the registered service by apps');
  101. /**
  102. * Now that all register methods have been called, we can delegate the registrations
  103. * to the actual services
  104. */
  105. $this->registrationContext->delegateCapabilityRegistrations($apps);
  106. $this->registrationContext->delegateCrashReporterRegistrations($apps, $this->registry);
  107. $this->registrationContext->delegateDashboardPanelRegistrations($this->dashboardManager);
  108. $this->registrationContext->delegateEventListenerRegistrations($this->eventDispatcher);
  109. $this->registrationContext->delegateContainerRegistrations($apps);
  110. $this->eventLogger->end('bootstrap:register_apps:apply');
  111. $this->eventLogger->end('bootstrap:register_apps');
  112. }
  113. public function getRegistrationContext(): ?RegistrationContext {
  114. return $this->registrationContext;
  115. }
  116. public function bootApp(string $appId): void {
  117. if (isset($this->bootedApps[$appId])) {
  118. return;
  119. }
  120. $this->bootedApps[$appId] = true;
  121. $appNameSpace = App::buildAppNamespace($appId);
  122. $applicationClassName = $appNameSpace . '\\AppInfo\\Application';
  123. if (!class_exists($applicationClassName)) {
  124. // Nothing to boot
  125. return;
  126. }
  127. /*
  128. * Now it is time to fetch an instance of the App class. For classes
  129. * that implement \OCP\AppFramework\Bootstrap\IBootstrap this means
  130. * the instance was already created for register, but any other
  131. * (legacy) code will now do their magic via the constructor.
  132. */
  133. $this->eventLogger->start('bootstrap:boot_app:' . $appId, "Call `Application::boot` for $appId");
  134. try {
  135. /** @var App $application */
  136. $application = $this->serverContainer->query($applicationClassName);
  137. if ($application instanceof IBootstrap) {
  138. /** @var BootContext $context */
  139. $context = new BootContext($application->getContainer());
  140. $application->boot($context);
  141. }
  142. } catch (QueryException $e) {
  143. $this->logger->error("Could not boot $appId: " . $e->getMessage(), [
  144. 'exception' => $e,
  145. ]);
  146. } catch (Throwable $e) {
  147. $this->logger->emergency("Could not boot $appId: " . $e->getMessage(), [
  148. 'exception' => $e,
  149. ]);
  150. }
  151. $this->eventLogger->end('bootstrap:boot_app:' . $appId);
  152. }
  153. public function isBootable(string $appId) {
  154. $appNameSpace = App::buildAppNamespace($appId);
  155. $applicationClassName = $appNameSpace . '\\AppInfo\\Application';
  156. return class_exists($applicationClassName) &&
  157. in_array(IBootstrap::class, class_implements($applicationClassName), true);
  158. }
  159. }