Passed
Push — master ( d1b03f...c1183f )
by Christoph
11:01 queued 11s
created

RegistrationContext::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 2
rs 10
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 OCP\AppFramework\App;
30
use OCP\AppFramework\Bootstrap\IRegistrationContext;
31
use OCP\EventDispatcher\IEventDispatcher;
32
use OCP\ILogger;
33
use Throwable;
34
35
class RegistrationContext {
36
37
	/** @var array[] */
38
	private $capabilities = [];
39
40
	/** @var array[] */
41
	private $services = [];
42
43
	/** @var array[] */
44
	private $aliases = [];
45
46
	/** @var array[] */
47
	private $parameters = [];
48
49
	/** @var array[] */
50
	private $eventListeners = [];
51
52
	/** @var array[] */
53
	private $middlewares = [];
54
55
	/** @var ILogger */
56
	private $logger;
57
58
	public function __construct(ILogger $logger) {
59
		$this->logger = $logger;
60
	}
61
62
	public function for(string $appId): IRegistrationContext {
63
		return new class($appId, $this) implements IRegistrationContext {
64
			/** @var string */
65
			private $appId;
66
67
			/** @var RegistrationContext */
68
			private $context;
69
70
			public function __construct(string $appId, RegistrationContext $context) {
71
				$this->appId = $appId;
72
				$this->context = $context;
73
			}
74
75
			public function registerCapability(string $capability): void {
76
				$this->context->registerCapability(
77
					$this->appId,
78
					$capability
79
				);
80
			}
81
82
			public function registerService(string $name, callable $factory, bool $shared = true): void {
83
				$this->context->registerService(
84
					$this->appId,
85
					$name,
86
					$factory,
87
					$shared
88
				);
89
			}
90
91
			public function registerServiceAlias(string $alias, string $target): void {
92
				$this->context->registerServiceAlias(
93
					$this->appId,
94
					$alias,
95
					$target
96
				);
97
			}
98
99
			public function registerParameter(string $name, $value): void {
100
				$this->context->registerParameter(
101
					$this->appId,
102
					$name,
103
					$value
104
				);
105
			}
106
107
			public function registerEventListener(string $event, string $listener, int $priority = 0): void {
108
				$this->context->registerEventListener(
109
					$this->appId,
110
					$event,
111
					$listener,
112
					$priority
113
				);
114
			}
115
116
			public function registerMiddleware(string $class): void {
117
				$this->context->registerMiddleware(
118
					$this->appId,
119
					$class
120
				);
121
			}
122
		};
123
	}
124
125
	public function registerCapability(string $appId, string $capability): void {
126
		$this->capabilities[] = [
127
			'appId' => $appId,
128
			'capability' => $capability
129
		];
130
	}
131
132
	public function registerService(string $appId, string $name, callable $factory, bool $shared = true): void {
133
		$this->services[] = [
134
			"appId" => $appId,
135
			"name" => $name,
136
			"factory" => $factory,
137
			"sharred" => $shared,
138
		];
139
	}
140
141
	public function registerServiceAlias(string $appId, string $alias, string $target): void {
142
		$this->aliases[] = [
143
			"appId" => $appId,
144
			"alias" => $alias,
145
			"target" => $target,
146
		];
147
	}
148
149
	public function registerParameter(string $appId, string $name, $value): void {
150
		$this->parameters[] = [
151
			"appId" => $appId,
152
			"name" => $name,
153
			"value" => $value,
154
		];
155
	}
156
157
	public function registerEventListener(string $appId, string $event, string $listener, int $priority = 0): void {
158
		$this->eventListeners[] = [
159
			"appId" => $appId,
160
			"event" => $event,
161
			"listener" => $listener,
162
			"priority" => $priority,
163
		];
164
	}
165
166
	public function registerMiddleware(string $appId, string $class): void {
167
		$this->middlewares[] = [
168
			"appId" => $appId,
169
			"class" => $class,
170
		];
171
	}
172
173
	/**
174
	 * @param App[] $apps
175
	 */
176
	public function delegateCapabilityRegistrations(array $apps): void {
177
		foreach ($this->capabilities as $registration) {
178
			try {
179
				$apps[$registration['appId']]
180
					->getContainer()
181
					->registerCapability($registration['capability']);
182
			} catch (Throwable $e) {
183
				$appId = $registration['appId'];
184
				$this->logger->logException($e, [
185
					'message' => "Error during capability registration of $appId: " . $e->getMessage(),
186
					'level' => ILogger::ERROR,
187
				]);
188
			}
189
		}
190
	}
191
192
	public function delegateEventListenerRegistrations(IEventDispatcher $eventDispatcher): void {
193
		foreach ($this->eventListeners as $registration) {
194
			try {
195
				if (isset($registration['priority'])) {
196
					$eventDispatcher->addServiceListener(
197
						$registration['event'],
198
						$registration['listener'],
199
						$registration['priority']
200
					);
201
				} else {
202
					$eventDispatcher->addListener(
203
						$registration['event'],
204
						$registration['listener']
205
					);
206
				}
207
			} catch (Throwable $e) {
208
				$appId = $registration['appId'];
209
				$this->logger->logException($e, [
210
					'message' => "Error during event listener registration of $appId: " . $e->getMessage(),
211
					'level' => ILogger::ERROR,
212
				]);
213
			}
214
		}
215
	}
216
217
	/**
218
	 * @param App[] $apps
219
	 */
220
	public function delegateContainerRegistrations(array $apps): void {
221
		foreach ($this->services as $registration) {
222
			try {
223
				/**
224
				 * Register the service and convert the callable into a \Closure if necessary
225
				 */
226
				$apps[$registration['appId']]
227
					->getContainer()
228
					->registerService(
229
						$registration['name'],
230
						Closure::fromCallable($registration['factory']),
231
						$registration['shared'] ?? true
232
					);
233
			} catch (Throwable $e) {
234
				$appId = $registration['appId'];
235
				$this->logger->logException($e, [
236
					'message' => "Error during service registration of $appId: " . $e->getMessage(),
237
					'level' => ILogger::ERROR,
238
				]);
239
			}
240
		}
241
242
		foreach ($this->aliases as $registration) {
243
			try {
244
				$apps[$registration['appId']]
245
					->getContainer()
246
					->registerAlias(
247
						$registration['alias'],
248
						$registration['target']
249
					);
250
			} catch (Throwable $e) {
251
				$appId = $registration['appId'];
252
				$this->logger->logException($e, [
253
					'message' => "Error during service alias registration of $appId: " . $e->getMessage(),
254
					'level' => ILogger::ERROR,
255
				]);
256
			}
257
		}
258
259
		foreach ($this->parameters as $registration) {
260
			try {
261
				$apps[$registration['appId']]
262
					->getContainer()
263
					->registerParameter(
264
						$registration['name'],
265
						$registration['value']
266
					);
267
			} catch (Throwable $e) {
268
				$appId = $registration['appId'];
269
				$this->logger->logException($e, [
270
					'message' => "Error during service alias registration of $appId: " . $e->getMessage(),
271
					'level' => ILogger::ERROR,
272
				]);
273
			}
274
		}
275
	}
276
277
	/**
278
	 * @param App[] $apps
279
	 */
280
	public function delegateMiddlewareRegistrations(array $apps): void {
281
		foreach ($this->middlewares as $middleware) {
282
			try {
283
				$apps[$middleware['appId']]
284
					->getContainer()
285
					->registerMiddleWare($middleware['class']);
286
			} catch (Throwable $e) {
287
				$appId = $middleware['appId'];
288
				$this->logger->logException($e, [
289
					'message' => "Error during capability registration of $appId: " . $e->getMessage(),
290
					'level' => ILogger::ERROR,
291
				]);
292
			}
293
		}
294
	}
295
}
296