Passed
Push — master ( 403388...874eff )
by Roeland
17:00 queued 11s
created

RegistrationContext::getWellKnownHandlers()

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
nc 1
nop 0
dl 0
loc 2
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright 2020 Christoph Wurst <[email protected]>
7
 *
8
 * @author Christoph Wurst <[email protected]>
9
 * @author Joas Schilling <[email protected]>
10
 * @author Julius Härtl <[email protected]>
11
 * @author Robin Windey <[email protected]>
12
 * @author Roeland Jago Douma <[email protected]>
13
 *
14
 * @license GNU AGPL version 3 or any later version
15
 *
16
 * This program is free software: you can redistribute it and/or modify
17
 * it under the terms of the GNU Affero General Public License as
18
 * published by the Free Software Foundation, either version 3 of the
19
 * License, or (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU Affero General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU Affero General Public License
27
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28
 *
29
 */
30
31
namespace OC\AppFramework\Bootstrap;
32
33
use Closure;
34
use OC\Support\CrashReport\Registry;
35
use OCP\AppFramework\App;
36
use OCP\AppFramework\Bootstrap\IRegistrationContext;
37
use OCP\AppFramework\Middleware;
38
use OCP\AppFramework\Services\InitialStateProvider;
39
use OCP\Authentication\IAlternativeLogin;
40
use OCP\Capabilities\ICapability;
41
use OCP\Dashboard\IManager;
42
use OCP\Dashboard\IWidget;
43
use OCP\EventDispatcher\IEventDispatcher;
44
use OCP\Files\Template\ICustomTemplateProvider;
45
use OCP\Http\WellKnown\IHandler;
46
use OCP\ILogger;
47
use OCP\Notification\INotifier;
48
use OCP\Search\IProvider;
49
use OCP\Support\CrashReport\IReporter;
50
use Throwable;
51
use function array_shift;
52
53
class RegistrationContext {
54
55
	/** @var ServiceRegistration<ICapability>[] */
56
	private $capabilities = [];
57
58
	/** @var ServiceRegistration<IReporter>[] */
59
	private $crashReporters = [];
60
61
	/** @var ServiceRegistration<IWidget>[] */
62
	private $dashboardPanels = [];
63
64
	/** @var ServiceFactoryRegistration[] */
65
	private $services = [];
66
67
	/** @var ServiceAliasRegistration[] */
68
	private $aliases = [];
69
70
	/** @var ParameterRegistration[] */
71
	private $parameters = [];
72
73
	/** @var EventListenerRegistration[] */
74
	private $eventListeners = [];
75
76
	/** @var ServiceRegistration<Middleware>[] */
77
	private $middlewares = [];
78
79
	/** @var ServiceRegistration<IProvider>[] */
80
	private $searchProviders = [];
81
82
	/** @var ServiceRegistration<IAlternativeLogin>[] */
83
	private $alternativeLogins = [];
84
85
	/** @var ServiceRegistration<InitialStateProvider>[] */
86
	private $initialStates = [];
87
88
	/** @var ServiceRegistration<IHandler>[] */
89
	private $wellKnownHandlers = [];
90
91
	/** @var ServiceRegistration<ICustomTemplateProvider>[] */
92
	private $templateProviders = [];
93
94
	/** @var ServiceRegistration<INotifier>[] */
95
	private $notifierServices;
96
97
	/** @var ILogger */
98
	private $logger;
99
100
	public function __construct(ILogger $logger) {
101
		$this->logger = $logger;
102
	}
103
104
	public function for(string $appId): IRegistrationContext {
105
		return new class($appId, $this) implements IRegistrationContext {
106
			/** @var string */
107
			private $appId;
108
109
			/** @var RegistrationContext */
110
			private $context;
111
112
			public function __construct(string $appId, RegistrationContext $context) {
113
				$this->appId = $appId;
114
				$this->context = $context;
115
			}
116
117
			public function registerCapability(string $capability): void {
118
				$this->context->registerCapability(
119
					$this->appId,
120
					$capability
121
				);
122
			}
123
124
			public function registerCrashReporter(string $reporterClass): void {
125
				$this->context->registerCrashReporter(
126
					$this->appId,
127
					$reporterClass
128
				);
129
			}
130
131
			public function registerDashboardWidget(string $widgetClass): void {
132
				$this->context->registerDashboardPanel(
133
					$this->appId,
134
					$widgetClass
135
				);
136
			}
137
138
			public function registerService(string $name, callable $factory, bool $shared = true): void {
139
				$this->context->registerService(
140
					$this->appId,
141
					$name,
142
					$factory,
143
					$shared
144
				);
145
			}
146
147
			public function registerServiceAlias(string $alias, string $target): void {
148
				$this->context->registerServiceAlias(
149
					$this->appId,
150
					$alias,
151
					$target
152
				);
153
			}
154
155
			public function registerParameter(string $name, $value): void {
156
				$this->context->registerParameter(
157
					$this->appId,
158
					$name,
159
					$value
160
				);
161
			}
162
163
			public function registerEventListener(string $event, string $listener, int $priority = 0): void {
164
				$this->context->registerEventListener(
165
					$this->appId,
166
					$event,
167
					$listener,
168
					$priority
169
				);
170
			}
171
172
			public function registerMiddleware(string $class): void {
173
				$this->context->registerMiddleware(
174
					$this->appId,
175
					$class
176
				);
177
			}
178
179
			public function registerSearchProvider(string $class): void {
180
				$this->context->registerSearchProvider(
181
					$this->appId,
182
					$class
183
				);
184
			}
185
186
			public function registerAlternativeLogin(string $class): void {
187
				$this->context->registerAlternativeLogin(
188
					$this->appId,
189
					$class
190
				);
191
			}
192
193
			public function registerInitialStateProvider(string $class): void {
194
				$this->context->registerInitialState(
195
					$this->appId,
196
					$class
197
				);
198
			}
199
200
			public function registerWellKnownHandler(string $class): void {
201
				$this->context->registerWellKnown(
202
					$this->appId,
203
					$class
204
				);
205
			}
206
207
			public function registerTemplateProvider(string $providerClass): void {
208
				$this->context->registerTemplateProvider(
209
					$this->appId,
210
					$providerClass
211
				);
212
			}
213
214
			public function registerNotifierService(string $notifierClass): void {
215
				$this->context->registerNotifierService(
216
					$this->appId,
217
					$notifierClass
218
				);
219
			}
220
		};
221
	}
222
223
	/**
224
	 * @psalm-param class-string<ICapability> $capability
225
	 */
226
	public function registerCapability(string $appId, string $capability): void {
227
		$this->capabilities[] = new ServiceRegistration($appId, $capability);
228
	}
229
230
	/**
231
	 * @psalm-param class-string<IReporter> $capability
232
	 */
233
	public function registerCrashReporter(string $appId, string $reporterClass): void {
234
		$this->crashReporters[] = new ServiceRegistration($appId, $reporterClass);
235
	}
236
237
	/**
238
	 * @psalm-param class-string<IWidget> $capability
239
	 */
240
	public function registerDashboardPanel(string $appId, string $panelClass): void {
241
		$this->dashboardPanels[] = new ServiceRegistration($appId, $panelClass);
242
	}
243
244
	public function registerService(string $appId, string $name, callable $factory, bool $shared = true): void {
245
		$this->services[] = new ServiceFactoryRegistration($appId, $name, $factory, $shared);
246
	}
247
248
	public function registerServiceAlias(string $appId, string $alias, string $target): void {
249
		$this->aliases[] = new ServiceAliasRegistration($appId, $alias, $target);
250
	}
251
252
	public function registerParameter(string $appId, string $name, $value): void {
253
		$this->parameters[] = new ParameterRegistration($appId, $name, $value);
254
	}
255
256
	public function registerEventListener(string $appId, string $event, string $listener, int $priority = 0): void {
257
		$this->eventListeners[] = new EventListenerRegistration($appId, $event, $listener, $priority);
258
	}
259
260
	/**
261
	 * @psalm-param class-string<Middleware> $class
262
	 */
263
	public function registerMiddleware(string $appId, string $class): void {
264
		$this->middlewares[] = new ServiceRegistration($appId, $class);
265
	}
266
267
	public function registerSearchProvider(string $appId, string $class) {
268
		$this->searchProviders[] = new ServiceRegistration($appId, $class);
269
	}
270
271
	public function registerAlternativeLogin(string $appId, string $class): void {
272
		$this->alternativeLogins[] = new ServiceRegistration($appId, $class);
273
	}
274
275
	public function registerInitialState(string $appId, string $class): void {
276
		$this->initialStates[] = new ServiceRegistration($appId, $class);
277
	}
278
279
	public function registerWellKnown(string $appId, string $class): void {
280
		$this->wellKnownHandlers[] = new ServiceRegistration($appId, $class);
281
	}
282
283
	public function registerTemplateProvider(string $appId, string $class): void {
284
		$this->templateProviders[] = new ServiceRegistration($appId, $class);
285
	}
286
287
	public function registerNotifierService(string $appId, string $class): void {
288
		$this->notifierServices[] = new ServiceRegistration($appId, $class);
289
	}
290
291
	/**
292
	 * @param App[] $apps
293
	 */
294
	public function delegateCapabilityRegistrations(array $apps): void {
295
		while (($registration = array_shift($this->capabilities)) !== null) {
0 ignored issues
show
Bug introduced by
$this->capabilities of type OC\AppFramework\Bootstrap\ServiceRegistration is incompatible with the type array expected by parameter $array of array_shift(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

295
		while (($registration = array_shift(/** @scrutinizer ignore-type */ $this->capabilities)) !== null) {
Loading history...
296
			try {
297
				$apps[$registration->getAppId()]
298
					->getContainer()
299
					->registerCapability($registration->getService());
300
			} catch (Throwable $e) {
301
				$appId = $registration->getAppId();
302
				$this->logger->logException($e, [
303
					'message' => "Error during capability registration of $appId: " . $e->getMessage(),
304
					'level' => ILogger::ERROR,
0 ignored issues
show
Deprecated Code introduced by
The constant OCP\ILogger::ERROR has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

304
					'level' => /** @scrutinizer ignore-deprecated */ ILogger::ERROR,

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
305
				]);
306
			}
307
		}
308
	}
309
310
	/**
311
	 * @param App[] $apps
312
	 */
313
	public function delegateCrashReporterRegistrations(array $apps, Registry $registry): void {
0 ignored issues
show
Unused Code introduced by
The parameter $apps is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

313
	public function delegateCrashReporterRegistrations(/** @scrutinizer ignore-unused */ array $apps, Registry $registry): void {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
314
		while (($registration = array_shift($this->crashReporters)) !== null) {
0 ignored issues
show
Bug introduced by
$this->crashReporters of type OC\AppFramework\Bootstrap\ServiceRegistration is incompatible with the type array expected by parameter $array of array_shift(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

314
		while (($registration = array_shift(/** @scrutinizer ignore-type */ $this->crashReporters)) !== null) {
Loading history...
315
			try {
316
				$registry->registerLazy($registration->getService());
317
			} catch (Throwable $e) {
318
				$appId = $registration->getAppId();
319
				$this->logger->logException($e, [
320
					'message' => "Error during crash reporter registration of $appId: " . $e->getMessage(),
321
					'level' => ILogger::ERROR,
0 ignored issues
show
Deprecated Code introduced by
The constant OCP\ILogger::ERROR has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

321
					'level' => /** @scrutinizer ignore-deprecated */ ILogger::ERROR,

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
322
				]);
323
			}
324
		}
325
	}
326
327
	/**
328
	 * @param App[] $apps
329
	 */
330
	public function delegateDashboardPanelRegistrations(array $apps, IManager $dashboardManager): void {
0 ignored issues
show
Unused Code introduced by
The parameter $apps is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

330
	public function delegateDashboardPanelRegistrations(/** @scrutinizer ignore-unused */ array $apps, IManager $dashboardManager): void {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
331
		while (($panel = array_shift($this->dashboardPanels)) !== null) {
0 ignored issues
show
Bug introduced by
$this->dashboardPanels of type OC\AppFramework\Bootstrap\ServiceRegistration is incompatible with the type array expected by parameter $array of array_shift(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

331
		while (($panel = array_shift(/** @scrutinizer ignore-type */ $this->dashboardPanels)) !== null) {
Loading history...
332
			try {
333
				$dashboardManager->lazyRegisterWidget($panel->getService());
334
			} catch (Throwable $e) {
335
				$appId = $panel->getAppId();
336
				$this->logger->logException($e, [
337
					'message' => "Error during dashboard registration of $appId: " . $e->getMessage(),
338
					'level' => ILogger::ERROR,
0 ignored issues
show
Deprecated Code introduced by
The constant OCP\ILogger::ERROR has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

338
					'level' => /** @scrutinizer ignore-deprecated */ ILogger::ERROR,

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
339
				]);
340
			}
341
		}
342
	}
343
344
	public function delegateEventListenerRegistrations(IEventDispatcher $eventDispatcher): void {
345
		while (($registration = array_shift($this->eventListeners)) !== null) {
346
			try {
347
				$eventDispatcher->addServiceListener(
348
					$registration->getEvent(),
349
					$registration->getService(),
350
					$registration->getPriority()
351
				);
352
			} catch (Throwable $e) {
353
				$appId = $registration->getAppId();
354
				$this->logger->logException($e, [
355
					'message' => "Error during event listener registration of $appId: " . $e->getMessage(),
356
					'level' => ILogger::ERROR,
0 ignored issues
show
Deprecated Code introduced by
The constant OCP\ILogger::ERROR has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

356
					'level' => /** @scrutinizer ignore-deprecated */ ILogger::ERROR,

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
357
				]);
358
			}
359
		}
360
	}
361
362
	/**
363
	 * @param App[] $apps
364
	 */
365
	public function delegateContainerRegistrations(array $apps): void {
366
		while (($registration = array_shift($this->services)) !== null) {
367
			try {
368
				/**
369
				 * Register the service and convert the callable into a \Closure if necessary
370
				 */
371
				$apps[$registration->getAppId()]
372
					->getContainer()
373
					->registerService(
374
						$registration->getName(),
375
						Closure::fromCallable($registration->getFactory()),
376
						$registration->isShared()
377
					);
378
			} catch (Throwable $e) {
379
				$appId = $registration->getAppId();
380
				$this->logger->logException($e, [
381
					'message' => "Error during service registration of $appId: " . $e->getMessage(),
382
					'level' => ILogger::ERROR,
0 ignored issues
show
Deprecated Code introduced by
The constant OCP\ILogger::ERROR has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

382
					'level' => /** @scrutinizer ignore-deprecated */ ILogger::ERROR,

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
383
				]);
384
			}
385
		}
386
387
		while (($registration = array_shift($this->aliases)) !== null) {
388
			try {
389
				$apps[$registration->getAppId()]
390
					->getContainer()
391
					->registerAlias(
392
						$registration->getAlias(),
393
						$registration->getTarget()
394
					);
395
			} catch (Throwable $e) {
396
				$appId = $registration->getAppId();
397
				$this->logger->logException($e, [
398
					'message' => "Error during service alias registration of $appId: " . $e->getMessage(),
399
					'level' => ILogger::ERROR,
0 ignored issues
show
Deprecated Code introduced by
The constant OCP\ILogger::ERROR has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

399
					'level' => /** @scrutinizer ignore-deprecated */ ILogger::ERROR,

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
400
				]);
401
			}
402
		}
403
404
		while (($registration = array_shift($this->parameters)) !== null) {
405
			try {
406
				$apps[$registration->getAppId()]
407
					->getContainer()
408
					->registerParameter(
409
						$registration->getName(),
410
						$registration->getValue()
411
					);
412
			} catch (Throwable $e) {
413
				$appId = $registration->getAppId();
414
				$this->logger->logException($e, [
415
					'message' => "Error during service alias registration of $appId: " . $e->getMessage(),
416
					'level' => ILogger::ERROR,
0 ignored issues
show
Deprecated Code introduced by
The constant OCP\ILogger::ERROR has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

416
					'level' => /** @scrutinizer ignore-deprecated */ ILogger::ERROR,

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
417
				]);
418
			}
419
		}
420
	}
421
422
	/**
423
	 * @param App[] $apps
424
	 */
425
	public function delegateMiddlewareRegistrations(array $apps): void {
426
		while (($middleware = array_shift($this->middlewares)) !== null) {
0 ignored issues
show
Bug introduced by
$this->middlewares of type OC\AppFramework\Bootstrap\ServiceRegistration is incompatible with the type array expected by parameter $array of array_shift(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

426
		while (($middleware = array_shift(/** @scrutinizer ignore-type */ $this->middlewares)) !== null) {
Loading history...
427
			try {
428
				$apps[$middleware->getAppId()]
429
					->getContainer()
430
					->registerMiddleWare($middleware->getService());
431
			} catch (Throwable $e) {
432
				$appId = $middleware->getAppId();
433
				$this->logger->logException($e, [
434
					'message' => "Error during capability registration of $appId: " . $e->getMessage(),
435
					'level' => ILogger::ERROR,
0 ignored issues
show
Deprecated Code introduced by
The constant OCP\ILogger::ERROR has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

435
					'level' => /** @scrutinizer ignore-deprecated */ ILogger::ERROR,

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
436
				]);
437
			}
438
		}
439
	}
440
441
	/**
442
	 * @return ServiceRegistration<IProvider>[]
443
	 */
444
	public function getSearchProviders(): array {
445
		return $this->searchProviders;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->searchProviders returns the type OC\AppFramework\Bootstrap\ServiceRegistration which is incompatible with the type-hinted return array.
Loading history...
446
	}
447
448
	/**
449
	 * @return ServiceRegistration<IAlternativeLogin>[]
450
	 */
451
	public function getAlternativeLogins(): array {
452
		return $this->alternativeLogins;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->alternativeLogins returns the type OC\AppFramework\Bootstrap\ServiceRegistration which is incompatible with the type-hinted return array.
Loading history...
453
	}
454
455
	/**
456
	 * @return ServiceRegistration<InitialStateProvider>[]
457
	 */
458
	public function getInitialStates(): array {
459
		return $this->initialStates;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->initialStates returns the type OC\AppFramework\Bootstrap\ServiceRegistration which is incompatible with the type-hinted return array.
Loading history...
460
	}
461
462
	/**
463
	 * @return ServiceRegistration<IHandler>[]
464
	 */
465
	public function getWellKnownHandlers(): array {
466
		return $this->wellKnownHandlers;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->wellKnownHandlers returns the type OC\AppFramework\Bootstrap\ServiceRegistration which is incompatible with the type-hinted return array.
Loading history...
467
	}
468
469
	/**
470
	 * @return ServiceRegistration<ICustomTemplateProvider>[]
471
	 */
472
	public function getTemplateProviders(): array {
473
		return $this->templateProviders;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->templateProviders returns the type OC\AppFramework\Bootstrap\ServiceRegistration which is incompatible with the type-hinted return array.
Loading history...
474
	}
475
476
	/**
477
	 * @return ServiceRegistration<INotifier>[]
478
	 */
479
	public function getNotifierServices(): array {
480
		return $this->notifierServices;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->notifierServices returns the type OC\AppFramework\Bootstrap\ServiceRegistration which is incompatible with the type-hinted return array.
Loading history...
481
	}
482
}
483