Passed
Push — master ( 7e7284...62fa85 )
by Roeland
17:42 queued 12s
created

RegistrationContext::getAlternativeLogins()

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\Dashboard\IManager;
38
use OCP\EventDispatcher\IEventDispatcher;
39
use OCP\ILogger;
40
use Throwable;
41
42
class RegistrationContext {
43
44
	/** @var array[] */
45
	private $capabilities = [];
46
47
	/** @var array[] */
48
	private $crashReporters = [];
49
50
	/** @var array[] */
51
	private $dashboardPanels = [];
52
53
	/** @var array[] */
54
	private $services = [];
55
56
	/** @var array[] */
57
	private $aliases = [];
58
59
	/** @var array[] */
60
	private $parameters = [];
61
62
	/** @var array[] */
63
	private $eventListeners = [];
64
65
	/** @var array[] */
66
	private $middlewares = [];
67
68
	/** @var array[] */
69
	private $searchProviders = [];
70
71
	/** @var array[] */
72
	private $alternativeLogins = [];
73
74
	/** @var array[] */
75
	private $initialStates = [];
76
77
	/** @var array[] */
78
	private $wellKnownHandlers = [];
79
80
	/** @var array[] */
81
	private $templateProviders = [];
82
83
	/** @var ILogger */
84
	private $logger;
85
86
	public function __construct(ILogger $logger) {
87
		$this->logger = $logger;
88
	}
89
90
	public function for(string $appId): IRegistrationContext {
91
		return new class($appId, $this) implements IRegistrationContext {
92
			/** @var string */
93
			private $appId;
94
95
			/** @var RegistrationContext */
96
			private $context;
97
98
			public function __construct(string $appId, RegistrationContext $context) {
99
				$this->appId = $appId;
100
				$this->context = $context;
101
			}
102
103
			public function registerCapability(string $capability): void {
104
				$this->context->registerCapability(
105
					$this->appId,
106
					$capability
107
				);
108
			}
109
110
			public function registerCrashReporter(string $reporterClass): void {
111
				$this->context->registerCrashReporter(
112
					$this->appId,
113
					$reporterClass
114
				);
115
			}
116
117
			public function registerDashboardWidget(string $widgetClass): void {
118
				$this->context->registerDashboardPanel(
119
					$this->appId,
120
					$widgetClass
121
				);
122
			}
123
124
			public function registerService(string $name, callable $factory, bool $shared = true): void {
125
				$this->context->registerService(
126
					$this->appId,
127
					$name,
128
					$factory,
129
					$shared
130
				);
131
			}
132
133
			public function registerServiceAlias(string $alias, string $target): void {
134
				$this->context->registerServiceAlias(
135
					$this->appId,
136
					$alias,
137
					$target
138
				);
139
			}
140
141
			public function registerParameter(string $name, $value): void {
142
				$this->context->registerParameter(
143
					$this->appId,
144
					$name,
145
					$value
146
				);
147
			}
148
149
			public function registerEventListener(string $event, string $listener, int $priority = 0): void {
150
				$this->context->registerEventListener(
151
					$this->appId,
152
					$event,
153
					$listener,
154
					$priority
155
				);
156
			}
157
158
			public function registerMiddleware(string $class): void {
159
				$this->context->registerMiddleware(
160
					$this->appId,
161
					$class
162
				);
163
			}
164
165
			public function registerSearchProvider(string $class): void {
166
				$this->context->registerSearchProvider(
167
					$this->appId,
168
					$class
169
				);
170
			}
171
172
			public function registerAlternativeLogin(string $class): void {
173
				$this->context->registerAlternativeLogin(
174
					$this->appId,
175
					$class
176
				);
177
			}
178
179
			public function registerInitialStateProvider(string $class): void {
180
				$this->context->registerInitialState(
181
					$this->appId,
182
					$class
183
				);
184
			}
185
186
			public function registerWellKnownHandler(string $class): void {
187
				$this->context->registerWellKnown(
188
					$this->appId,
189
					$class
190
				);
191
			}
192
193
			public function registerTemplateProvider(string $providerClass): void {
194
				$this->context->registerTemplateProvider(
195
					$this->appId,
196
					$providerClass
197
				);
198
			}
199
		};
200
	}
201
202
	public function registerCapability(string $appId, string $capability): void {
203
		$this->capabilities[] = [
204
			'appId' => $appId,
205
			'capability' => $capability
206
		];
207
	}
208
209
	public function registerCrashReporter(string $appId, string $reporterClass): void {
210
		$this->crashReporters[] = [
211
			'appId' => $appId,
212
			'class' => $reporterClass,
213
		];
214
	}
215
216
	public function registerDashboardPanel(string $appId, string $panelClass): void {
217
		$this->dashboardPanels[] = [
218
			'appId' => $appId,
219
			'class' => $panelClass
220
		];
221
	}
222
223
	public function registerService(string $appId, string $name, callable $factory, bool $shared = true): void {
224
		$this->services[] = [
225
			"appId" => $appId,
226
			"name" => $name,
227
			"factory" => $factory,
228
			"shared" => $shared,
229
		];
230
	}
231
232
	public function registerServiceAlias(string $appId, string $alias, string $target): void {
233
		$this->aliases[] = [
234
			"appId" => $appId,
235
			"alias" => $alias,
236
			"target" => $target,
237
		];
238
	}
239
240
	public function registerParameter(string $appId, string $name, $value): void {
241
		$this->parameters[] = [
242
			"appId" => $appId,
243
			"name" => $name,
244
			"value" => $value,
245
		];
246
	}
247
248
	public function registerEventListener(string $appId, string $event, string $listener, int $priority = 0): void {
249
		$this->eventListeners[] = [
250
			"appId" => $appId,
251
			"event" => $event,
252
			"listener" => $listener,
253
			"priority" => $priority,
254
		];
255
	}
256
257
	public function registerMiddleware(string $appId, string $class): void {
258
		$this->middlewares[] = [
259
			"appId" => $appId,
260
			"class" => $class,
261
		];
262
	}
263
264
	public function registerSearchProvider(string $appId, string $class) {
265
		$this->searchProviders[] = [
266
			'appId' => $appId,
267
			'class' => $class,
268
		];
269
	}
270
271
	public function registerAlternativeLogin(string $appId, string $class): void {
272
		$this->alternativeLogins[] = [
273
			'appId' => $appId,
274
			'class' => $class,
275
		];
276
	}
277
278
	public function registerInitialState(string $appId, string $class): void {
279
		$this->initialStates[] = [
280
			'appId' => $appId,
281
			'class' => $class,
282
		];
283
	}
284
285
	public function registerWellKnown(string $appId, string $class): void {
286
		$this->wellKnownHandlers[] = [
287
			'appId' => $appId,
288
			'class' => $class,
289
		];
290
	}
291
292
	public function registerTemplateProvider(string $appId, string $class): void {
293
		$this->templateProviders[] = [
294
			'appId' => $appId,
295
			'class' => $class,
296
		];
297
	}
298
299
	/**
300
	 * @param App[] $apps
301
	 */
302
	public function delegateCapabilityRegistrations(array $apps): void {
303
		while (($registration = array_shift($this->capabilities)) !== null) {
304
			try {
305
				$apps[$registration['appId']]
306
					->getContainer()
307
					->registerCapability($registration['capability']);
308
			} catch (Throwable $e) {
309
				$appId = $registration['appId'];
310
				$this->logger->logException($e, [
311
					'message' => "Error during capability registration of $appId: " . $e->getMessage(),
312
					'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

312
					'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...
313
				]);
314
			}
315
		}
316
	}
317
318
	/**
319
	 * @param App[] $apps
320
	 */
321
	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

321
	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...
322
		while (($registration = array_shift($this->crashReporters)) !== null) {
323
			try {
324
				$registry->registerLazy($registration['class']);
325
			} catch (Throwable $e) {
326
				$appId = $registration['appId'];
327
				$this->logger->logException($e, [
328
					'message' => "Error during crash reporter registration of $appId: " . $e->getMessage(),
329
					'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

329
					'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...
330
				]);
331
			}
332
		}
333
	}
