Passed
Push — master ( 12da69...b976cd )
by Roeland
20:03 queued 05:06
created

RegistrationContext::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
rs 10
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
 *
12
 * @license GNU AGPL version 3 or any later version
13
 *
14
 * This program is free software: you can redistribute it and/or modify
15
 * it under the terms of the GNU Affero General Public License as
16
 * published by the Free Software Foundation, either version 3 of the
17
 * License, or (at your option) any later version.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 * GNU Affero General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU Affero General Public License
25
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26
 *
27
 */
28
29
namespace OC\AppFramework\Bootstrap;
30
31
use Closure;
32
use OC\Support\CrashReport\Registry;
33
use OCP\AppFramework\App;
34
use OCP\AppFramework\Bootstrap\IRegistrationContext;
35
use OCP\Dashboard\IManager;
36
use OCP\EventDispatcher\IEventDispatcher;
37
use OCP\ILogger;
38
use Throwable;
39
40
class RegistrationContext {
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,
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

276
					'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...
277
				]);
278
			}
279
		}
280
	}
281
282
	/**
283
	 * @param App[] $apps
284
	 */
285
	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

285
	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...
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,
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

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

302
	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...
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,
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

310
					'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...
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,
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

335
					'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...
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,
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

361
					'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...
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,
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

378
					'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...
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,
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

395
					'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...
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,
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
	 * @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
}
441