@@ -32,37 +32,37 @@ |
||
| 32 | 32 | use OCP\IUserSession; |
| 33 | 33 | |
| 34 | 34 | class JSDataService extends InitialStateProvider { |
| 35 | - /** @var IUserSession */ |
|
| 36 | - private $userSession; |
|
| 37 | - /** @var IConfig */ |
|
| 38 | - private $config; |
|
| 35 | + /** @var IUserSession */ |
|
| 36 | + private $userSession; |
|
| 37 | + /** @var IConfig */ |
|
| 38 | + private $config; |
|
| 39 | 39 | |
| 40 | - public function __construct( |
|
| 41 | - IUserSession $userSession, |
|
| 42 | - IConfig $config |
|
| 43 | - ) { |
|
| 44 | - $this->userSession = $userSession; |
|
| 45 | - $this->config = $config; |
|
| 46 | - } |
|
| 40 | + public function __construct( |
|
| 41 | + IUserSession $userSession, |
|
| 42 | + IConfig $config |
|
| 43 | + ) { |
|
| 44 | + $this->userSession = $userSession; |
|
| 45 | + $this->config = $config; |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - public function getKey(): string { |
|
| 49 | - return 'data'; |
|
| 50 | - } |
|
| 48 | + public function getKey(): string { |
|
| 49 | + return 'data'; |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - public function getData() { |
|
| 53 | - $user = $this->userSession->getUser(); |
|
| 52 | + public function getData() { |
|
| 53 | + $user = $this->userSession->getUser(); |
|
| 54 | 54 | |
| 55 | - if ($user === null) { |
|
| 56 | - $theme = false; |
|
| 57 | - $highcontrast = false; |
|
| 58 | - } else { |
|
| 59 | - $theme = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'theme', false); |
|
| 60 | - $highcontrast = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'highcontrast', false) !== false; |
|
| 61 | - } |
|
| 55 | + if ($user === null) { |
|
| 56 | + $theme = false; |
|
| 57 | + $highcontrast = false; |
|
| 58 | + } else { |
|
| 59 | + $theme = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'theme', false); |
|
| 60 | + $highcontrast = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'highcontrast', false) !== false; |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - return [ |
|
| 64 | - 'theme' => $theme, |
|
| 65 | - 'highcontrast' => $highcontrast, |
|
| 66 | - ]; |
|
| 67 | - } |
|
| 63 | + return [ |
|
| 64 | + 'theme' => $theme, |
|
| 65 | + 'highcontrast' => $highcontrast, |
|
| 66 | + ]; |
|
| 67 | + } |
|
| 68 | 68 | } |
@@ -45,41 +45,41 @@ |
||
| 45 | 45 | |
| 46 | 46 | class Application extends App implements IBootstrap { |
| 47 | 47 | |
| 48 | - /** @var string */ |
|
| 49 | - public const APP_ID = 'accessibility'; |
|
| 48 | + /** @var string */ |
|
| 49 | + public const APP_ID = 'accessibility'; |
|
| 50 | 50 | |
| 51 | - public function __construct() { |
|
| 52 | - parent::__construct(self::APP_ID); |
|
| 53 | - } |
|
| 51 | + public function __construct() { |
|
| 52 | + parent::__construct(self::APP_ID); |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - public function register(IRegistrationContext $context): void { |
|
| 56 | - $context->registerInitialStateProvider(JSDataService::class); |
|
| 57 | - } |
|
| 55 | + public function register(IRegistrationContext $context): void { |
|
| 56 | + $context->registerInitialStateProvider(JSDataService::class); |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - public function boot(IBootContext $context): void { |
|
| 60 | - $context->injectFn([$this, 'injectCss']); |
|
| 61 | - } |
|
| 59 | + public function boot(IBootContext $context): void { |
|
| 60 | + $context->injectFn([$this, 'injectCss']); |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - public function injectCss(IUserSession $userSession, |
|
| 64 | - IConfig $config, |
|
| 65 | - IURLGenerator $urlGenerator) { |
|
| 66 | - // Inject the fake css on all pages if enabled and user is logged |
|
| 67 | - $loggedUser = $userSession->getUser(); |
|
| 68 | - if ($loggedUser !== null) { |
|
| 69 | - $userValues = $config->getUserKeys($loggedUser->getUID(), self::APP_ID); |
|
| 70 | - // we want to check if any theme or font is enabled. |
|
| 71 | - if (count($userValues) > 0) { |
|
| 72 | - $hash = $config->getUserValue($loggedUser->getUID(), self::APP_ID, 'icons-css', md5(implode('-', $userValues))); |
|
| 73 | - $linkToCSS = $urlGenerator->linkToRoute(self::APP_ID . '.accessibility.getCss', ['md5' => $hash]); |
|
| 74 | - \OCP\Util::addHeader('link', ['rel' => 'stylesheet', 'href' => $linkToCSS]); |
|
| 75 | - } |
|
| 76 | - \OCP\Util::addScript('accessibility', 'accessibilityoca'); |
|
| 77 | - } else { |
|
| 78 | - $userValues = ['dark']; |
|
| 63 | + public function injectCss(IUserSession $userSession, |
|
| 64 | + IConfig $config, |
|
| 65 | + IURLGenerator $urlGenerator) { |
|
| 66 | + // Inject the fake css on all pages if enabled and user is logged |
|
| 67 | + $loggedUser = $userSession->getUser(); |
|
| 68 | + if ($loggedUser !== null) { |
|
| 69 | + $userValues = $config->getUserKeys($loggedUser->getUID(), self::APP_ID); |
|
| 70 | + // we want to check if any theme or font is enabled. |
|
| 71 | + if (count($userValues) > 0) { |
|
| 72 | + $hash = $config->getUserValue($loggedUser->getUID(), self::APP_ID, 'icons-css', md5(implode('-', $userValues))); |
|
| 73 | + $linkToCSS = $urlGenerator->linkToRoute(self::APP_ID . '.accessibility.getCss', ['md5' => $hash]); |
|
| 74 | + \OCP\Util::addHeader('link', ['rel' => 'stylesheet', 'href' => $linkToCSS]); |
|
| 75 | + } |
|
| 76 | + \OCP\Util::addScript('accessibility', 'accessibilityoca'); |
|
| 77 | + } else { |
|
| 78 | + $userValues = ['dark']; |
|
| 79 | 79 | |
| 80 | - $hash = md5(implode('-', $userValues)); |
|
| 81 | - $linkToCSS = $urlGenerator->linkToRoute(self::APP_ID . '.accessibility.getCss', ['md5' => $hash]); |
|
| 82 | - \OCP\Util::addHeader('link', ['rel' => 'stylesheet', 'media' => '(prefers-color-scheme: dark)', 'href' => $linkToCSS]); |
|
| 83 | - } |
|
| 84 | - } |
|
| 80 | + $hash = md5(implode('-', $userValues)); |
|
| 81 | + $linkToCSS = $urlGenerator->linkToRoute(self::APP_ID . '.accessibility.getCss', ['md5' => $hash]); |
|
| 82 | + \OCP\Util::addHeader('link', ['rel' => 'stylesheet', 'media' => '(prefers-color-scheme: dark)', 'href' => $linkToCSS]); |
|
| 83 | + } |
|
| 84 | + } |
|
| 85 | 85 | } |
@@ -70,7 +70,7 @@ discard block |
||
| 70 | 70 | // we want to check if any theme or font is enabled. |
| 71 | 71 | if (count($userValues) > 0) { |
| 72 | 72 | $hash = $config->getUserValue($loggedUser->getUID(), self::APP_ID, 'icons-css', md5(implode('-', $userValues))); |
| 73 | - $linkToCSS = $urlGenerator->linkToRoute(self::APP_ID . '.accessibility.getCss', ['md5' => $hash]); |
|
| 73 | + $linkToCSS = $urlGenerator->linkToRoute(self::APP_ID.'.accessibility.getCss', ['md5' => $hash]); |
|
| 74 | 74 | \OCP\Util::addHeader('link', ['rel' => 'stylesheet', 'href' => $linkToCSS]); |
| 75 | 75 | } |
| 76 | 76 | \OCP\Util::addScript('accessibility', 'accessibilityoca'); |
@@ -78,7 +78,7 @@ discard block |
||
| 78 | 78 | $userValues = ['dark']; |
| 79 | 79 | |
| 80 | 80 | $hash = md5(implode('-', $userValues)); |
| 81 | - $linkToCSS = $urlGenerator->linkToRoute(self::APP_ID . '.accessibility.getCss', ['md5' => $hash]); |
|
| 81 | + $linkToCSS = $urlGenerator->linkToRoute(self::APP_ID.'.accessibility.getCss', ['md5' => $hash]); |
|
| 82 | 82 | \OCP\Util::addHeader('link', ['rel' => 'stylesheet', 'media' => '(prefers-color-scheme: dark)', 'href' => $linkToCSS]); |
| 83 | 83 | } |
| 84 | 84 | } |
@@ -39,402 +39,402 @@ |
||
| 39 | 39 | |
| 40 | 40 | class RegistrationContext { |
| 41 | 41 | |
| 42 | - /** @var array[] */ |
|
| 43 | - private $capabilities = []; |
|
| 44 | - |
|
| 45 | - /** @var array[] */ |
|
| 46 | - private $crashReporters = []; |
|
| 47 | - |
|
| 48 | - /** @var array[] */ |
|
| 49 | - private $dashboardPanels = []; |
|
| 50 | - |
|
| 51 | - /** @var array[] */ |
|
| 52 | - private $services = []; |
|
| 53 | - |
|
| 54 | - /** @var array[] */ |
|
| 55 | - private $aliases = []; |
|
| 56 | - |
|
| 57 | - /** @var array[] */ |
|
| 58 | - private $parameters = []; |
|
| 59 | - |
|
| 60 | - /** @var array[] */ |
|
| 61 | - private $eventListeners = []; |
|
| 62 | - |
|
| 63 | - /** @var array[] */ |
|
| 64 | - private $middlewares = []; |
|
| 65 | - |
|
| 66 | - /** @var array[] */ |
|
| 67 | - private $searchProviders = []; |
|
| 68 | - |
|
| 69 | - /** @var array[] */ |
|
| 70 | - private $alternativeLogins = []; |
|
| 71 | - |
|
| 72 | - /** @var array[] */ |
|
| 73 | - private $initialStates = []; |
|
| 74 | - |
|
| 75 | - /** @var ILogger */ |
|
| 76 | - private $logger; |
|
| 77 | - |
|
| 78 | - public function __construct(ILogger $logger) { |
|
| 79 | - $this->logger = $logger; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - public function for(string $appId): IRegistrationContext { |
|
| 83 | - return new class($appId, $this) implements IRegistrationContext { |
|
| 84 | - /** @var string */ |
|
| 85 | - private $appId; |
|
| 86 | - |
|
| 87 | - /** @var RegistrationContext */ |
|
| 88 | - private $context; |
|
| 89 | - |
|
| 90 | - public function __construct(string $appId, RegistrationContext $context) { |
|
| 91 | - $this->appId = $appId; |
|
| 92 | - $this->context = $context; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - public function registerCapability(string $capability): void { |
|
| 96 | - $this->context->registerCapability( |
|
| 97 | - $this->appId, |
|
| 98 | - $capability |
|
| 99 | - ); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - public function registerCrashReporter(string $reporterClass): void { |
|
| 103 | - $this->context->registerCrashReporter( |
|
| 104 | - $this->appId, |
|
| 105 | - $reporterClass |
|
| 106 | - ); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - public function registerDashboardWidget(string $widgetClass): void { |
|
| 110 | - $this->context->registerDashboardPanel( |
|
| 111 | - $this->appId, |
|
| 112 | - $widgetClass |
|
| 113 | - ); |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - public function registerService(string $name, callable $factory, bool $shared = true): void { |
|
| 117 | - $this->context->registerService( |
|
| 118 | - $this->appId, |
|
| 119 | - $name, |
|
| 120 | - $factory, |
|
| 121 | - $shared |
|
| 122 | - ); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - public function registerServiceAlias(string $alias, string $target): void { |
|
| 126 | - $this->context->registerServiceAlias( |
|
| 127 | - $this->appId, |
|
| 128 | - $alias, |
|
| 129 | - $target |
|
| 130 | - ); |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - public function registerParameter(string $name, $value): void { |
|
| 134 | - $this->context->registerParameter( |
|
| 135 | - $this->appId, |
|
| 136 | - $name, |
|
| 137 | - $value |
|
| 138 | - ); |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - public function registerEventListener(string $event, string $listener, int $priority = 0): void { |
|
| 142 | - $this->context->registerEventListener( |
|
| 143 | - $this->appId, |
|
| 144 | - $event, |
|
| 145 | - $listener, |
|
| 146 | - $priority |
|
| 147 | - ); |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - public function registerMiddleware(string $class): void { |
|
| 151 | - $this->context->registerMiddleware( |
|
| 152 | - $this->appId, |
|
| 153 | - $class |
|
| 154 | - ); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - public function registerSearchProvider(string $class): void { |
|
| 158 | - $this->context->registerSearchProvider( |
|
| 159 | - $this->appId, |
|
| 160 | - $class |
|
| 161 | - ); |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - public function registerAlternativeLogin(string $class): void { |
|
| 165 | - $this->context->registerAlternativeLogin( |
|
| 166 | - $this->appId, |
|
| 167 | - $class |
|
| 168 | - ); |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - public function registerInitialStateProvider(string $class): void { |
|
| 172 | - $this->context->registerInitialState( |
|
| 173 | - $this->appId, |
|
| 174 | - $class |
|
| 175 | - ); |
|
| 176 | - } |
|
| 177 | - }; |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - public function registerCapability(string $appId, string $capability): void { |
|
| 181 | - $this->capabilities[] = [ |
|
| 182 | - 'appId' => $appId, |
|
| 183 | - 'capability' => $capability |
|
| 184 | - ]; |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - public function registerCrashReporter(string $appId, string $reporterClass): void { |
|
| 188 | - $this->crashReporters[] = [ |
|
| 189 | - 'appId' => $appId, |
|
| 190 | - 'class' => $reporterClass, |
|
| 191 | - ]; |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - public function registerDashboardPanel(string $appId, string $panelClass): void { |
|
| 195 | - $this->dashboardPanels[] = [ |
|
| 196 | - 'appId' => $appId, |
|
| 197 | - 'class' => $panelClass |
|
| 198 | - ]; |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - public function registerService(string $appId, string $name, callable $factory, bool $shared = true): void { |
|
| 202 | - $this->services[] = [ |
|
| 203 | - "appId" => $appId, |
|
| 204 | - "name" => $name, |
|
| 205 | - "factory" => $factory, |
|
| 206 | - "sharred" => $shared, |
|
| 207 | - ]; |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - public function registerServiceAlias(string $appId, string $alias, string $target): void { |
|
| 211 | - $this->aliases[] = [ |
|
| 212 | - "appId" => $appId, |
|
| 213 | - "alias" => $alias, |
|
| 214 | - "target" => $target, |
|
| 215 | - ]; |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - public function registerParameter(string $appId, string $name, $value): void { |
|
| 219 | - $this->parameters[] = [ |
|
| 220 | - "appId" => $appId, |
|
| 221 | - "name" => $name, |
|
| 222 | - "value" => $value, |
|
| 223 | - ]; |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - public function registerEventListener(string $appId, string $event, string $listener, int $priority = 0): void { |
|
| 227 | - $this->eventListeners[] = [ |
|
| 228 | - "appId" => $appId, |
|
| 229 | - "event" => $event, |
|
| 230 | - "listener" => $listener, |
|
| 231 | - "priority" => $priority, |
|
| 232 | - ]; |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - public function registerMiddleware(string $appId, string $class): void { |
|
| 236 | - $this->middlewares[] = [ |
|
| 237 | - "appId" => $appId, |
|
| 238 | - "class" => $class, |
|
| 239 | - ]; |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - public function registerSearchProvider(string $appId, string $class) { |
|
| 243 | - $this->searchProviders[] = [ |
|
| 244 | - 'appId' => $appId, |
|
| 245 | - 'class' => $class, |
|
| 246 | - ]; |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - public function registerAlternativeLogin(string $appId, string $class): void { |
|
| 250 | - $this->alternativeLogins[] = [ |
|
| 251 | - 'appId' => $appId, |
|
| 252 | - 'class' => $class, |
|
| 253 | - ]; |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - public function registerInitialState(string $appId, string $class): void { |
|
| 257 | - $this->initialStates[] = [ |
|
| 258 | - 'appId' => $appId, |
|
| 259 | - 'class' => $class, |
|
| 260 | - ]; |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - /** |
|
| 264 | - * @param App[] $apps |
|
| 265 | - */ |
|
| 266 | - public function delegateCapabilityRegistrations(array $apps): void { |
|
| 267 | - foreach ($this->capabilities as $registration) { |
|
| 268 | - try { |
|
| 269 | - $apps[$registration['appId']] |
|
| 270 | - ->getContainer() |
|
| 271 | - ->registerCapability($registration['capability']); |
|
| 272 | - } catch (Throwable $e) { |
|
| 273 | - $appId = $registration['appId']; |
|
| 274 | - $this->logger->logException($e, [ |
|
| 275 | - 'message' => "Error during capability registration of $appId: " . $e->getMessage(), |
|
| 276 | - 'level' => ILogger::ERROR, |
|
| 277 | - ]); |
|
| 278 | - } |
|
| 279 | - } |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - /** |
|
| 283 | - * @param App[] $apps |
|
| 284 | - */ |
|
| 285 | - public function delegateCrashReporterRegistrations(array $apps, Registry $registry): void { |
|
| 286 | - foreach ($this->crashReporters as $registration) { |
|
| 287 | - try { |
|
| 288 | - $registry->registerLazy($registration['class']); |
|
| 289 | - } catch (Throwable $e) { |
|
| 290 | - $appId = $registration['appId']; |
|
| 291 | - $this->logger->logException($e, [ |
|
| 292 | - 'message' => "Error during crash reporter registration of $appId: " . $e->getMessage(), |
|
| 293 | - 'level' => ILogger::ERROR, |
|
| 294 | - ]); |
|
| 295 | - } |
|
| 296 | - } |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - /** |
|
| 300 | - * @param App[] $apps |
|
| 301 | - */ |
|
| 302 | - public function delegateDashboardPanelRegistrations(array $apps, IManager $dashboardManager): void { |
|
| 303 | - foreach ($this->dashboardPanels as $panel) { |
|
| 304 | - try { |
|
| 305 | - $dashboardManager->lazyRegisterWidget($panel['class']); |
|
| 306 | - } catch (Throwable $e) { |
|
| 307 | - $appId = $panel['appId']; |
|
| 308 | - $this->logger->logException($e, [ |
|
| 309 | - 'message' => "Error during dashboard registration of $appId: " . $e->getMessage(), |
|
| 310 | - 'level' => ILogger::ERROR, |
|
| 311 | - ]); |
|
| 312 | - } |
|
| 313 | - } |
|
| 314 | - } |
|
| 315 | - |
|
| 316 | - public function delegateEventListenerRegistrations(IEventDispatcher $eventDispatcher): void { |
|
| 317 | - foreach ($this->eventListeners as $registration) { |
|
| 318 | - try { |
|
| 319 | - if (isset($registration['priority'])) { |
|
| 320 | - $eventDispatcher->addServiceListener( |
|
| 321 | - $registration['event'], |
|
| 322 | - $registration['listener'], |
|
| 323 | - $registration['priority'] |
|
| 324 | - ); |
|
| 325 | - } else { |
|
| 326 | - $eventDispatcher->addServiceListener( |
|
| 327 | - $registration['event'], |
|
| 328 | - $registration['listener'] |
|
| 329 | - ); |
|
| 330 | - } |
|
| 331 | - } catch (Throwable $e) { |
|
| 332 | - $appId = $registration['appId']; |
|
| 333 | - $this->logger->logException($e, [ |
|
| 334 | - 'message' => "Error during event listener registration of $appId: " . $e->getMessage(), |
|
| 335 | - 'level' => ILogger::ERROR, |
|
| 336 | - ]); |
|
| 337 | - } |
|
| 338 | - } |
|
| 339 | - } |
|
| 340 | - |
|
| 341 | - /** |
|
| 342 | - * @param App[] $apps |
|
| 343 | - */ |
|
| 344 | - public function delegateContainerRegistrations(array $apps): void { |
|
| 345 | - foreach ($this->services as $registration) { |
|
| 346 | - try { |
|
| 347 | - /** |
|
| 348 | - * Register the service and convert the callable into a \Closure if necessary |
|
| 349 | - */ |
|
| 350 | - $apps[$registration['appId']] |
|
| 351 | - ->getContainer() |
|
| 352 | - ->registerService( |
|
| 353 | - $registration['name'], |
|
| 354 | - Closure::fromCallable($registration['factory']), |
|
| 355 | - $registration['shared'] ?? true |
|
| 356 | - ); |
|
| 357 | - } catch (Throwable $e) { |
|
| 358 | - $appId = $registration['appId']; |
|
| 359 | - $this->logger->logException($e, [ |
|
| 360 | - 'message' => "Error during service registration of $appId: " . $e->getMessage(), |
|
| 361 | - 'level' => ILogger::ERROR, |
|
| 362 | - ]); |
|
| 363 | - } |
|
| 364 | - } |
|
| 365 | - |
|
| 366 | - foreach ($this->aliases as $registration) { |
|
| 367 | - try { |
|
| 368 | - $apps[$registration['appId']] |
|
| 369 | - ->getContainer() |
|
| 370 | - ->registerAlias( |
|
| 371 | - $registration['alias'], |
|
| 372 | - $registration['target'] |
|
| 373 | - ); |
|
| 374 | - } catch (Throwable $e) { |
|
| 375 | - $appId = $registration['appId']; |
|
| 376 | - $this->logger->logException($e, [ |
|
| 377 | - 'message' => "Error during service alias registration of $appId: " . $e->getMessage(), |
|
| 378 | - 'level' => ILogger::ERROR, |
|
| 379 | - ]); |
|
| 380 | - } |
|
| 381 | - } |
|
| 382 | - |
|
| 383 | - foreach ($this->parameters as $registration) { |
|
| 384 | - try { |
|
| 385 | - $apps[$registration['appId']] |
|
| 386 | - ->getContainer() |
|
| 387 | - ->registerParameter( |
|
| 388 | - $registration['name'], |
|
| 389 | - $registration['value'] |
|
| 390 | - ); |
|
| 391 | - } catch (Throwable $e) { |
|
| 392 | - $appId = $registration['appId']; |
|
| 393 | - $this->logger->logException($e, [ |
|
| 394 | - 'message' => "Error during service alias registration of $appId: " . $e->getMessage(), |
|
| 395 | - 'level' => ILogger::ERROR, |
|
| 396 | - ]); |
|
| 397 | - } |
|
| 398 | - } |
|
| 399 | - } |
|
| 400 | - |
|
| 401 | - /** |
|
| 402 | - * @param App[] $apps |
|
| 403 | - */ |
|
| 404 | - public function delegateMiddlewareRegistrations(array $apps): void { |
|
| 405 | - foreach ($this->middlewares as $middleware) { |
|
| 406 | - try { |
|
| 407 | - $apps[$middleware['appId']] |
|
| 408 | - ->getContainer() |
|
| 409 | - ->registerMiddleWare($middleware['class']); |
|
| 410 | - } catch (Throwable $e) { |
|
| 411 | - $appId = $middleware['appId']; |
|
| 412 | - $this->logger->logException($e, [ |
|
| 413 | - 'message' => "Error during capability registration of $appId: " . $e->getMessage(), |
|
| 414 | - 'level' => ILogger::ERROR, |
|
| 415 | - ]); |
|
| 416 | - } |
|
| 417 | - } |
|
| 418 | - } |
|
| 419 | - |
|
| 420 | - /** |
|
| 421 | - * @return array[] |
|
| 422 | - */ |
|
| 423 | - public function getSearchProviders(): array { |
|
| 424 | - return $this->searchProviders; |
|
| 425 | - } |
|
| 426 | - |
|
| 427 | - /** |
|
| 428 | - * @return array[] |
|
| 429 | - */ |
|
| 430 | - public function getAlternativeLogins(): array { |
|
| 431 | - return $this->alternativeLogins; |
|
| 432 | - } |
|
| 433 | - |
|
| 434 | - /** |
|
| 435 | - * @erturn array[] |
|
| 436 | - */ |
|
| 437 | - public function getInitialStates(): array { |
|
| 438 | - return $this->initialStates; |
|
| 439 | - } |
|
| 42 | + /** @var array[] */ |
|
| 43 | + private $capabilities = []; |
|
| 44 | + |
|
| 45 | + /** @var array[] */ |
|
| 46 | + private $crashReporters = []; |
|
| 47 | + |
|
| 48 | + /** @var array[] */ |
|
| 49 | + private $dashboardPanels = []; |
|
| 50 | + |
|
| 51 | + /** @var array[] */ |
|
| 52 | + private $services = []; |
|
| 53 | + |
|
| 54 | + /** @var array[] */ |
|
| 55 | + private $aliases = []; |
|
| 56 | + |
|
| 57 | + /** @var array[] */ |
|
| 58 | + private $parameters = []; |
|
| 59 | + |
|
| 60 | + /** @var array[] */ |
|
| 61 | + private $eventListeners = []; |
|
| 62 | + |
|
| 63 | + /** @var array[] */ |
|
| 64 | + private $middlewares = []; |
|
| 65 | + |
|
| 66 | + /** @var array[] */ |
|
| 67 | + private $searchProviders = []; |
|
| 68 | + |
|
| 69 | + /** @var array[] */ |
|
| 70 | + private $alternativeLogins = []; |
|
| 71 | + |
|
| 72 | + /** @var array[] */ |
|
| 73 | + private $initialStates = []; |
|
| 74 | + |
|
| 75 | + /** @var ILogger */ |
|
| 76 | + private $logger; |
|
| 77 | + |
|
| 78 | + public function __construct(ILogger $logger) { |
|
| 79 | + $this->logger = $logger; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + public function for(string $appId): IRegistrationContext { |
|
| 83 | + return new class($appId, $this) implements IRegistrationContext { |
|
| 84 | + /** @var string */ |
|
| 85 | + private $appId; |
|
| 86 | + |
|
| 87 | + /** @var RegistrationContext */ |
|
| 88 | + private $context; |
|
| 89 | + |
|
| 90 | + public function __construct(string $appId, RegistrationContext $context) { |
|
| 91 | + $this->appId = $appId; |
|
| 92 | + $this->context = $context; |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + public function registerCapability(string $capability): void { |
|
| 96 | + $this->context->registerCapability( |
|
| 97 | + $this->appId, |
|
| 98 | + $capability |
|
| 99 | + ); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + public function registerCrashReporter(string $reporterClass): void { |
|
| 103 | + $this->context->registerCrashReporter( |
|
| 104 | + $this->appId, |
|
| 105 | + $reporterClass |
|
| 106 | + ); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + public function registerDashboardWidget(string $widgetClass): void { |
|
| 110 | + $this->context->registerDashboardPanel( |
|
| 111 | + $this->appId, |
|
| 112 | + $widgetClass |
|
| 113 | + ); |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + public function registerService(string $name, callable $factory, bool $shared = true): void { |
|
| 117 | + $this->context->registerService( |
|
| 118 | + $this->appId, |
|
| 119 | + $name, |
|
| 120 | + $factory, |
|
| 121 | + $shared |
|
| 122 | + ); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + public function registerServiceAlias(string $alias, string $target): void { |
|
| 126 | + $this->context->registerServiceAlias( |
|
| 127 | + $this->appId, |
|
| 128 | + $alias, |
|
| 129 | + $target |
|
| 130 | + ); |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + public function registerParameter(string $name, $value): void { |
|
| 134 | + $this->context->registerParameter( |
|
| 135 | + $this->appId, |
|
| 136 | + $name, |
|
| 137 | + $value |
|
| 138 | + ); |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + public function registerEventListener(string $event, string $listener, int $priority = 0): void { |
|
| 142 | + $this->context->registerEventListener( |
|
| 143 | + $this->appId, |
|
| 144 | + $event, |
|
| 145 | + $listener, |
|
| 146 | + $priority |
|
| 147 | + ); |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + public function registerMiddleware(string $class): void { |
|
| 151 | + $this->context->registerMiddleware( |
|
| 152 | + $this->appId, |
|
| 153 | + $class |
|
| 154 | + ); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + public function registerSearchProvider(string $class): void { |
|
| 158 | + $this->context->registerSearchProvider( |
|
| 159 | + $this->appId, |
|
| 160 | + $class |
|
| 161 | + ); |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + public function registerAlternativeLogin(string $class): void { |
|
| 165 | + $this->context->registerAlternativeLogin( |
|
| 166 | + $this->appId, |
|
| 167 | + $class |
|
| 168 | + ); |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + public function registerInitialStateProvider(string $class): void { |
|
| 172 | + $this->context->registerInitialState( |
|
| 173 | + $this->appId, |
|
| 174 | + $class |
|
| 175 | + ); |
|
| 176 | + } |
|
| 177 | + }; |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + public function registerCapability(string $appId, string $capability): void { |
|
| 181 | + $this->capabilities[] = [ |
|
| 182 | + 'appId' => $appId, |
|
| 183 | + 'capability' => $capability |
|
| 184 | + ]; |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + public function registerCrashReporter(string $appId, string $reporterClass): void { |
|
| 188 | + $this->crashReporters[] = [ |
|
| 189 | + 'appId' => $appId, |
|
| 190 | + 'class' => $reporterClass, |
|
| 191 | + ]; |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + public function registerDashboardPanel(string $appId, string $panelClass): void { |
|
| 195 | + $this->dashboardPanels[] = [ |
|
| 196 | + 'appId' => $appId, |
|
| 197 | + 'class' => $panelClass |
|
| 198 | + ]; |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + public function registerService(string $appId, string $name, callable $factory, bool $shared = true): void { |
|
| 202 | + $this->services[] = [ |
|
| 203 | + "appId" => $appId, |
|
| 204 | + "name" => $name, |
|
| 205 | + "factory" => $factory, |
|
| 206 | + "sharred" => $shared, |
|
| 207 | + ]; |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + public function registerServiceAlias(string $appId, string $alias, string $target): void { |
|
| 211 | + $this->aliases[] = [ |
|
| 212 | + "appId" => $appId, |
|
| 213 | + "alias" => $alias, |
|
| 214 | + "target" => $target, |
|
| 215 | + ]; |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + public function registerParameter(string $appId, string $name, $value): void { |
|
| 219 | + $this->parameters[] = [ |
|
| 220 | + "appId" => $appId, |
|
| 221 | + "name" => $name, |
|
| 222 | + "value" => $value, |
|
| 223 | + ]; |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + public function registerEventListener(string $appId, string $event, string $listener, int $priority = 0): void { |
|
| 227 | + $this->eventListeners[] = [ |
|
| 228 | + "appId" => $appId, |
|
| 229 | + "event" => $event, |
|
| 230 | + "listener" => $listener, |
|
| 231 | + "priority" => $priority, |
|
| 232 | + ]; |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + public function registerMiddleware(string $appId, string $class): void { |
|
| 236 | + $this->middlewares[] = [ |
|
| 237 | + "appId" => $appId, |
|
| 238 | + "class" => $class, |
|
| 239 | + ]; |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + public function registerSearchProvider(string $appId, string $class) { |
|
| 243 | + $this->searchProviders[] = [ |
|
| 244 | + 'appId' => $appId, |
|
| 245 | + 'class' => $class, |
|
| 246 | + ]; |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + public function registerAlternativeLogin(string $appId, string $class): void { |
|
| 250 | + $this->alternativeLogins[] = [ |
|
| 251 | + 'appId' => $appId, |
|
| 252 | + 'class' => $class, |
|
| 253 | + ]; |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + public function registerInitialState(string $appId, string $class): void { |
|
| 257 | + $this->initialStates[] = [ |
|
| 258 | + 'appId' => $appId, |
|
| 259 | + 'class' => $class, |
|
| 260 | + ]; |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + /** |
|
| 264 | + * @param App[] $apps |
|
| 265 | + */ |
|
| 266 | + public function delegateCapabilityRegistrations(array $apps): void { |
|
| 267 | + foreach ($this->capabilities as $registration) { |
|
| 268 | + try { |
|
| 269 | + $apps[$registration['appId']] |
|
| 270 | + ->getContainer() |
|
| 271 | + ->registerCapability($registration['capability']); |
|
| 272 | + } catch (Throwable $e) { |
|
| 273 | + $appId = $registration['appId']; |
|
| 274 | + $this->logger->logException($e, [ |
|
| 275 | + 'message' => "Error during capability registration of $appId: " . $e->getMessage(), |
|
| 276 | + 'level' => ILogger::ERROR, |
|
| 277 | + ]); |
|
| 278 | + } |
|
| 279 | + } |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + /** |
|
| 283 | + * @param App[] $apps |
|
| 284 | + */ |
|
| 285 | + public function delegateCrashReporterRegistrations(array $apps, Registry $registry): void { |
|
| 286 | + foreach ($this->crashReporters as $registration) { |
|
| 287 | + try { |
|
| 288 | + $registry->registerLazy($registration['class']); |
|
| 289 | + } catch (Throwable $e) { |
|
| 290 | + $appId = $registration['appId']; |
|
| 291 | + $this->logger->logException($e, [ |
|
| 292 | + 'message' => "Error during crash reporter registration of $appId: " . $e->getMessage(), |
|
| 293 | + 'level' => ILogger::ERROR, |
|
| 294 | + ]); |
|
| 295 | + } |
|
| 296 | + } |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + /** |
|
| 300 | + * @param App[] $apps |
|
| 301 | + */ |
|
| 302 | + public function delegateDashboardPanelRegistrations(array $apps, IManager $dashboardManager): void { |
|
| 303 | + foreach ($this->dashboardPanels as $panel) { |
|
| 304 | + try { |
|
| 305 | + $dashboardManager->lazyRegisterWidget($panel['class']); |
|
| 306 | + } catch (Throwable $e) { |
|
| 307 | + $appId = $panel['appId']; |
|
| 308 | + $this->logger->logException($e, [ |
|
| 309 | + 'message' => "Error during dashboard registration of $appId: " . $e->getMessage(), |
|
| 310 | + 'level' => ILogger::ERROR, |
|
| 311 | + ]); |
|
| 312 | + } |
|
| 313 | + } |
|
| 314 | + } |
|
| 315 | + |
|
| 316 | + public function delegateEventListenerRegistrations(IEventDispatcher $eventDispatcher): void { |
|
| 317 | + foreach ($this->eventListeners as $registration) { |
|
| 318 | + try { |
|
| 319 | + if (isset($registration['priority'])) { |
|
| 320 | + $eventDispatcher->addServiceListener( |
|
| 321 | + $registration['event'], |
|
| 322 | + $registration['listener'], |
|
| 323 | + $registration['priority'] |
|
| 324 | + ); |
|
| 325 | + } else { |
|
| 326 | + $eventDispatcher->addServiceListener( |
|
| 327 | + $registration['event'], |
|
| 328 | + $registration['listener'] |
|
| 329 | + ); |
|
| 330 | + } |
|
| 331 | + } catch (Throwable $e) { |
|
| 332 | + $appId = $registration['appId']; |
|
| 333 | + $this->logger->logException($e, [ |
|
| 334 | + 'message' => "Error during event listener registration of $appId: " . $e->getMessage(), |
|
| 335 | + 'level' => ILogger::ERROR, |
|
| 336 | + ]); |
|
| 337 | + } |
|
| 338 | + } |
|
| 339 | + } |
|
| 340 | + |
|
| 341 | + /** |
|
| 342 | + * @param App[] $apps |
|
| 343 | + */ |
|
| 344 | + public function delegateContainerRegistrations(array $apps): void { |
|
| 345 | + foreach ($this->services as $registration) { |
|
| 346 | + try { |
|
| 347 | + /** |
|
| 348 | + * Register the service and convert the callable into a \Closure if necessary |
|
| 349 | + */ |
|
| 350 | + $apps[$registration['appId']] |
|
| 351 | + ->getContainer() |
|
| 352 | + ->registerService( |
|
| 353 | + $registration['name'], |
|
| 354 | + Closure::fromCallable($registration['factory']), |
|
| 355 | + $registration['shared'] ?? true |
|
| 356 | + ); |
|
| 357 | + } catch (Throwable $e) { |
|
| 358 | + $appId = $registration['appId']; |
|
| 359 | + $this->logger->logException($e, [ |
|
| 360 | + 'message' => "Error during service registration of $appId: " . $e->getMessage(), |
|
| 361 | + 'level' => ILogger::ERROR, |
|
| 362 | + ]); |
|
| 363 | + } |
|
| 364 | + } |
|
| 365 | + |
|
| 366 | + foreach ($this->aliases as $registration) { |
|
| 367 | + try { |
|
| 368 | + $apps[$registration['appId']] |
|
| 369 | + ->getContainer() |
|
| 370 | + ->registerAlias( |
|
| 371 | + $registration['alias'], |
|
| 372 | + $registration['target'] |
|
| 373 | + ); |
|
| 374 | + } catch (Throwable $e) { |
|
| 375 | + $appId = $registration['appId']; |
|
| 376 | + $this->logger->logException($e, [ |
|
| 377 | + 'message' => "Error during service alias registration of $appId: " . $e->getMessage(), |
|
| 378 | + 'level' => ILogger::ERROR, |
|
| 379 | + ]); |
|
| 380 | + } |
|
| 381 | + } |
|
| 382 | + |
|
| 383 | + foreach ($this->parameters as $registration) { |
|
| 384 | + try { |
|
| 385 | + $apps[$registration['appId']] |
|
| 386 | + ->getContainer() |
|
| 387 | + ->registerParameter( |
|
| 388 | + $registration['name'], |
|
| 389 | + $registration['value'] |
|
| 390 | + ); |
|
| 391 | + } catch (Throwable $e) { |
|
| 392 | + $appId = $registration['appId']; |
|
| 393 | + $this->logger->logException($e, [ |
|
| 394 | + 'message' => "Error during service alias registration of $appId: " . $e->getMessage(), |
|
| 395 | + 'level' => ILogger::ERROR, |
|
| 396 | + ]); |
|
| 397 | + } |
|
| 398 | + } |
|
| 399 | + } |
|
| 400 | + |
|
| 401 | + /** |
|
| 402 | + * @param App[] $apps |
|
| 403 | + */ |
|
| 404 | + public function delegateMiddlewareRegistrations(array $apps): void { |
|
| 405 | + foreach ($this->middlewares as $middleware) { |
|
| 406 | + try { |
|
| 407 | + $apps[$middleware['appId']] |
|
| 408 | + ->getContainer() |
|
| 409 | + ->registerMiddleWare($middleware['class']); |
|
| 410 | + } catch (Throwable $e) { |
|
| 411 | + $appId = $middleware['appId']; |
|
| 412 | + $this->logger->logException($e, [ |
|
| 413 | + 'message' => "Error during capability registration of $appId: " . $e->getMessage(), |
|
| 414 | + 'level' => ILogger::ERROR, |
|
| 415 | + ]); |
|
| 416 | + } |
|
| 417 | + } |
|
| 418 | + } |
|
| 419 | + |
|
| 420 | + /** |
|
| 421 | + * @return array[] |
|
| 422 | + */ |
|
| 423 | + public function getSearchProviders(): array { |
|
| 424 | + return $this->searchProviders; |
|
| 425 | + } |
|
| 426 | + |
|
| 427 | + /** |
|
| 428 | + * @return array[] |
|
| 429 | + */ |
|
| 430 | + public function getAlternativeLogins(): array { |
|
| 431 | + return $this->alternativeLogins; |
|
| 432 | + } |
|
| 433 | + |
|
| 434 | + /** |
|
| 435 | + * @erturn array[] |
|
| 436 | + */ |
|
| 437 | + public function getInitialStates(): array { |
|
| 438 | + return $this->initialStates; |
|
| 439 | + } |
|
| 440 | 440 | } |
@@ -37,115 +37,115 @@ |
||
| 37 | 37 | |
| 38 | 38 | class InitialStateService implements IInitialStateService { |
| 39 | 39 | |
| 40 | - /** @var ILogger */ |
|
| 41 | - private $logger; |
|
| 42 | - |
|
| 43 | - /** @var string[][] */ |
|
| 44 | - private $states = []; |
|
| 45 | - |
|
| 46 | - /** @var Closure[][] */ |
|
| 47 | - private $lazyStates = []; |
|
| 48 | - |
|
| 49 | - /** @var Coordinator */ |
|
| 50 | - private $bootstrapCoordinator; |
|
| 51 | - |
|
| 52 | - /** @var IServerContainer */ |
|
| 53 | - private $container; |
|
| 54 | - |
|
| 55 | - public function __construct(ILogger $logger, Coordinator $bootstrapCoordinator, IServerContainer $container) { |
|
| 56 | - $this->logger = $logger; |
|
| 57 | - $this->bootstrapCoordinator = $bootstrapCoordinator; |
|
| 58 | - $this->container = $container; |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - public function provideInitialState(string $appName, string $key, $data): void { |
|
| 62 | - // Scalars and JsonSerializable are fine |
|
| 63 | - if (is_scalar($data) || $data instanceof \JsonSerializable || is_array($data)) { |
|
| 64 | - if (!isset($this->states[$appName])) { |
|
| 65 | - $this->states[$appName] = []; |
|
| 66 | - } |
|
| 67 | - $this->states[$appName][$key] = json_encode($data); |
|
| 68 | - return; |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - $this->logger->warning('Invalid data provided to provideInitialState by ' . $appName); |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - public function provideLazyInitialState(string $appName, string $key, Closure $closure): void { |
|
| 75 | - if (!isset($this->lazyStates[$appName])) { |
|
| 76 | - $this->lazyStates[$appName] = []; |
|
| 77 | - } |
|
| 78 | - $this->lazyStates[$appName][$key] = $closure; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Invoke all callbacks to populate the `states` property |
|
| 83 | - */ |
|
| 84 | - private function invokeLazyStateCallbacks(): void { |
|
| 85 | - foreach ($this->lazyStates as $app => $lazyStates) { |
|
| 86 | - foreach ($lazyStates as $key => $lazyState) { |
|
| 87 | - $startTime = microtime(true); |
|
| 88 | - $this->provideInitialState($app, $key, $lazyState()); |
|
| 89 | - $endTime = microtime(true); |
|
| 90 | - $duration = $endTime - $startTime; |
|
| 91 | - if ($duration > 1) { |
|
| 92 | - $this->logger->warning('Lazy initial state provider for {key} took {duration} seconds.', [ |
|
| 93 | - 'app' => $app, |
|
| 94 | - 'key' => $key, |
|
| 95 | - 'duration' => round($duration, 2), |
|
| 96 | - ]); |
|
| 97 | - } |
|
| 98 | - } |
|
| 99 | - } |
|
| 100 | - $this->lazyStates = []; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * Load the lazy states via the IBootstrap mechanism |
|
| 105 | - */ |
|
| 106 | - private function loadLazyStates(): void { |
|
| 107 | - $context = $this->bootstrapCoordinator->getRegistrationContext(); |
|
| 108 | - |
|
| 109 | - if ($context === null) { |
|
| 110 | - // To early, nothing to do yet |
|
| 111 | - return; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - $initialStates = $context->getInitialStates(); |
|
| 115 | - foreach ($initialStates as $initialState) { |
|
| 116 | - try { |
|
| 117 | - $provider = $this->container->query($initialState['class']); |
|
| 118 | - } catch (QueryException $e) { |
|
| 119 | - // Log an continue. We can be fault tolerant here. |
|
| 120 | - $this->logger->logException($e, [ |
|
| 121 | - 'message' => 'Could not load initial state provider dynamically: ' . $e->getMessage(), |
|
| 122 | - 'level' => ILogger::ERROR, |
|
| 123 | - 'app' => $initialState['appId'], |
|
| 124 | - ]); |
|
| 125 | - continue; |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - if (!($provider instanceof InitialStateProvider)) { |
|
| 129 | - // Log an continue. We can be fault tolerant here. |
|
| 130 | - $this->logger->error('Initial state provider is not an InitialStateProvider instance: ' . $initialState['class'], [ |
|
| 131 | - 'app' => $initialState['appId'], |
|
| 132 | - ]); |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - $this->provideInitialState($initialState['appId'], $provider->getKey(), $provider); |
|
| 136 | - } |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - public function getInitialStates(): array { |
|
| 140 | - $this->invokeLazyStateCallbacks(); |
|
| 141 | - $this->loadLazyStates(); |
|
| 142 | - |
|
| 143 | - $appStates = []; |
|
| 144 | - foreach ($this->states as $app => $states) { |
|
| 145 | - foreach ($states as $key => $value) { |
|
| 146 | - $appStates["$app-$key"] = $value; |
|
| 147 | - } |
|
| 148 | - } |
|
| 149 | - return $appStates; |
|
| 150 | - } |
|
| 40 | + /** @var ILogger */ |
|
| 41 | + private $logger; |
|
| 42 | + |
|
| 43 | + /** @var string[][] */ |
|
| 44 | + private $states = []; |
|
| 45 | + |
|
| 46 | + /** @var Closure[][] */ |
|
| 47 | + private $lazyStates = []; |
|
| 48 | + |
|
| 49 | + /** @var Coordinator */ |
|
| 50 | + private $bootstrapCoordinator; |
|
| 51 | + |
|
| 52 | + /** @var IServerContainer */ |
|
| 53 | + private $container; |
|
| 54 | + |
|
| 55 | + public function __construct(ILogger $logger, Coordinator $bootstrapCoordinator, IServerContainer $container) { |
|
| 56 | + $this->logger = $logger; |
|
| 57 | + $this->bootstrapCoordinator = $bootstrapCoordinator; |
|
| 58 | + $this->container = $container; |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + public function provideInitialState(string $appName, string $key, $data): void { |
|
| 62 | + // Scalars and JsonSerializable are fine |
|
| 63 | + if (is_scalar($data) || $data instanceof \JsonSerializable || is_array($data)) { |
|
| 64 | + if (!isset($this->states[$appName])) { |
|
| 65 | + $this->states[$appName] = []; |
|
| 66 | + } |
|
| 67 | + $this->states[$appName][$key] = json_encode($data); |
|
| 68 | + return; |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + $this->logger->warning('Invalid data provided to provideInitialState by ' . $appName); |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + public function provideLazyInitialState(string $appName, string $key, Closure $closure): void { |
|
| 75 | + if (!isset($this->lazyStates[$appName])) { |
|
| 76 | + $this->lazyStates[$appName] = []; |
|
| 77 | + } |
|
| 78 | + $this->lazyStates[$appName][$key] = $closure; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Invoke all callbacks to populate the `states` property |
|
| 83 | + */ |
|
| 84 | + private function invokeLazyStateCallbacks(): void { |
|
| 85 | + foreach ($this->lazyStates as $app => $lazyStates) { |
|
| 86 | + foreach ($lazyStates as $key => $lazyState) { |
|
| 87 | + $startTime = microtime(true); |
|
| 88 | + $this->provideInitialState($app, $key, $lazyState()); |
|
| 89 | + $endTime = microtime(true); |
|
| 90 | + $duration = $endTime - $startTime; |
|
| 91 | + if ($duration > 1) { |
|
| 92 | + $this->logger->warning('Lazy initial state provider for {key} took {duration} seconds.', [ |
|
| 93 | + 'app' => $app, |
|
| 94 | + 'key' => $key, |
|
| 95 | + 'duration' => round($duration, 2), |
|
| 96 | + ]); |
|
| 97 | + } |
|
| 98 | + } |
|
| 99 | + } |
|
| 100 | + $this->lazyStates = []; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * Load the lazy states via the IBootstrap mechanism |
|
| 105 | + */ |
|
| 106 | + private function loadLazyStates(): void { |
|
| 107 | + $context = $this->bootstrapCoordinator->getRegistrationContext(); |
|
| 108 | + |
|
| 109 | + if ($context === null) { |
|
| 110 | + // To early, nothing to do yet |
|
| 111 | + return; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + $initialStates = $context->getInitialStates(); |
|
| 115 | + foreach ($initialStates as $initialState) { |
|
| 116 | + try { |
|
| 117 | + $provider = $this->container->query($initialState['class']); |
|
| 118 | + } catch (QueryException $e) { |
|
| 119 | + // Log an continue. We can be fault tolerant here. |
|
| 120 | + $this->logger->logException($e, [ |
|
| 121 | + 'message' => 'Could not load initial state provider dynamically: ' . $e->getMessage(), |
|
| 122 | + 'level' => ILogger::ERROR, |
|
| 123 | + 'app' => $initialState['appId'], |
|
| 124 | + ]); |
|
| 125 | + continue; |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + if (!($provider instanceof InitialStateProvider)) { |
|
| 129 | + // Log an continue. We can be fault tolerant here. |
|
| 130 | + $this->logger->error('Initial state provider is not an InitialStateProvider instance: ' . $initialState['class'], [ |
|
| 131 | + 'app' => $initialState['appId'], |
|
| 132 | + ]); |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + $this->provideInitialState($initialState['appId'], $provider->getKey(), $provider); |
|
| 136 | + } |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + public function getInitialStates(): array { |
|
| 140 | + $this->invokeLazyStateCallbacks(); |
|
| 141 | + $this->loadLazyStates(); |
|
| 142 | + |
|
| 143 | + $appStates = []; |
|
| 144 | + foreach ($this->states as $app => $states) { |
|
| 145 | + foreach ($states as $key => $value) { |
|
| 146 | + $appStates["$app-$key"] = $value; |
|
| 147 | + } |
|
| 148 | + } |
|
| 149 | + return $appStates; |
|
| 150 | + } |
|
| 151 | 151 | } |
@@ -68,7 +68,7 @@ discard block |
||
| 68 | 68 | return; |
| 69 | 69 | } |
| 70 | 70 | |
| 71 | - $this->logger->warning('Invalid data provided to provideInitialState by ' . $appName); |
|
| 71 | + $this->logger->warning('Invalid data provided to provideInitialState by '.$appName); |
|
| 72 | 72 | } |
| 73 | 73 | |
| 74 | 74 | public function provideLazyInitialState(string $appName, string $key, Closure $closure): void { |
@@ -118,7 +118,7 @@ discard block |
||
| 118 | 118 | } catch (QueryException $e) { |
| 119 | 119 | // Log an continue. We can be fault tolerant here. |
| 120 | 120 | $this->logger->logException($e, [ |
| 121 | - 'message' => 'Could not load initial state provider dynamically: ' . $e->getMessage(), |
|
| 121 | + 'message' => 'Could not load initial state provider dynamically: '.$e->getMessage(), |
|
| 122 | 122 | 'level' => ILogger::ERROR, |
| 123 | 123 | 'app' => $initialState['appId'], |
| 124 | 124 | ]); |
@@ -127,7 +127,7 @@ discard block |
||
| 127 | 127 | |
| 128 | 128 | if (!($provider instanceof InitialStateProvider)) { |
| 129 | 129 | // Log an continue. We can be fault tolerant here. |
| 130 | - $this->logger->error('Initial state provider is not an InitialStateProvider instance: ' . $initialState['class'], [ |
|
| 130 | + $this->logger->error('Initial state provider is not an InitialStateProvider instance: '.$initialState['class'], [ |
|
| 131 | 131 | 'app' => $initialState['appId'], |
| 132 | 132 | ]); |
| 133 | 133 | } |
@@ -30,20 +30,20 @@ |
||
| 30 | 30 | */ |
| 31 | 31 | abstract class InitialStateProvider implements \JsonSerializable { |
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * @since 21.0.0 |
|
| 35 | - */ |
|
| 36 | - abstract public function getKey(): string; |
|
| 33 | + /** |
|
| 34 | + * @since 21.0.0 |
|
| 35 | + */ |
|
| 36 | + abstract public function getKey(): string; |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * @since 21.0.0 |
|
| 40 | - */ |
|
| 41 | - abstract public function getData(); |
|
| 38 | + /** |
|
| 39 | + * @since 21.0.0 |
|
| 40 | + */ |
|
| 41 | + abstract public function getData(); |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * @since 21.0.0 |
|
| 45 | - */ |
|
| 46 | - final public function jsonSerialize() { |
|
| 47 | - return $this->getData(); |
|
| 48 | - } |
|
| 43 | + /** |
|
| 44 | + * @since 21.0.0 |
|
| 45 | + */ |
|
| 46 | + final public function jsonSerialize() { |
|
| 47 | + return $this->getData(); |
|
| 48 | + } |
|
| 49 | 49 | } |
@@ -40,132 +40,132 @@ |
||
| 40 | 40 | */ |
| 41 | 41 | interface IRegistrationContext { |
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * @param string $capability |
|
| 45 | - * @see IAppContainer::registerCapability |
|
| 46 | - * |
|
| 47 | - * @since 20.0.0 |
|
| 48 | - */ |
|
| 49 | - public function registerCapability(string $capability): void; |
|
| 43 | + /** |
|
| 44 | + * @param string $capability |
|
| 45 | + * @see IAppContainer::registerCapability |
|
| 46 | + * |
|
| 47 | + * @since 20.0.0 |
|
| 48 | + */ |
|
| 49 | + public function registerCapability(string $capability): void; |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * Register an implementation of \OCP\Support\CrashReport\IReporter that |
|
| 53 | - * will receive unhandled exceptions and throwables |
|
| 54 | - * |
|
| 55 | - * @param string $reporterClass |
|
| 56 | - * @return void |
|
| 57 | - * @since 20.0.0 |
|
| 58 | - */ |
|
| 59 | - public function registerCrashReporter(string $reporterClass): void; |
|
| 51 | + /** |
|
| 52 | + * Register an implementation of \OCP\Support\CrashReport\IReporter that |
|
| 53 | + * will receive unhandled exceptions and throwables |
|
| 54 | + * |
|
| 55 | + * @param string $reporterClass |
|
| 56 | + * @return void |
|
| 57 | + * @since 20.0.0 |
|
| 58 | + */ |
|
| 59 | + public function registerCrashReporter(string $reporterClass): void; |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * Register an implementation of \OCP\Dashboard\IPanel that |
|
| 63 | - * will handle the implementation of a dashboard panel |
|
| 64 | - * |
|
| 65 | - * @param string $widgetClass |
|
| 66 | - * @return void |
|
| 67 | - * @since 20.0.0 |
|
| 68 | - */ |
|
| 69 | - public function registerDashboardWidget(string $widgetClass): void; |
|
| 70 | - /** |
|
| 71 | - * Register a service |
|
| 72 | - * |
|
| 73 | - * @param string $name |
|
| 74 | - * @param callable $factory |
|
| 75 | - * @param bool $shared |
|
| 76 | - * |
|
| 77 | - * @return void |
|
| 78 | - * @see IContainer::registerService() |
|
| 79 | - * |
|
| 80 | - * @since 20.0.0 |
|
| 81 | - */ |
|
| 82 | - public function registerService(string $name, callable $factory, bool $shared = true): void; |
|
| 61 | + /** |
|
| 62 | + * Register an implementation of \OCP\Dashboard\IPanel that |
|
| 63 | + * will handle the implementation of a dashboard panel |
|
| 64 | + * |
|
| 65 | + * @param string $widgetClass |
|
| 66 | + * @return void |
|
| 67 | + * @since 20.0.0 |
|
| 68 | + */ |
|
| 69 | + public function registerDashboardWidget(string $widgetClass): void; |
|
| 70 | + /** |
|
| 71 | + * Register a service |
|
| 72 | + * |
|
| 73 | + * @param string $name |
|
| 74 | + * @param callable $factory |
|
| 75 | + * @param bool $shared |
|
| 76 | + * |
|
| 77 | + * @return void |
|
| 78 | + * @see IContainer::registerService() |
|
| 79 | + * |
|
| 80 | + * @since 20.0.0 |
|
| 81 | + */ |
|
| 82 | + public function registerService(string $name, callable $factory, bool $shared = true): void; |
|
| 83 | 83 | |
| 84 | - /** |
|
| 85 | - * @param string $alias |
|
| 86 | - * @param string $target |
|
| 87 | - * |
|
| 88 | - * @return void |
|
| 89 | - * @see IContainer::registerAlias() |
|
| 90 | - * |
|
| 91 | - * @since 20.0.0 |
|
| 92 | - */ |
|
| 93 | - public function registerServiceAlias(string $alias, string $target): void; |
|
| 84 | + /** |
|
| 85 | + * @param string $alias |
|
| 86 | + * @param string $target |
|
| 87 | + * |
|
| 88 | + * @return void |
|
| 89 | + * @see IContainer::registerAlias() |
|
| 90 | + * |
|
| 91 | + * @since 20.0.0 |
|
| 92 | + */ |
|
| 93 | + public function registerServiceAlias(string $alias, string $target): void; |
|
| 94 | 94 | |
| 95 | - /** |
|
| 96 | - * @param string $name |
|
| 97 | - * @param mixed $value |
|
| 98 | - * |
|
| 99 | - * @return void |
|
| 100 | - * @see IContainer::registerParameter() |
|
| 101 | - * |
|
| 102 | - * @since 20.0.0 |
|
| 103 | - */ |
|
| 104 | - public function registerParameter(string $name, $value): void; |
|
| 95 | + /** |
|
| 96 | + * @param string $name |
|
| 97 | + * @param mixed $value |
|
| 98 | + * |
|
| 99 | + * @return void |
|
| 100 | + * @see IContainer::registerParameter() |
|
| 101 | + * |
|
| 102 | + * @since 20.0.0 |
|
| 103 | + */ |
|
| 104 | + public function registerParameter(string $name, $value): void; |
|
| 105 | 105 | |
| 106 | - /** |
|
| 107 | - * Register a service listener |
|
| 108 | - * |
|
| 109 | - * This is equivalent to calling IEventDispatcher::addServiceListener |
|
| 110 | - * |
|
| 111 | - * @param string $event preferably the fully-qualified class name of the Event sub class to listen for |
|
| 112 | - * @param string $listener fully qualified class name (or ::class notation) of a \OCP\EventDispatcher\IEventListener that can be built by the DI container |
|
| 113 | - * @param int $priority |
|
| 114 | - * |
|
| 115 | - * @see IEventDispatcher::addServiceListener() |
|
| 116 | - * |
|
| 117 | - * @since 20.0.0 |
|
| 118 | - */ |
|
| 119 | - public function registerEventListener(string $event, string $listener, int $priority = 0): void; |
|
| 106 | + /** |
|
| 107 | + * Register a service listener |
|
| 108 | + * |
|
| 109 | + * This is equivalent to calling IEventDispatcher::addServiceListener |
|
| 110 | + * |
|
| 111 | + * @param string $event preferably the fully-qualified class name of the Event sub class to listen for |
|
| 112 | + * @param string $listener fully qualified class name (or ::class notation) of a \OCP\EventDispatcher\IEventListener that can be built by the DI container |
|
| 113 | + * @param int $priority |
|
| 114 | + * |
|
| 115 | + * @see IEventDispatcher::addServiceListener() |
|
| 116 | + * |
|
| 117 | + * @since 20.0.0 |
|
| 118 | + */ |
|
| 119 | + public function registerEventListener(string $event, string $listener, int $priority = 0): void; |
|
| 120 | 120 | |
| 121 | - /** |
|
| 122 | - * @param string $class |
|
| 123 | - * |
|
| 124 | - * @return void |
|
| 125 | - * @see IAppContainer::registerMiddleWare() |
|
| 126 | - * |
|
| 127 | - * @since 20.0.0 |
|
| 128 | - */ |
|
| 129 | - public function registerMiddleware(string $class): void; |
|
| 121 | + /** |
|
| 122 | + * @param string $class |
|
| 123 | + * |
|
| 124 | + * @return void |
|
| 125 | + * @see IAppContainer::registerMiddleWare() |
|
| 126 | + * |
|
| 127 | + * @since 20.0.0 |
|
| 128 | + */ |
|
| 129 | + public function registerMiddleware(string $class): void; |
|
| 130 | 130 | |
| 131 | - /** |
|
| 132 | - * Register a search provider for the unified search |
|
| 133 | - * |
|
| 134 | - * It is allowed to register more than one provider per app as the search |
|
| 135 | - * results can go into distinct sections, e.g. "Files" and "Files shared |
|
| 136 | - * with you" in the Files app. |
|
| 137 | - * |
|
| 138 | - * @param string $class |
|
| 139 | - * |
|
| 140 | - * @return void |
|
| 141 | - * |
|
| 142 | - * @since 20.0.0 |
|
| 143 | - */ |
|
| 144 | - public function registerSearchProvider(string $class): void; |
|
| 131 | + /** |
|
| 132 | + * Register a search provider for the unified search |
|
| 133 | + * |
|
| 134 | + * It is allowed to register more than one provider per app as the search |
|
| 135 | + * results can go into distinct sections, e.g. "Files" and "Files shared |
|
| 136 | + * with you" in the Files app. |
|
| 137 | + * |
|
| 138 | + * @param string $class |
|
| 139 | + * |
|
| 140 | + * @return void |
|
| 141 | + * |
|
| 142 | + * @since 20.0.0 |
|
| 143 | + */ |
|
| 144 | + public function registerSearchProvider(string $class): void; |
|
| 145 | 145 | |
| 146 | - /** |
|
| 147 | - * Register an alternative login option |
|
| 148 | - * |
|
| 149 | - * It is allowed to register more than one option per app. |
|
| 150 | - * |
|
| 151 | - * @param string $class |
|
| 152 | - * |
|
| 153 | - * @return void |
|
| 154 | - * |
|
| 155 | - * @since 20.0.0 |
|
| 156 | - */ |
|
| 157 | - public function registerAlternativeLogin(string $class): void; |
|
| 146 | + /** |
|
| 147 | + * Register an alternative login option |
|
| 148 | + * |
|
| 149 | + * It is allowed to register more than one option per app. |
|
| 150 | + * |
|
| 151 | + * @param string $class |
|
| 152 | + * |
|
| 153 | + * @return void |
|
| 154 | + * |
|
| 155 | + * @since 20.0.0 |
|
| 156 | + */ |
|
| 157 | + public function registerAlternativeLogin(string $class): void; |
|
| 158 | 158 | |
| 159 | - /** |
|
| 160 | - * Register an initialstate provider |
|
| 161 | - * |
|
| 162 | - * It is allowed to register more than one provider per app. |
|
| 163 | - * |
|
| 164 | - * @param string $class |
|
| 165 | - * |
|
| 166 | - * @return void |
|
| 167 | - * |
|
| 168 | - * @since 21.0.0 |
|
| 169 | - */ |
|
| 170 | - public function registerInitialStateProvider(string $class): void; |
|
| 159 | + /** |
|
| 160 | + * Register an initialstate provider |
|
| 161 | + * |
|
| 162 | + * It is allowed to register more than one provider per app. |
|
| 163 | + * |
|
| 164 | + * @param string $class |
|
| 165 | + * |
|
| 166 | + * @return void |
|
| 167 | + * |
|
| 168 | + * @since 21.0.0 |
|
| 169 | + */ |
|
| 170 | + public function registerInitialStateProvider(string $class): void; |
|
| 171 | 171 | } |
@@ -33,35 +33,35 @@ |
||
| 33 | 33 | * @since 16.0.0 |
| 34 | 34 | */ |
| 35 | 35 | interface IInitialStateService { |
| 36 | - /** |
|
| 37 | - * Allows an app to provide its initial state to the template system. |
|
| 38 | - * Use this if you know your initial state sill be used for example if |
|
| 39 | - * you are in the render function of you controller. |
|
| 40 | - * |
|
| 41 | - * @since 16.0.0 |
|
| 42 | - * |
|
| 43 | - * @param string $appName |
|
| 44 | - * @param string $key |
|
| 45 | - * @param bool|int|float|string|array|\JsonSerializable $data |
|
| 46 | - * |
|
| 47 | - * @deprecated 21 Use OCP\AppFramework\Services\IInitialState or OCP\AppFramework\Services\InitialStateProvider |
|
| 48 | - */ |
|
| 49 | - public function provideInitialState(string $appName, string $key, $data): void; |
|
| 36 | + /** |
|
| 37 | + * Allows an app to provide its initial state to the template system. |
|
| 38 | + * Use this if you know your initial state sill be used for example if |
|
| 39 | + * you are in the render function of you controller. |
|
| 40 | + * |
|
| 41 | + * @since 16.0.0 |
|
| 42 | + * |
|
| 43 | + * @param string $appName |
|
| 44 | + * @param string $key |
|
| 45 | + * @param bool|int|float|string|array|\JsonSerializable $data |
|
| 46 | + * |
|
| 47 | + * @deprecated 21 Use OCP\AppFramework\Services\IInitialState or OCP\AppFramework\Services\InitialStateProvider |
|
| 48 | + */ |
|
| 49 | + public function provideInitialState(string $appName, string $key, $data): void; |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * Allows an app to provide its initial state via a lazy method. |
|
| 53 | - * This will call the closure when the template is being generated. |
|
| 54 | - * Use this if your app is injected into pages. Since then the render method |
|
| 55 | - * is not called explicitly. But we do not want to load the state on webdav |
|
| 56 | - * requests for example. |
|
| 57 | - * |
|
| 58 | - * @since 16.0.0 |
|
| 59 | - * |
|
| 60 | - * @param string $appName |
|
| 61 | - * @param string $key |
|
| 62 | - * @param Closure $closure returns a primitive or an object that implements JsonSerializable |
|
| 63 | - * |
|
| 64 | - * @deprecated 21 Use OCP\AppFramework\Services\IInitialState or OCP\AppFramework\Services\InitialStateProvider |
|
| 65 | - */ |
|
| 66 | - public function provideLazyInitialState(string $appName, string $key, Closure $closure): void; |
|
| 51 | + /** |
|
| 52 | + * Allows an app to provide its initial state via a lazy method. |
|
| 53 | + * This will call the closure when the template is being generated. |
|
| 54 | + * Use this if your app is injected into pages. Since then the render method |
|
| 55 | + * is not called explicitly. But we do not want to load the state on webdav |
|
| 56 | + * requests for example. |
|
| 57 | + * |
|
| 58 | + * @since 16.0.0 |
|
| 59 | + * |
|
| 60 | + * @param string $appName |
|
| 61 | + * @param string $key |
|
| 62 | + * @param Closure $closure returns a primitive or an object that implements JsonSerializable |
|
| 63 | + * |
|
| 64 | + * @deprecated 21 Use OCP\AppFramework\Services\IInitialState or OCP\AppFramework\Services\InitialStateProvider |
|
| 65 | + */ |
|
| 66 | + public function provideLazyInitialState(string $appName, string $key, Closure $closure): void; |
|
| 67 | 67 | } |