334
335
	/**
336
	 * @param App[] $apps
337
	 */
338
	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

338
	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...
339
		while (($panel = array_shift($this->dashboardPanels)) !== null) {
340
			try {
341
				$dashboardManager->lazyRegisterWidget($panel['class']);
342
			} catch (Throwable $e) {
343
				$appId = $panel['appId'];
344
				$this->logger->logException($e, [
345
					'message' => "Error during dashboard registration of $appId: " . $e->getMessage(),
346
					'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

346
					'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...
347
				]);
348
			}
349
		}
350
	}
351
352
	public function delegateEventListenerRegistrations(IEventDispatcher $eventDispatcher): void {
353
		while (($registration = array_shift($this->eventListeners)) !== null) {
354
			try {
355
				if (isset($registration['priority'])) {
356
					$eventDispatcher->addServiceListener(
357
						$registration['event'],
358
						$registration['listener'],
359
						$registration['priority']
360
					);
361
				} else {
362
					$eventDispatcher->addServiceListener(
363
						$registration['event'],
364
						$registration['listener']
365
					);
366
				}
367
			} catch (Throwable $e) {
368
				$appId = $registration['appId'];
369
				$this->logger->logException($e, [
370
					'message' => "Error during event listener registration of $appId: " . $e->getMessage(),
371
					'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

371
					'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...
372
				]);
373
			}
374
		}
375
	}
