Passed
Push — master ( 1b3486...6f167f )
by Morris
13:41 queued 12s
created

RegistrationContext.php$0 ➔ getSearchProviders()   A

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 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 2020 Christoph Wurst <[email protected]>
9
 *
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 */
25
26
namespace OC\AppFramework\Bootstrap;
27
28
use Closure;
29
use OC\Support\CrashReport\Registry;
30
use OCP\AppFramework\App;
31
use OCP\AppFramework\Bootstrap\IRegistrationContext;
32
use OCP\EventDispatcher\IEventDispatcher;
33
use OCP\ILogger;
34
use Throwable;
35
36
class RegistrationContext {
37
38
	/** @var array[] */
39
	private $capabilities = [];
40
41
	/** @var array[] */
42
	private $crashReporters = [];
43
44
	/** @var array[] */
45
	private $services = [];
46
47
	/** @var array[] */
48
	private $aliases = [];
49
50
	/** @var array[] */
51
	private $parameters = [];
52
53
	/** @var array[] */
54
	private $eventListeners = [];
55
56
	/** @var array[] */
57
	private $middlewares = [];
58
59
	/** @var array[] */
60
	private $searchProviders = [];
61
62
	/** @var ILogger */
63
	private $logger;
64
65
	public function __construct(ILogger $logger) {
66
		$this->logger = $logger;
67
	}
68
69
	public function for(string $appId): IRegistrationContext {
70
		return new class($appId, $this) implements IRegistrationContext {
71
			/** @var string */
72
			private $appId;
73
74
			/** @var RegistrationContext */
75
			private $context;
76
77
			public function __construct(string $appId, RegistrationContext $context) {
78
				$this->appId = $appId;
79
				$this->context = $context;
80
			}
81
82
			public function registerCapability(string $capability): void {
83
				$this->context->registerCapability(
84
					$this->appId,
85
					$capability
86
				);
87
			}
88
89
			public function registerCrashReporter(string $reporterClass): void {
90
				$this->context->registerCrashReporter(
91
					$this->appId,
92
					$reporterClass
93
				);
94
			}
95
96
			public function registerService(string $name, callable $factory, bool $shared = true): void {
97
				$this->context->registerService(
98
					$this->appId,
99
					$name,
100
					$factory,
101
					$shared
102
				);
103
			}
104
105
			public function registerServiceAlias(string $alias, string $target): void {
106
				$this->context->registerServiceAlias(
107
					$this->appId,
108
					$alias,
109
					$target
110
				);
111
			}
112
113
			public function registerParameter(string $name, $value): void {
114
				$this->context->registerParameter(
115
					$this->appId,
116
					$name,
117
					$value
118
				);
119
			}
120
121
			public function registerEventListener(string $event, string $listener, int $priority = 0): void {
122
				$this->context->registerEventListener(
123
					$this->appId,
124
					$event,
125
					$listener,
126
					$priority
127
				);
128
			}
129
130
			public function registerMiddleware(string $class): void {
131
				$this->context->registerMiddleware(
132
					$this->appId,
133
					$class
134
				);
135
			}
136
137
			public function registerSearchProvider(string $class): void {
138
				$this->context->registerSearchProvider(
139
					$this->appId,
140
					$class
141
				);
142
			}
143
		};
144
	}
145
146
	public function registerCapability(string $appId, string $capability): void {
147
		$this->capabilities[] = [
148
			'appId' => $appId,
149
			'capability' => $capability
150
		];
151
	}
152
153
	public function registerCrashReporter(string $appId, string $reporterClass): void {
154
		$this->crashReporters[] = [
155
			'appId' => $appId,
156
			'class' => $reporterClass,
157
		];
158
	}
159
160
	public function registerService(string $appId, string $name, callable $factory, bool $shared = true): void {
161
		$this->services[] = [
162
			"appId" => $appId,
163
			"name" => $name,
164
			"factory" => $factory,
165
			"sharred" => $shared,
166
		];
167
	}
168
169
	public function registerServiceAlias(string $appId, string $alias, string $target): void {
170
		$this->aliases[] = [
171
			"appId" => $appId,
172
			"alias" => $alias,
173
			"target" => $target,
174
		];
175
	}
176
177
	public function registerParameter(string $appId, string $name, $value): void {
178
		$this->parameters[] = [
179
			"appId" => $appId,
180
			"name" => $name,
181
			"value" => $value,
182
		];
183
	}
184
185
	public function registerEventListener(string $appId, string $event, string $listener, int $priority = 0): void {
186
		$this->eventListeners[] = [
187
			"appId" => $appId,
188
			"event" => $event,
189
			"listener" => $listener,
190
			"priority" => $priority,
191
		];
192
	}
193
194
	public function registerMiddleware(string $appId, string $class): void {
195
		$this->middlewares[] = [
196
			"appId" => $appId,
197
			"class" => $class,
198
		];
199
	}
200
201
	public function registerSearchProvider(string $appId, string $class) {
202
		$this->searchProviders[] = [
203
			'appId' => $appId,
204
			'class' => $class,
205
		];
206
	}
207
208
	/**
209
	 * @param App[] $apps
210
	 */
211
	public function delegateCapabilityRegistrations(array $apps): void {
212
		foreach ($this->capabilities as $registration) {
213
			try {
214
				$apps[$registration['appId']]
215
					->getContainer()
216
					->registerCapability($registration['capability']);
217
			} catch (Throwable $e) {
218
				$appId = $registration['appId'];
219
				$this->logger->logException($e, [
220
					'message' => "Error during capability registration of $appId: " . $e->getMessage(),
221
					'level' => ILogger::ERROR,
222
				]);
223
			}
224
		}
225
	}
226
227
	/**
228
	 * @param App[] $apps
229
	 */
230
	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

230
	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...
231
		foreach ($this->crashReporters as $registration) {
232
			try {
233
				$registry->registerLazy($registration['class']);
234
			} catch (Throwable $e) {
235
				$appId = $registration['appId'];
236
				$this->logger->logException($e, [
237
					'message' => "Error during crash reporter registration of $appId: " . $e->getMessage(),
238
					'level' => ILogger::ERROR,
239
				]);
240
			}
241
		}
242
	}
