Passed
Push — master ( fbf25e...f37e15 )
by Christoph
18:24 queued 11s
created

delegateDashboardPanelRegistrations()

Size

Total Lines 9
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
nc 3
nop 2
dl 0
loc 9
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 ILogger */
81
	private $logger;
82
83
	public function __construct(ILogger $logger) {
84
		$this->logger = $logger;
85
	}
86
87
	public function for(string $appId): IRegistrationContext {
88
		return new class($appId, $this) implements IRegistrationContext {
89
			/** @var string */
90
			private $appId;
91
92
			/** @var RegistrationContext */
93
			private $context;
94
95
			public function __construct(string $appId, RegistrationContext $context) {
96
				$this->appId = $appId;
97
				$this->context = $context;
98
			}
99
100
			public function registerCapability(string $capability): void {
101
				$this->context->registerCapability(
102
					$this->appId,
103
					$capability
104
				);
105
			}
106
107
			public function registerCrashReporter(string $reporterClass): void {
108
				$this->context->registerCrashReporter(
109
					$this->appId,
110
					$reporterClass
111
				);
112
			}
113
114
			public function registerDashboardWidget(string $widgetClass): void {
115
				$this->context->registerDashboardPanel(
116
					$this->appId,
117
					$widgetClass
118
				);
119
			}
120
121
			public function registerService(string $name, callable $factory, bool $shared = true): void {
122
				$this->context->registerService(
123
					$this->appId,
124
					$name,
125
					$factory,
126
					$shared
127
				);
128
			}
129
130
			public function registerServiceAlias(string $alias, string $target): void {
131
				$this->context->registerServiceAlias(
132
					$this->appId,
133
					$alias,
134
					$target
135
				);
136
			}
137
138
			public function registerParameter(string $name, $value): void {
139
				$this->context->registerParameter(
140
					$this->appId,
141
					$name,
142
					$value
143
				);
144
			}
145
146
			public function registerEventListener(string $event, string $listener, int $priority = 0): void {
147
				$this->context->registerEventListener(
148
					$this->appId,
149
					$event,
150
					$listener,
151
					$priority
152
				);
153
			}
154
155
			public function registerMiddleware(string $class): void {
156
				$this->context->registerMiddleware(
157
					$this->appId,
158
					$class
159
				);
160
			}
161
162
			public function registerSearchProvider(string $class): void {
163
				$this->context->registerSearchProvider(
164
					$this->appId,
165
					$class
166
				);
167
			}
168
169
			public function registerAlternativeLogin(string $class): void {
170
				$this->context->registerAlternativeLogin(
171
					$this->appId,
172
					$class
173
				);
174
			}
175
176
			public function registerInitialStateProvider(string $class): void {
177
				$this->context->registerInitialState(
178
					$this->appId,
179
					$class
180
				);
181
			}
182
183
			public function registerWellKnownHandler(string $class): void {
184
				$this->context->registerWellKnown(
185
					$this->appId,
186
					$class
187
				);
188
			}
189
		};
190
	}
191
192
	public function registerCapability(string $appId, string $capability): void {
193
		$this->capabilities[] = [
194
			'appId' => $appId,
195
			'capability' => $capability
196
		];
197
	}
198
199
	public function registerCrashReporter(string $appId, string $reporterClass): void {
200
		$this->crashReporters[] = [
201
			'appId' => $appId,
202
			'class' => $reporterClass,
203
		];
204
	}
205
206
	public function registerDashboardPanel(string $appId, string $panelClass): void {
207
		$this->dashboardPanels[] = [
208
			'appId' => $appId,
209
			'class' => $panelClass
210
		];
211
	}
212
213
	public function registerService(string $appId, string $name, callable $factory, bool $shared = true): void {
214
		$this->services[] = [
215
			"appId" => $appId,
216
			"name" => $name,
217
			"factory" => $factory,
218
			"shared" => $shared,
219
		];
220
	}
221
222
	public function registerServiceAlias(string $appId, string $alias, string $target): void {
223
		$this->aliases[] = [
224
			"appId" => $appId,
225
			"alias" => $alias,
226
			"target" => $target,
227
		];
228
	}
229
230
	public function registerParameter(string $appId, string $name, $value): void {
231
		$this->parameters[] = [
232
			"appId" => $appId,
233
			"name" => $name,
234
			"value" => $value,
235
		];
236
	}
237
238
	public function registerEventListener(string $appId, string $event, string $listener, int $priority = 0): void {
239
		$this->eventListeners[] = [
240
			"appId" => $appId,
241
			"event" => $event,
242
			"listener" => $listener,
243
			"priority" => $priority,
244
		];
245
	}
246
247
	public function registerMiddleware(string $appId, string $class): void {
248
		$this->middlewares[] = [
249
			"appId" => $appId,
250
			"class" => $class,
251
		];
252
	}
253
254
	public function registerSearchProvider(string $appId, string $class) {
255
		$this->searchProviders[] = [
256
			'appId' => $appId,
257
			'class' => $class,
258
		];
259
	}
260
261
	public function registerAlternativeLogin(string $appId, string $class): void {
262
		$this->alternativeLogins[] = [
263
			'appId' => $appId,
264
			'class' => $class,
265
		];
266
	}