376
377
	/**
378
	 * @param App[] $apps
379
	 */
380
	public function delegateContainerRegistrations(array $apps): void {
381
		while (($registration = array_shift($this->services)) !== null) {
382
			try {
383
				/**
384
				 * Register the service and convert the callable into a \Closure if necessary
385
				 */
386
				$apps[$registration['appId']]
387
					->getContainer()
388
					->registerService(
389
						$registration['name'],
390
						Closure::fromCallable($registration['factory']),
391
						$registration['shared'] ?? true
392
					);
393
			} catch (Throwable $e) {
394
				$appId = $registration['appId'];
395
				$this->logger->logException($e, [
396
					'message' => "Error during service registration of $appId: " . $e->getMessage(),
397
					'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

397
					'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...
398
				]);
399
			}
400
		}
401
402
		foreach ($this->aliases as $registration) {
403
			try {
404
				$apps[$registration['appId']]
405
					->getContainer()
406
					->registerAlias(
407
						$registration['alias'],
408
						$registration['target']
409
					);
410
			} catch (Throwable $e) {
411
				$appId = $registration['appId'];
412
				$this->logger->logException($e, [
413
					'message' => "Error during service alias registration of $appId: " . $e->getMessage(),
414
					'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

414
					'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...
415
				]);
416
			}
417
		}
418
419
		foreach ($this->parameters as $registration) {
420
			try {
421
				$apps[$registration['appId']]
422
					->getContainer()
423
					->registerParameter(
424
						$registration['name'],
425
						$registration['value']
426
					);
427
			} catch (Throwable $e) {
428
				$appId = $registration['appId'];
429
				$this->logger->logException($e, [
430
					'message' => "Error during service alias registration of $appId: " . $e->getMessage(),
431
					'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

431
					'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...
432
				]);
433
			}
434
		}
435
	}
436
437
	/**
438
	 * @param App[] $apps
439
	 */
440
	public function delegateMiddlewareRegistrations(array $apps): void {
441
		while (($middleware = array_shift($this->middlewares)) !== null) {
442
			try {
443
				$apps[$middleware['appId']]
444
					->getContainer()
445
					->registerMiddleWare($middleware['class']);
446
			} catch (Throwable $e) {
447
				$appId = $middleware['appId'];
448
				$this->logger->logException($e, [
449
					'message' => "Error during capability registration of $appId: " . $e->getMessage(),
450
					'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

450
					'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...
451
				]);
452
			}
453
		}
454
	}
455
456
	/**
457
	 * @return array[]
458
	 */
459
	public function getSearchProviders(): array {
460
		return $this->searchProviders;
461
	}
462
463
	/**
464
	 * @return array[]
465
	 */
466
	public function getAlternativeLogins(): array {
467
		return $this->alternativeLogins;
468
	}
469
470
	/**
471
	 * @return array[]
472
	 */
473
	public function getInitialStates(): array {
474
		return $this->initialStates;
475
	}
476
477
	/**
478
	 * @return array[]
479
	 */
480
	public function getWellKnownHandlers(): array {
481
		return $this->wellKnownHandlers;
482
	}
483
484
	public function getTemplateProviders(): array {
485
		return $this->templateProviders;
486
	}
487
}
488