243
244
	public function delegateEventListenerRegistrations(IEventDispatcher $eventDispatcher): void {
245
		foreach ($this->eventListeners as $registration) {
246
			try {
247
				if (isset($registration['priority'])) {
248
					$eventDispatcher->addServiceListener(
249
						$registration['event'],
250
						$registration['listener'],
251
						$registration['priority']
252
					);
253
				} else {
254
					$eventDispatcher->addServiceListener(
255
						$registration['event'],
256
						$registration['listener']
257
					);
258
				}
259
			} catch (Throwable $e) {
260
				$appId = $registration['appId'];
261
				$this->logger->logException($e, [
262
					'message' => "Error during event listener registration of $appId: " . $e->getMessage(),
263
					'level' => ILogger::ERROR,
264
				]);
265
			}
266
		}
267
	}
268
269
	/**
270
	 * @param App[] $apps
271
	 */
272
	public function delegateContainerRegistrations(array $apps): void {
273
		foreach ($this->services as $registration) {
274
			try {
275
				/**
276
				 * Register the service and convert the callable into a \Closure if necessary
277
				 */
278
				$apps[$registration['appId']]
279
					->getContainer()
280
					->registerService(
281
						$registration['name'],
282
						Closure::fromCallable($registration['factory']),
283
						$registration['shared'] ?? true
284
					);
285
			} catch (Throwable $e) {
286
				$appId = $registration['appId'];
287
				$this->logger->logException($e, [
288
					'message' => "Error during service registration of $appId: " . $e->getMessage(),
289
					'level' => ILogger::ERROR,
290
				]);
291
			}
292
		}
293
294
		foreach ($this->aliases as $registration) {
295
			try {
296
				$apps[$registration['appId']]
297
					->getContainer()
298
					->registerAlias(
299
						$registration['alias'],
300
						$registration['target']
301
					);
302
			} catch (Throwable $e) {
303
				$appId = $registration['appId'];
304
				$this->logger->logException($e, [
305
					'message' => "Error during service alias registration of $appId: " . $e->getMessage(),
306
					'level' => ILogger::ERROR,
307
				]);
308
			}
309
		}
310
311
		foreach ($this->parameters as $registration) {
312
			try {
313
				$apps[$registration['appId']]
314
					->getContainer()
315
					->registerParameter(
316
						$registration['name'],
317
						$registration['value']
318
					);
319
			} catch (Throwable $e) {
320
				$appId = $registration['appId'];
321
				$this->logger->logException($e, [
322
					'message' => "Error during service alias registration of $appId: " . $e->getMessage(),
323
					'level' => ILogger::ERROR,
324
				]);
325
			}
326
		}
327
	}
328
329
	/**
330
	 * @param App[] $apps
331
	 */
332
	public function delegateMiddlewareRegistrations(array $apps): void {
333
		foreach ($this->middlewares as $middleware) {
334
			try {
335
				$apps[$middleware['appId']]
336
					->getContainer()
337
					->registerMiddleWare($middleware['class']);
338
			} catch (Throwable $e) {
339
				$appId = $middleware['appId'];
340
				$this->logger->logException($e, [
341
					'message' => "Error during capability registration of $appId: " . $e->getMessage(),
342
					'level' => ILogger::ERROR,
343
				]);
344
			}
345
		}
346
	}
347
348
	/**
349
	 * @return array[]
350
	 */
351
	public function getSearchProviders(): array {
352
		return $this->searchProviders;
353
	}
354
}
355