267
268
	public function registerInitialState(string $appId, string $class): void {
269
		$this->initialStates[] = [
270
			'appId' => $appId,
271
			'class' => $class,
272
		];
273
	}
274
275
	public function registerWellKnown(string $appId, string $class): void {
276
		$this->wellKnownHandlers[] = [
277
			'appId' => $appId,
278
			'class' => $class,
279
		];
280
	}
281
282
	/**
283
	 * @param App[] $apps
284
	 */
285
	public function delegateCapabilityRegistrations(array $apps): void {
286
		while (($registration = array_shift($this->capabilities)) !== null) {
287
			try {
288
				$apps[$registration['appId']]
289
					->getContainer()
290
					->registerCapability($registration['capability']);
291
			} catch (Throwable $e) {
292
				$appId = $registration['appId'];
293
				$this->logger->logException($e, [
294
					'message' => "Error during capability registration of $appId: " . $e->getMessage(),
295
					'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

295
					'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...
296
				]);
297
			}
298
		}
299
	}
300
301
	/**
302
	 * @param App[] $apps
303
	 */
304
	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

304
	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...
305
		while (($registration = array_shift($this->crashReporters)) !== null) {
306
			try {
307
				$registry->registerLazy($registration['class']);
308
			} catch (Throwable $e) {
309
				$appId = $registration['appId'];
310
				$this->logger->logException($e, [
311
					'message' => "Error during crash reporter 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 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

321
	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...
322
		while (($panel = array_shift($this->dashboardPanels)) !== null) {
323
			try {
324
				$dashboardManager->lazyRegisterWidget($panel['class']);
325
			} catch (Throwable $e) {
326
				$appId = $panel['appId'];
327
				$this->logger->logException($e, [
328
					'message' => "Error during dashboard 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
	public function delegateEventListenerRegistrations(IEventDispatcher $eventDispatcher): void {
336
		while (($registration = array_shift($this->eventListeners)) !== null) {
337
			try {
338
				if (isset($registration['priority'])) {
339
					$eventDispatcher->addServiceListener(
340
						$registration['event'],
341
						$registration['listener'],
342
						$registration['priority']
343
					);
344
				} else {
345
					$eventDispatcher->addServiceListener(
346
						$registration['event'],
347
						$registration['listener']
348
					);
349
				}
350
			} catch (Throwable $e) {
351
				$appId = $registration['appId'];
352
				$this->logger->logException($e, [
353
					'message' => "Error during event listener registration of $appId: " . $e->getMessage(),
354
					'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

354
					'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...
355
				]);
356
			}
357
		}
358
	}
359
360
	/**
361
	 * @param App[] $apps
362
	 */
363
	public function delegateContainerRegistrations(array $apps): void {
364
		while (($registration = array_shift($this->services)) !== null) {
365
			try {
366
				/**
367
				 * Register the service and convert the callable into a \Closure if necessary
368
				 */
369
				$apps[$registration['appId']]
370
					->getContainer()
371
					->registerService(
372
						$registration['name'],
373
						Closure::fromCallable($registration['factory']),
374
						$registration['shared'] ?? true
375
					);
376
			} catch (Throwable $e) {
377
				$appId = $registration['appId'];
378
				$this->logger->logException($e, [
379
					'message' => "Error during service registration of $appId: " . $e->getMessage(),
380
					'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

380
					'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...
381
				]);
382
			}
383
		}
384
385
		foreach ($this->aliases as $registration) {
386
			try {
387
				$apps[$registration['appId']]
388
					->getContainer()
389
					->registerAlias(
390
						$registration['alias'],
391
						$registration['target']
392
					);
393
			} catch (Throwable $e) {
394
				$appId = $registration['appId'];
395
				$this->logger->logException($e, [
396
					'message' => "Error during service alias 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->parameters as $registration) {
403
			try {
404
				$apps[$registration['appId']]
405
					->getContainer()
406
					->registerParameter(
407
						$registration['name'],
408
						$registration['value']
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
420
	/**
421
	 * @param App[] $apps
422
	 */
423
	public function delegateMiddlewareRegistrations(array $apps): void {
424
		while (($middleware = array_shift($this->middlewares)) !== null) {
425
			try {
426
				$apps[$middleware['appId']]
427
					->getContainer()
428
					->registerMiddleWare($middleware['class']);
429
			} catch (Throwable $e) {
430
				$appId = $middleware['appId'];
431
				$this->logger->logException($e, [
432
					'message' => "Error during capability registration of $appId: " . $e->getMessage(),
433
					'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

433
					'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...
434
				]);
435
			}
436
		}
437
	}
438
439
	/**
440
	 * @return array[]
441
	 */
442
	public function getSearchProviders(): array {
443
		return $this->searchProviders;
444
	}
445
446
	/**
447
	 * @return array[]
448
	 */
449
	public function getAlternativeLogins(): array {
450
		return $this->alternativeLogins;
451
	}
452
453
	/**
454
	 * @erturn array[]
455
	 */
456
	public function getInitialStates(): array {
457
		return $this->initialStates;
458
	}
459
460
	/**
461
	 * @return array[]
462
	 */
463
	public function getWellKnownHandlers(): array {
464
		return $this->wellKnownHandlers;
465
	}
466
}
467