Completed
Push — master ( b0af46...2d0c5c )
by
unknown
31:45
created
lib/private/AppFramework/DependencyInjection/DIContainer.php 2 patches
Indentation   +305 added lines, -305 removed lines patch added patch discarded remove patch
@@ -64,309 +64,309 @@
 block discarded – undo
64 64
 use Psr\Log\LoggerInterface;
65 65
 
66 66
 class DIContainer extends SimpleContainer implements IAppContainer {
67
-	protected string $appName;
68
-	private array $middleWares = [];
69
-	private ServerContainer $server;
70
-
71
-	public function __construct(string $appName, array $urlParams = [], ?ServerContainer $server = null) {
72
-		parent::__construct();
73
-		$this->appName = $appName;
74
-		$this->registerParameter('appName', $appName);
75
-		$this->registerParameter('urlParams', $urlParams);
76
-
77
-		/** @deprecated 32.0.0 */
78
-		$this->registerDeprecatedAlias('Request', IRequest::class);
79
-
80
-		if ($server === null) {
81
-			$server = \OC::$server;
82
-		}
83
-		$this->server = $server;
84
-		$this->server->registerAppContainer($appName, $this);
85
-
86
-		// aliases
87
-		/** @deprecated 26.0.0 inject $appName */
88
-		$this->registerDeprecatedAlias('AppName', 'appName');
89
-		/** @deprecated 26.0.0 inject $webRoot*/
90
-		$this->registerDeprecatedAlias('WebRoot', 'webRoot');
91
-		/** @deprecated 26.0.0 inject $userId */
92
-		$this->registerDeprecatedAlias('UserId', 'userId');
93
-
94
-		/**
95
-		 * Core services
96
-		 */
97
-		/* Cannot be an alias because Output is not in OCA */
98
-		$this->registerService(IOutput::class, fn (ContainerInterface $c): IOutput => new Output($c->get('webRoot')));
99
-
100
-		$this->registerService(Folder::class, function () {
101
-			return $this->getServer()->getUserFolder();
102
-		});
103
-
104
-		$this->registerService(IAppData::class, function (ContainerInterface $c): IAppData {
105
-			return $c->get(IAppDataFactory::class)->get($c->get('appName'));
106
-		});
107
-
108
-		$this->registerService(IL10N::class, function (ContainerInterface $c) {
109
-			return $this->getServer()->getL10N($c->get('appName'));
110
-		});
111
-
112
-		// Log wrappers
113
-		$this->registerService(LoggerInterface::class, function (ContainerInterface $c) {
114
-			/* Cannot be an alias because it uses LoggerInterface so it would infinite loop */
115
-			return new ScopedPsrLogger(
116
-				$c->get(PsrLoggerAdapter::class),
117
-				$c->get('appName')
118
-			);
119
-		});
120
-
121
-		$this->registerService(IServerContainer::class, function () {
122
-			return $this->getServer();
123
-		});
124
-		/** @deprecated 32.0.0 */
125
-		$this->registerDeprecatedAlias('ServerContainer', IServerContainer::class);
126
-
127
-		$this->registerAlias(\OCP\WorkflowEngine\IManager::class, Manager::class);
128
-
129
-		$this->registerService(ContainerInterface::class, fn (ContainerInterface $c) => $c);
130
-		$this->registerDeprecatedAlias(IAppContainer::class, ContainerInterface::class);
131
-
132
-		// commonly used attributes
133
-		$this->registerService('userId', function (ContainerInterface $c): ?string {
134
-			return $c->get(ISession::class)->get('user_id');
135
-		});
136
-
137
-		$this->registerService('webRoot', function (ContainerInterface $c): string {
138
-			return $c->get(IServerContainer::class)->getWebRoot();
139
-		});
140
-
141
-		$this->registerService('OC_Defaults', function (ContainerInterface $c): object {
142
-			return $c->get(IServerContainer::class)->get('ThemingDefaults');
143
-		});
144
-
145
-		/** @deprecated 32.0.0 */
146
-		$this->registerDeprecatedAlias('Protocol', Http::class);
147
-		$this->registerService(Http::class, function (ContainerInterface $c) {
148
-			$protocol = $c->get(IRequest::class)->getHttpProtocol();
149
-			return new Http($_SERVER, $protocol);
150
-		});
151
-
152
-		/** @deprecated 32.0.0 */
153
-		$this->registerDeprecatedAlias('Dispatcher', Dispatcher::class);
154
-		$this->registerService(Dispatcher::class, function (ContainerInterface $c) {
155
-			return new Dispatcher(
156
-				$c->get(Http::class),
157
-				$c->get(MiddlewareDispatcher::class),
158
-				$c->get(IControllerMethodReflector::class),
159
-				$c->get(IRequest::class),
160
-				$c->get(IConfig::class),
161
-				$c->get(IDBConnection::class),
162
-				$c->get(LoggerInterface::class),
163
-				$c->get(EventLogger::class),
164
-				$c,
165
-			);
166
-		});
167
-
168
-		/**
169
-		 * App Framework default arguments
170
-		 */
171
-		$this->registerParameter('corsMethods', 'PUT, POST, GET, DELETE, PATCH');
172
-		$this->registerParameter('corsAllowedHeaders', 'Authorization, Content-Type, Accept');
173
-		$this->registerParameter('corsMaxAge', 1728000);
174
-
175
-		/**
176
-		 * Middleware
177
-		 */
178
-		/** @deprecated 32.0.0 */
179
-		$this->registerDeprecatedAlias('MiddlewareDispatcher', MiddlewareDispatcher::class);
180
-		$this->registerService(MiddlewareDispatcher::class, function (ContainerInterface $c) {
181
-			$server = $this->getServer();
182
-
183
-			$dispatcher = new MiddlewareDispatcher();
184
-
185
-			$dispatcher->registerMiddleware($c->get(CompressionMiddleware::class));
186
-			$dispatcher->registerMiddleware($c->get(NotModifiedMiddleware::class));
187
-			$dispatcher->registerMiddleware($c->get(ReloadExecutionMiddleware::class));
188
-			$dispatcher->registerMiddleware($c->get(SameSiteCookieMiddleware::class));
189
-			$dispatcher->registerMiddleware($c->get(CORSMiddleware::class));
190
-			$dispatcher->registerMiddleware($c->get(OCSMiddleware::class));
191
-
192
-			$dispatcher->registerMiddleware($c->get(FlowV2EphemeralSessionsMiddleware::class));
193
-
194
-			$securityMiddleware = new SecurityMiddleware(
195
-				$c->get(IRequest::class),
196
-				$c->get(IControllerMethodReflector::class),
197
-				$c->get(INavigationManager::class),
198
-				$c->get(IURLGenerator::class),
199
-				$c->get(LoggerInterface::class),
200
-				$c->get('appName'),
201
-				$server->getUserSession()->isLoggedIn(),
202
-				$c->get(IGroupManager::class),
203
-				$c->get(ISubAdmin::class),
204
-				$c->get(IAppManager::class),
205
-				$server->getL10N('lib'),
206
-				$c->get(AuthorizedGroupMapper::class),
207
-				$c->get(IUserSession::class),
208
-				$c->get(IRemoteAddress::class),
209
-			);
210
-			$dispatcher->registerMiddleware($securityMiddleware);
211
-			$dispatcher->registerMiddleware($c->get(CSPMiddleware::class));
212
-			$dispatcher->registerMiddleware($c->get(FeaturePolicyMiddleware::class));
213
-			$dispatcher->registerMiddleware($c->get(PasswordConfirmationMiddleware::class));
214
-			$dispatcher->registerMiddleware($c->get(TwoFactorMiddleware::class));
215
-			$dispatcher->registerMiddleware($c->get(BruteForceMiddleware::class));
216
-			$dispatcher->registerMiddleware($c->get(RateLimitingMiddleware::class));
217
-			$dispatcher->registerMiddleware($c->get(PublicShareMiddleware::class));
218
-			$dispatcher->registerMiddleware($c->get(AdditionalScriptsMiddleware::class));
219
-
220
-			$coordinator = $c->get(\OC\AppFramework\Bootstrap\Coordinator::class);
221
-			$registrationContext = $coordinator->getRegistrationContext();
222
-			if ($registrationContext !== null) {
223
-				$appId = $this->get('appName');
224
-				foreach ($registrationContext->getMiddlewareRegistrations() as $middlewareRegistration) {
225
-					if ($middlewareRegistration->getAppId() === $appId
226
-						|| $middlewareRegistration->isGlobal()) {
227
-						$dispatcher->registerMiddleware($c->get($middlewareRegistration->getService()));
228
-					}
229
-				}
230
-			}
231
-			foreach ($this->middleWares as $middleWare) {
232
-				$dispatcher->registerMiddleware($c->get($middleWare));
233
-			}
234
-
235
-			$dispatcher->registerMiddleware($c->get(SessionMiddleware::class));
236
-			return $dispatcher;
237
-		});
238
-
239
-		$this->registerAlias(IAppConfig::class, \OC\AppFramework\Services\AppConfig::class);
240
-		$this->registerAlias(IInitialState::class, \OC\AppFramework\Services\InitialState::class);
241
-	}
242
-
243
-	/**
244
-	 * @return \OCP\IServerContainer
245
-	 */
246
-	public function getServer() {
247
-		return $this->server;
248
-	}
249
-
250
-	/**
251
-	 * @param string $middleWare
252
-	 */
253
-	public function registerMiddleWare($middleWare): bool {
254
-		if (in_array($middleWare, $this->middleWares, true) !== false) {
255
-			return false;
256
-		}
257
-		$this->middleWares[] = $middleWare;
258
-		return true;
259
-	}
260
-
261
-	/**
262
-	 * used to return the appname of the set application
263
-	 * @return string the name of your application
264
-	 */
265
-	public function getAppName() {
266
-		return $this->query('appName');
267
-	}
268
-
269
-	/**
270
-	 * @deprecated 12.0.0 use IUserSession->isLoggedIn()
271
-	 * @return boolean
272
-	 */
273
-	public function isLoggedIn() {
274
-		return \OC::$server->getUserSession()->isLoggedIn();
275
-	}
276
-
277
-	/**
278
-	 * @deprecated 12.0.0 use IGroupManager->isAdmin($userId)
279
-	 * @return boolean
280
-	 */
281
-	public function isAdminUser() {
282
-		$uid = $this->getUserId();
283
-		return \OC_User::isAdminUser($uid);
284
-	}
285
-
286
-	private function getUserId(): string {
287
-		return $this->getServer()->getSession()->get('user_id');
288
-	}
289
-
290
-	/**
291
-	 * Register a capability
292
-	 *
293
-	 * @param string $serviceName e.g. 'OCA\Files\Capabilities'
294
-	 */
295
-	public function registerCapability($serviceName) {
296
-		$this->query('OC\CapabilitiesManager')->registerCapability(function () use ($serviceName) {
297
-			return $this->query($serviceName);
298
-		});
299
-	}
300
-
301
-	public function has($id): bool {
302
-		if (parent::has($id)) {
303
-			return true;
304
-		}
305
-
306
-		if ($this->server->has($id, true)) {
307
-			return true;
308
-		}
309
-
310
-		return false;
311
-	}
312
-
313
-	/**
314
-	 * @inheritDoc
315
-	 * @param list<class-string> $chain
316
-	 */
317
-	public function query(string $name, bool $autoload = true, array $chain = []) {
318
-		if ($name === 'AppName' || $name === 'appName') {
319
-			return $this->appName;
320
-		}
321
-
322
-		$isServerClass = str_starts_with($name, 'OCP\\') || str_starts_with($name, 'OC\\');
323
-		if ($isServerClass && !$this->has($name)) {
324
-			/** @var ServerContainer $server */
325
-			$server = $this->getServer();
326
-			return $server->query($name, $autoload, $chain);
327
-		}
328
-
329
-		try {
330
-			return $this->queryNoFallback($name, $chain);
331
-		} catch (QueryException $firstException) {
332
-			try {
333
-				/** @var ServerContainer $server */
334
-				$server = $this->getServer();
335
-				return $server->query($name, $autoload, $chain);
336
-			} catch (QueryException $secondException) {
337
-				if ($firstException->getCode() === 1) {
338
-					throw $secondException;
339
-				}
340
-				throw $firstException;
341
-			}
342
-		}
343
-	}
344
-
345
-	/**
346
-	 * @param string $name
347
-	 * @return mixed
348
-	 * @throws QueryException if the query could not be resolved
349
-	 */
350
-	public function queryNoFallback($name, array $chain) {
351
-		$name = $this->sanitizeName($name);
352
-
353
-		if ($this->offsetExists($name)) {
354
-			return parent::query($name, chain: $chain);
355
-		} elseif ($this->appName === 'settings' && str_starts_with($name, 'OC\\Settings\\')) {
356
-			return parent::query($name, chain: $chain);
357
-		} elseif ($this->appName === 'core' && str_starts_with($name, 'OC\\Core\\')) {
358
-			return parent::query($name, chain: $chain);
359
-		} elseif (str_starts_with($name, \OC\AppFramework\App::buildAppNamespace($this->appName) . '\\')) {
360
-			return parent::query($name, chain: $chain);
361
-		} elseif (
362
-			str_starts_with($name, 'OC\\AppFramework\\Services\\')
363
-			|| str_starts_with($name, 'OC\\AppFramework\\Middleware\\')
364
-		) {
365
-			/* AppFramework services are scoped to the application */
366
-			return parent::query($name, chain: $chain);
367
-		}
368
-
369
-		throw new QueryException('Could not resolve ' . $name . '!'
370
-			. ' Class can not be instantiated', 1);
371
-	}
67
+    protected string $appName;
68
+    private array $middleWares = [];
69
+    private ServerContainer $server;
70
+
71
+    public function __construct(string $appName, array $urlParams = [], ?ServerContainer $server = null) {
72
+        parent::__construct();
73
+        $this->appName = $appName;
74
+        $this->registerParameter('appName', $appName);
75
+        $this->registerParameter('urlParams', $urlParams);
76
+
77
+        /** @deprecated 32.0.0 */
78
+        $this->registerDeprecatedAlias('Request', IRequest::class);
79
+
80
+        if ($server === null) {
81
+            $server = \OC::$server;
82
+        }
83
+        $this->server = $server;
84
+        $this->server->registerAppContainer($appName, $this);
85
+
86
+        // aliases
87
+        /** @deprecated 26.0.0 inject $appName */
88
+        $this->registerDeprecatedAlias('AppName', 'appName');
89
+        /** @deprecated 26.0.0 inject $webRoot*/
90
+        $this->registerDeprecatedAlias('WebRoot', 'webRoot');
91
+        /** @deprecated 26.0.0 inject $userId */
92
+        $this->registerDeprecatedAlias('UserId', 'userId');
93
+
94
+        /**
95
+         * Core services
96
+         */
97
+        /* Cannot be an alias because Output is not in OCA */
98
+        $this->registerService(IOutput::class, fn (ContainerInterface $c): IOutput => new Output($c->get('webRoot')));
99
+
100
+        $this->registerService(Folder::class, function () {
101
+            return $this->getServer()->getUserFolder();
102
+        });
103
+
104
+        $this->registerService(IAppData::class, function (ContainerInterface $c): IAppData {
105
+            return $c->get(IAppDataFactory::class)->get($c->get('appName'));
106
+        });
107
+
108
+        $this->registerService(IL10N::class, function (ContainerInterface $c) {
109
+            return $this->getServer()->getL10N($c->get('appName'));
110
+        });
111
+
112
+        // Log wrappers
113
+        $this->registerService(LoggerInterface::class, function (ContainerInterface $c) {
114
+            /* Cannot be an alias because it uses LoggerInterface so it would infinite loop */
115
+            return new ScopedPsrLogger(
116
+                $c->get(PsrLoggerAdapter::class),
117
+                $c->get('appName')
118
+            );
119
+        });
120
+
121
+        $this->registerService(IServerContainer::class, function () {
122
+            return $this->getServer();
123
+        });
124
+        /** @deprecated 32.0.0 */
125
+        $this->registerDeprecatedAlias('ServerContainer', IServerContainer::class);
126
+
127
+        $this->registerAlias(\OCP\WorkflowEngine\IManager::class, Manager::class);
128
+
129
+        $this->registerService(ContainerInterface::class, fn (ContainerInterface $c) => $c);
130
+        $this->registerDeprecatedAlias(IAppContainer::class, ContainerInterface::class);
131
+
132
+        // commonly used attributes
133
+        $this->registerService('userId', function (ContainerInterface $c): ?string {
134
+            return $c->get(ISession::class)->get('user_id');
135
+        });
136
+
137
+        $this->registerService('webRoot', function (ContainerInterface $c): string {
138
+            return $c->get(IServerContainer::class)->getWebRoot();
139
+        });
140
+
141
+        $this->registerService('OC_Defaults', function (ContainerInterface $c): object {
142
+            return $c->get(IServerContainer::class)->get('ThemingDefaults');
143
+        });
144
+
145
+        /** @deprecated 32.0.0 */
146
+        $this->registerDeprecatedAlias('Protocol', Http::class);
147
+        $this->registerService(Http::class, function (ContainerInterface $c) {
148
+            $protocol = $c->get(IRequest::class)->getHttpProtocol();
149
+            return new Http($_SERVER, $protocol);
150
+        });
151
+
152
+        /** @deprecated 32.0.0 */
153
+        $this->registerDeprecatedAlias('Dispatcher', Dispatcher::class);
154
+        $this->registerService(Dispatcher::class, function (ContainerInterface $c) {
155
+            return new Dispatcher(
156
+                $c->get(Http::class),
157
+                $c->get(MiddlewareDispatcher::class),
158
+                $c->get(IControllerMethodReflector::class),
159
+                $c->get(IRequest::class),
160
+                $c->get(IConfig::class),
161
+                $c->get(IDBConnection::class),
162
+                $c->get(LoggerInterface::class),
163
+                $c->get(EventLogger::class),
164
+                $c,
165
+            );
166
+        });
167
+
168
+        /**
169
+         * App Framework default arguments
170
+         */
171
+        $this->registerParameter('corsMethods', 'PUT, POST, GET, DELETE, PATCH');
172
+        $this->registerParameter('corsAllowedHeaders', 'Authorization, Content-Type, Accept');
173
+        $this->registerParameter('corsMaxAge', 1728000);
174
+
175
+        /**
176
+         * Middleware
177
+         */
178
+        /** @deprecated 32.0.0 */
179
+        $this->registerDeprecatedAlias('MiddlewareDispatcher', MiddlewareDispatcher::class);
180
+        $this->registerService(MiddlewareDispatcher::class, function (ContainerInterface $c) {
181
+            $server = $this->getServer();
182
+
183
+            $dispatcher = new MiddlewareDispatcher();
184
+
185
+            $dispatcher->registerMiddleware($c->get(CompressionMiddleware::class));
186
+            $dispatcher->registerMiddleware($c->get(NotModifiedMiddleware::class));
187
+            $dispatcher->registerMiddleware($c->get(ReloadExecutionMiddleware::class));
188
+            $dispatcher->registerMiddleware($c->get(SameSiteCookieMiddleware::class));
189
+            $dispatcher->registerMiddleware($c->get(CORSMiddleware::class));
190
+            $dispatcher->registerMiddleware($c->get(OCSMiddleware::class));
191
+
192
+            $dispatcher->registerMiddleware($c->get(FlowV2EphemeralSessionsMiddleware::class));
193
+
194
+            $securityMiddleware = new SecurityMiddleware(
195
+                $c->get(IRequest::class),
196
+                $c->get(IControllerMethodReflector::class),
197
+                $c->get(INavigationManager::class),
198
+                $c->get(IURLGenerator::class),
199
+                $c->get(LoggerInterface::class),
200
+                $c->get('appName'),
201
+                $server->getUserSession()->isLoggedIn(),
202
+                $c->get(IGroupManager::class),
203
+                $c->get(ISubAdmin::class),
204
+                $c->get(IAppManager::class),
205
+                $server->getL10N('lib'),
206
+                $c->get(AuthorizedGroupMapper::class),
207
+                $c->get(IUserSession::class),
208
+                $c->get(IRemoteAddress::class),
209
+            );
210
+            $dispatcher->registerMiddleware($securityMiddleware);
211
+            $dispatcher->registerMiddleware($c->get(CSPMiddleware::class));
212
+            $dispatcher->registerMiddleware($c->get(FeaturePolicyMiddleware::class));
213
+            $dispatcher->registerMiddleware($c->get(PasswordConfirmationMiddleware::class));
214
+            $dispatcher->registerMiddleware($c->get(TwoFactorMiddleware::class));
215
+            $dispatcher->registerMiddleware($c->get(BruteForceMiddleware::class));
216
+            $dispatcher->registerMiddleware($c->get(RateLimitingMiddleware::class));
217
+            $dispatcher->registerMiddleware($c->get(PublicShareMiddleware::class));
218
+            $dispatcher->registerMiddleware($c->get(AdditionalScriptsMiddleware::class));
219
+
220
+            $coordinator = $c->get(\OC\AppFramework\Bootstrap\Coordinator::class);
221
+            $registrationContext = $coordinator->getRegistrationContext();
222
+            if ($registrationContext !== null) {
223
+                $appId = $this->get('appName');
224
+                foreach ($registrationContext->getMiddlewareRegistrations() as $middlewareRegistration) {
225
+                    if ($middlewareRegistration->getAppId() === $appId
226
+                        || $middlewareRegistration->isGlobal()) {
227
+                        $dispatcher->registerMiddleware($c->get($middlewareRegistration->getService()));
228
+                    }
229
+                }
230
+            }
231
+            foreach ($this->middleWares as $middleWare) {
232
+                $dispatcher->registerMiddleware($c->get($middleWare));
233
+            }
234
+
235
+            $dispatcher->registerMiddleware($c->get(SessionMiddleware::class));
236
+            return $dispatcher;
237
+        });
238
+
239
+        $this->registerAlias(IAppConfig::class, \OC\AppFramework\Services\AppConfig::class);
240
+        $this->registerAlias(IInitialState::class, \OC\AppFramework\Services\InitialState::class);
241
+    }
242
+
243
+    /**
244
+     * @return \OCP\IServerContainer
245
+     */
246
+    public function getServer() {
247
+        return $this->server;
248
+    }
249
+
250
+    /**
251
+     * @param string $middleWare
252
+     */
253
+    public function registerMiddleWare($middleWare): bool {
254
+        if (in_array($middleWare, $this->middleWares, true) !== false) {
255
+            return false;
256
+        }
257
+        $this->middleWares[] = $middleWare;
258
+        return true;
259
+    }
260
+
261
+    /**
262
+     * used to return the appname of the set application
263
+     * @return string the name of your application
264
+     */
265
+    public function getAppName() {
266
+        return $this->query('appName');
267
+    }
268
+
269
+    /**
270
+     * @deprecated 12.0.0 use IUserSession->isLoggedIn()
271
+     * @return boolean
272
+     */
273
+    public function isLoggedIn() {
274
+        return \OC::$server->getUserSession()->isLoggedIn();
275
+    }
276
+
277
+    /**
278
+     * @deprecated 12.0.0 use IGroupManager->isAdmin($userId)
279
+     * @return boolean
280
+     */
281
+    public function isAdminUser() {
282
+        $uid = $this->getUserId();
283
+        return \OC_User::isAdminUser($uid);
284
+    }
285
+
286
+    private function getUserId(): string {
287
+        return $this->getServer()->getSession()->get('user_id');
288
+    }
289
+
290
+    /**
291
+     * Register a capability
292
+     *
293
+     * @param string $serviceName e.g. 'OCA\Files\Capabilities'
294
+     */
295
+    public function registerCapability($serviceName) {
296
+        $this->query('OC\CapabilitiesManager')->registerCapability(function () use ($serviceName) {
297
+            return $this->query($serviceName);
298
+        });
299
+    }
300
+
301
+    public function has($id): bool {
302
+        if (parent::has($id)) {
303
+            return true;
304
+        }
305
+
306
+        if ($this->server->has($id, true)) {
307
+            return true;
308
+        }
309
+
310
+        return false;
311
+    }
312
+
313
+    /**
314
+     * @inheritDoc
315
+     * @param list<class-string> $chain
316
+     */
317
+    public function query(string $name, bool $autoload = true, array $chain = []) {
318
+        if ($name === 'AppName' || $name === 'appName') {
319
+            return $this->appName;
320
+        }
321
+
322
+        $isServerClass = str_starts_with($name, 'OCP\\') || str_starts_with($name, 'OC\\');
323
+        if ($isServerClass && !$this->has($name)) {
324
+            /** @var ServerContainer $server */
325
+            $server = $this->getServer();
326
+            return $server->query($name, $autoload, $chain);
327
+        }
328
+
329
+        try {
330
+            return $this->queryNoFallback($name, $chain);
331
+        } catch (QueryException $firstException) {
332
+            try {
333
+                /** @var ServerContainer $server */
334
+                $server = $this->getServer();
335
+                return $server->query($name, $autoload, $chain);
336
+            } catch (QueryException $secondException) {
337
+                if ($firstException->getCode() === 1) {
338
+                    throw $secondException;
339
+                }
340
+                throw $firstException;
341
+            }
342
+        }
343
+    }
344
+
345
+    /**
346
+     * @param string $name
347
+     * @return mixed
348
+     * @throws QueryException if the query could not be resolved
349
+     */
350
+    public function queryNoFallback($name, array $chain) {
351
+        $name = $this->sanitizeName($name);
352
+
353
+        if ($this->offsetExists($name)) {
354
+            return parent::query($name, chain: $chain);
355
+        } elseif ($this->appName === 'settings' && str_starts_with($name, 'OC\\Settings\\')) {
356
+            return parent::query($name, chain: $chain);
357
+        } elseif ($this->appName === 'core' && str_starts_with($name, 'OC\\Core\\')) {
358
+            return parent::query($name, chain: $chain);
359
+        } elseif (str_starts_with($name, \OC\AppFramework\App::buildAppNamespace($this->appName) . '\\')) {
360
+            return parent::query($name, chain: $chain);
361
+        } elseif (
362
+            str_starts_with($name, 'OC\\AppFramework\\Services\\')
363
+            || str_starts_with($name, 'OC\\AppFramework\\Middleware\\')
364
+        ) {
365
+            /* AppFramework services are scoped to the application */
366
+            return parent::query($name, chain: $chain);
367
+        }
368
+
369
+        throw new QueryException('Could not resolve ' . $name . '!'
370
+            . ' Class can not be instantiated', 1);
371
+    }
372 372
 }
Please login to merge, or discard this patch.
Spacing   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -97,20 +97,20 @@  discard block
 block discarded – undo
97 97
 		/* Cannot be an alias because Output is not in OCA */
98 98
 		$this->registerService(IOutput::class, fn (ContainerInterface $c): IOutput => new Output($c->get('webRoot')));
99 99
 
100
-		$this->registerService(Folder::class, function () {
100
+		$this->registerService(Folder::class, function() {
101 101
 			return $this->getServer()->getUserFolder();
102 102
 		});
103 103
 
104
-		$this->registerService(IAppData::class, function (ContainerInterface $c): IAppData {
104
+		$this->registerService(IAppData::class, function(ContainerInterface $c): IAppData {
105 105
 			return $c->get(IAppDataFactory::class)->get($c->get('appName'));
106 106
 		});
107 107
 
108
-		$this->registerService(IL10N::class, function (ContainerInterface $c) {
108
+		$this->registerService(IL10N::class, function(ContainerInterface $c) {
109 109
 			return $this->getServer()->getL10N($c->get('appName'));
110 110
 		});
111 111
 
112 112
 		// Log wrappers
113
-		$this->registerService(LoggerInterface::class, function (ContainerInterface $c) {
113
+		$this->registerService(LoggerInterface::class, function(ContainerInterface $c) {
114 114
 			/* Cannot be an alias because it uses LoggerInterface so it would infinite loop */
115 115
 			return new ScopedPsrLogger(
116 116
 				$c->get(PsrLoggerAdapter::class),
@@ -118,7 +118,7 @@  discard block
 block discarded – undo
118 118
 			);
119 119
 		});
120 120
 
121
-		$this->registerService(IServerContainer::class, function () {
121
+		$this->registerService(IServerContainer::class, function() {
122 122
 			return $this->getServer();
123 123
 		});
124 124
 		/** @deprecated 32.0.0 */
@@ -130,28 +130,28 @@  discard block
 block discarded – undo
130 130
 		$this->registerDeprecatedAlias(IAppContainer::class, ContainerInterface::class);
131 131
 
132 132
 		// commonly used attributes
133
-		$this->registerService('userId', function (ContainerInterface $c): ?string {
133
+		$this->registerService('userId', function(ContainerInterface $c): ?string {
134 134
 			return $c->get(ISession::class)->get('user_id');
135 135
 		});
136 136
 
137
-		$this->registerService('webRoot', function (ContainerInterface $c): string {
137
+		$this->registerService('webRoot', function(ContainerInterface $c): string {
138 138
 			return $c->get(IServerContainer::class)->getWebRoot();
139 139
 		});
140 140
 
141
-		$this->registerService('OC_Defaults', function (ContainerInterface $c): object {
141
+		$this->registerService('OC_Defaults', function(ContainerInterface $c): object {
142 142
 			return $c->get(IServerContainer::class)->get('ThemingDefaults');
143 143
 		});
144 144
 
145 145
 		/** @deprecated 32.0.0 */
146 146
 		$this->registerDeprecatedAlias('Protocol', Http::class);
147
-		$this->registerService(Http::class, function (ContainerInterface $c) {
147
+		$this->registerService(Http::class, function(ContainerInterface $c) {
148 148
 			$protocol = $c->get(IRequest::class)->getHttpProtocol();
149 149
 			return new Http($_SERVER, $protocol);
150 150
 		});
151 151
 
152 152
 		/** @deprecated 32.0.0 */
153 153
 		$this->registerDeprecatedAlias('Dispatcher', Dispatcher::class);
154
-		$this->registerService(Dispatcher::class, function (ContainerInterface $c) {
154
+		$this->registerService(Dispatcher::class, function(ContainerInterface $c) {
155 155
 			return new Dispatcher(
156 156
 				$c->get(Http::class),
157 157
 				$c->get(MiddlewareDispatcher::class),
@@ -177,7 +177,7 @@  discard block
 block discarded – undo
177 177
 		 */
178 178
 		/** @deprecated 32.0.0 */
179 179
 		$this->registerDeprecatedAlias('MiddlewareDispatcher', MiddlewareDispatcher::class);
180
-		$this->registerService(MiddlewareDispatcher::class, function (ContainerInterface $c) {
180
+		$this->registerService(MiddlewareDispatcher::class, function(ContainerInterface $c) {
181 181
 			$server = $this->getServer();
182 182
 
183 183
 			$dispatcher = new MiddlewareDispatcher();
@@ -293,7 +293,7 @@  discard block
 block discarded – undo
293 293
 	 * @param string $serviceName e.g. 'OCA\Files\Capabilities'
294 294
 	 */
295 295
 	public function registerCapability($serviceName) {
296
-		$this->query('OC\CapabilitiesManager')->registerCapability(function () use ($serviceName) {
296
+		$this->query('OC\CapabilitiesManager')->registerCapability(function() use ($serviceName) {
297 297
 			return $this->query($serviceName);
298 298
 		});
299 299
 	}
@@ -356,7 +356,7 @@  discard block
 block discarded – undo
356 356
 			return parent::query($name, chain: $chain);
357 357
 		} elseif ($this->appName === 'core' && str_starts_with($name, 'OC\\Core\\')) {
358 358
 			return parent::query($name, chain: $chain);
359
-		} elseif (str_starts_with($name, \OC\AppFramework\App::buildAppNamespace($this->appName) . '\\')) {
359
+		} elseif (str_starts_with($name, \OC\AppFramework\App::buildAppNamespace($this->appName).'\\')) {
360 360
 			return parent::query($name, chain: $chain);
361 361
 		} elseif (
362 362
 			str_starts_with($name, 'OC\\AppFramework\\Services\\')
@@ -366,7 +366,7 @@  discard block
 block discarded – undo
366 366
 			return parent::query($name, chain: $chain);
367 367
 		}
368 368
 
369
-		throw new QueryException('Could not resolve ' . $name . '!'
369
+		throw new QueryException('Could not resolve '.$name.'!'
370 370
 			. ' Class can not be instantiated', 1);
371 371
 	}
372 372
 }
Please login to merge, or discard this patch.
lib/private/AppFramework/Utility/SimpleContainer.php 2 patches
Indentation   +211 added lines, -211 removed lines patch added patch discarded remove patch
@@ -26,242 +26,242 @@
 block discarded – undo
26 26
  * SimpleContainer is a simple implementation of a container on basis of Pimple
27 27
  */
28 28
 class SimpleContainer implements ArrayAccess, ContainerInterface, IContainer {
29
-	public static bool $useLazyObjects = false;
29
+    public static bool $useLazyObjects = false;
30 30
 
31
-	private Container $container;
31
+    private Container $container;
32 32
 
33
-	public function __construct() {
34
-		$this->container = new Container();
35
-	}
33
+    public function __construct() {
34
+        $this->container = new Container();
35
+    }
36 36
 
37
-	/**
38
-	 * @template T
39
-	 * @param class-string<T>|string $id
40
-	 * @return T|mixed
41
-	 * @psalm-template S as class-string<T>|string
42
-	 * @psalm-param S $id
43
-	 * @psalm-return (S is class-string<T> ? T : mixed)
44
-	 */
45
-	public function get(string $id): mixed {
46
-		return $this->query($id);
47
-	}
37
+    /**
38
+     * @template T
39
+     * @param class-string<T>|string $id
40
+     * @return T|mixed
41
+     * @psalm-template S as class-string<T>|string
42
+     * @psalm-param S $id
43
+     * @psalm-return (S is class-string<T> ? T : mixed)
44
+     */
45
+    public function get(string $id): mixed {
46
+        return $this->query($id);
47
+    }
48 48
 
49
-	public function has(string $id): bool {
50
-		// If a service is no registered but is an existing class, we can probably load it
51
-		return isset($this->container[$id]) || class_exists($id);
52
-	}
49
+    public function has(string $id): bool {
50
+        // If a service is no registered but is an existing class, we can probably load it
51
+        return isset($this->container[$id]) || class_exists($id);
52
+    }
53 53
 
54
-	/**
55
-	 * @param ReflectionClass $class the class to instantiate
56
-	 * @param list<class-string> $chain
57
-	 * @return object the created class
58
-	 * @suppress PhanUndeclaredClassInstanceof
59
-	 */
60
-	private function buildClass(ReflectionClass $class, array $chain): object {
61
-		$constructor = $class->getConstructor();
62
-		if ($constructor === null) {
63
-			/* No constructor, return a instance directly */
64
-			return $class->newInstance();
65
-		}
66
-		if (PHP_VERSION_ID >= 80400 && self::$useLazyObjects && !$class->isInternal()) {
67
-			/* For PHP>=8.4, use a lazy ghost to delay constructor and dependency resolving */
68
-			/** @psalm-suppress UndefinedMethod */
69
-			return $class->newLazyGhost(function (object $object) use ($constructor, $chain): void {
70
-				/** @psalm-suppress DirectConstructorCall For lazy ghosts we have to call the constructor directly */
71
-				$object->__construct(...$this->buildClassConstructorParameters($constructor, $chain));
72
-			});
73
-		} else {
74
-			return $class->newInstanceArgs($this->buildClassConstructorParameters($constructor, $chain));
75
-		}
76
-	}
54
+    /**
55
+     * @param ReflectionClass $class the class to instantiate
56
+     * @param list<class-string> $chain
57
+     * @return object the created class
58
+     * @suppress PhanUndeclaredClassInstanceof
59
+     */
60
+    private function buildClass(ReflectionClass $class, array $chain): object {
61
+        $constructor = $class->getConstructor();
62
+        if ($constructor === null) {
63
+            /* No constructor, return a instance directly */
64
+            return $class->newInstance();
65
+        }
66
+        if (PHP_VERSION_ID >= 80400 && self::$useLazyObjects && !$class->isInternal()) {
67
+            /* For PHP>=8.4, use a lazy ghost to delay constructor and dependency resolving */
68
+            /** @psalm-suppress UndefinedMethod */
69
+            return $class->newLazyGhost(function (object $object) use ($constructor, $chain): void {
70
+                /** @psalm-suppress DirectConstructorCall For lazy ghosts we have to call the constructor directly */
71
+                $object->__construct(...$this->buildClassConstructorParameters($constructor, $chain));
72
+            });
73
+        } else {
74
+            return $class->newInstanceArgs($this->buildClassConstructorParameters($constructor, $chain));
75
+        }
76
+    }
77 77
 
78
-	/**
79
-	 * @param list<class-string> $chain
80
-	 */
81
-	private function buildClassConstructorParameters(\ReflectionMethod $constructor, array $chain): array {
82
-		return array_map(function (ReflectionParameter $parameter) use ($chain) {
83
-			$parameterType = $parameter->getType();
78
+    /**
79
+     * @param list<class-string> $chain
80
+     */
81
+    private function buildClassConstructorParameters(\ReflectionMethod $constructor, array $chain): array {
82
+        return array_map(function (ReflectionParameter $parameter) use ($chain) {
83
+            $parameterType = $parameter->getType();
84 84
 
85
-			$resolveName = $parameter->getName();
85
+            $resolveName = $parameter->getName();
86 86
 
87
-			// try to find out if it is a class or a simple parameter
88
-			if ($parameterType !== null && ($parameterType instanceof ReflectionNamedType) && !$parameterType->isBuiltin()) {
89
-				$resolveName = $parameterType->getName();
90
-			}
87
+            // try to find out if it is a class or a simple parameter
88
+            if ($parameterType !== null && ($parameterType instanceof ReflectionNamedType) && !$parameterType->isBuiltin()) {
89
+                $resolveName = $parameterType->getName();
90
+            }
91 91
 
92
-			try {
93
-				$builtIn = $parameterType !== null && ($parameterType instanceof ReflectionNamedType)
94
-							&& $parameterType->isBuiltin();
95
-				return $this->query($resolveName, !$builtIn, $chain);
96
-			} catch (ContainerExceptionInterface $e) {
97
-				// Service not found, use the default value when available
98
-				if ($parameter->isDefaultValueAvailable()) {
99
-					return $parameter->getDefaultValue();
100
-				}
92
+            try {
93
+                $builtIn = $parameterType !== null && ($parameterType instanceof ReflectionNamedType)
94
+                            && $parameterType->isBuiltin();
95
+                return $this->query($resolveName, !$builtIn, $chain);
96
+            } catch (ContainerExceptionInterface $e) {
97
+                // Service not found, use the default value when available
98
+                if ($parameter->isDefaultValueAvailable()) {
99
+                    return $parameter->getDefaultValue();
100
+                }
101 101
 
102
-				if ($parameterType !== null && ($parameterType instanceof ReflectionNamedType) && !$parameterType->isBuiltin()) {
103
-					$resolveName = $parameter->getName();
104
-					try {
105
-						return $this->query($resolveName, chain: $chain);
106
-					} catch (ContainerExceptionInterface $e2) {
107
-						// Pass null if typed and nullable
108
-						if ($parameter->allowsNull() && ($parameterType instanceof ReflectionNamedType)) {
109
-							return null;
110
-						}
102
+                if ($parameterType !== null && ($parameterType instanceof ReflectionNamedType) && !$parameterType->isBuiltin()) {
103
+                    $resolveName = $parameter->getName();
104
+                    try {
105
+                        return $this->query($resolveName, chain: $chain);
106
+                    } catch (ContainerExceptionInterface $e2) {
107
+                        // Pass null if typed and nullable
108
+                        if ($parameter->allowsNull() && ($parameterType instanceof ReflectionNamedType)) {
109
+                            return null;
110
+                        }
111 111
 
112
-						// don't lose the error we got while trying to query by type
113
-						throw new QueryException($e->getMessage(), (int)$e->getCode(), $e);
114
-					}
115
-				}
112
+                        // don't lose the error we got while trying to query by type
113
+                        throw new QueryException($e->getMessage(), (int)$e->getCode(), $e);
114
+                    }
115
+                }
116 116
 
117
-				throw $e;
118
-			}
119
-		}, $constructor->getParameters());
120
-	}
117
+                throw $e;
118
+            }
119
+        }, $constructor->getParameters());
120
+    }
121 121
 
122
-	/**
123
-	 * @inheritDoc
124
-	 * @param list<class-string> $chain
125
-	 */
126
-	public function resolve($name, array $chain = []) {
127
-		$baseMsg = 'Could not resolve ' . $name . '!';
128
-		try {
129
-			$class = new ReflectionClass($name);
130
-			if ($class->isInstantiable()) {
131
-				return $this->buildClass($class, $chain);
132
-			} else {
133
-				throw new QueryException($baseMsg
134
-					. ' Class can not be instantiated');
135
-			}
136
-		} catch (ReflectionException $e) {
137
-			// Class does not exist
138
-			throw new QueryNotFoundException($baseMsg . ' ' . $e->getMessage());
139
-		}
140
-	}
122
+    /**
123
+     * @inheritDoc
124
+     * @param list<class-string> $chain
125
+     */
126
+    public function resolve($name, array $chain = []) {
127
+        $baseMsg = 'Could not resolve ' . $name . '!';
128
+        try {
129
+            $class = new ReflectionClass($name);
130
+            if ($class->isInstantiable()) {
131
+                return $this->buildClass($class, $chain);
132
+            } else {
133
+                throw new QueryException($baseMsg
134
+                    . ' Class can not be instantiated');
135
+            }
136
+        } catch (ReflectionException $e) {
137
+            // Class does not exist
138
+            throw new QueryNotFoundException($baseMsg . ' ' . $e->getMessage());
139
+        }
140
+    }
141 141
 
142
-	/**
143
-	 * @inheritDoc
144
-	 * @param list<class-string> $chain
145
-	 */
146
-	public function query(string $name, bool $autoload = true, array $chain = []) {
147
-		$name = $this->sanitizeName($name);
148
-		if (isset($this->container[$name])) {
149
-			return $this->container[$name];
150
-		}
142
+    /**
143
+     * @inheritDoc
144
+     * @param list<class-string> $chain
145
+     */
146
+    public function query(string $name, bool $autoload = true, array $chain = []) {
147
+        $name = $this->sanitizeName($name);
148
+        if (isset($this->container[$name])) {
149
+            return $this->container[$name];
150
+        }
151 151
 
152
-		if ($autoload) {
153
-			if (in_array($name, $chain, true)) {
154
-				throw new RuntimeException('Tried to query ' . $name . ', but it is already in the chain: ' . implode(', ', $chain));
155
-			}
152
+        if ($autoload) {
153
+            if (in_array($name, $chain, true)) {
154
+                throw new RuntimeException('Tried to query ' . $name . ', but it is already in the chain: ' . implode(', ', $chain));
155
+            }
156 156
 
157
-			$object = $this->resolve($name, array_merge($chain, [$name]));
158
-			$this->registerService($name, function () use ($object) {
159
-				return $object;
160
-			});
161
-			return $object;
162
-		}
157
+            $object = $this->resolve($name, array_merge($chain, [$name]));
158
+            $this->registerService($name, function () use ($object) {
159
+                return $object;
160
+            });
161
+            return $object;
162
+        }
163 163
 
164
-		throw new QueryNotFoundException('Could not resolve ' . $name . '!');
165
-	}
164
+        throw new QueryNotFoundException('Could not resolve ' . $name . '!');
165
+    }
166 166
 
167
-	/**
168
-	 * @param string $name
169
-	 * @param mixed $value
170
-	 */
171
-	public function registerParameter($name, $value) {
172
-		$this[$name] = $value;
173
-	}
167
+    /**
168
+     * @param string $name
169
+     * @param mixed $value
170
+     */
171
+    public function registerParameter($name, $value) {
172
+        $this[$name] = $value;
173
+    }
174 174
 
175
-	/**
176
-	 * The given closure is call the first time the given service is queried.
177
-	 * The closure has to return the instance for the given service.
178
-	 * Created instance will be cached in case $shared is true.
179
-	 *
180
-	 * @param string $name name of the service to register another backend for
181
-	 * @param Closure $closure the closure to be called on service creation
182
-	 * @param bool $shared
183
-	 */
184
-	public function registerService($name, Closure $closure, $shared = true) {
185
-		$wrapped = function () use ($closure) {
186
-			return $closure($this);
187
-		};
188
-		$name = $this->sanitizeName($name);
189
-		if (isset($this->container[$name])) {
190
-			unset($this->container[$name]);
191
-		}
192
-		if ($shared) {
193
-			$this->container[$name] = $wrapped;
194
-		} else {
195
-			$this->container[$name] = $this->container->factory($wrapped);
196
-		}
197
-	}
175
+    /**
176
+     * The given closure is call the first time the given service is queried.
177
+     * The closure has to return the instance for the given service.
178
+     * Created instance will be cached in case $shared is true.
179
+     *
180
+     * @param string $name name of the service to register another backend for
181
+     * @param Closure $closure the closure to be called on service creation
182
+     * @param bool $shared
183
+     */
184
+    public function registerService($name, Closure $closure, $shared = true) {
185
+        $wrapped = function () use ($closure) {
186
+            return $closure($this);
187
+        };
188
+        $name = $this->sanitizeName($name);
189
+        if (isset($this->container[$name])) {
190
+            unset($this->container[$name]);
191
+        }
192
+        if ($shared) {
193
+            $this->container[$name] = $wrapped;
194
+        } else {
195
+            $this->container[$name] = $this->container->factory($wrapped);
196
+        }
197
+    }
198 198
 
199
-	/**
200
-	 * Shortcut for returning a service from a service under a different key,
201
-	 * e.g. to tell the container to return a class when queried for an
202
-	 * interface
203
-	 * @param string $alias the alias that should be registered
204
-	 * @param string $target the target that should be resolved instead
205
-	 */
206
-	public function registerAlias($alias, $target): void {
207
-		$this->registerService($alias, function (ContainerInterface $container) use ($target): mixed {
208
-			return $container->get($target);
209
-		}, false);
210
-	}
199
+    /**
200
+     * Shortcut for returning a service from a service under a different key,
201
+     * e.g. to tell the container to return a class when queried for an
202
+     * interface
203
+     * @param string $alias the alias that should be registered
204
+     * @param string $target the target that should be resolved instead
205
+     */
206
+    public function registerAlias($alias, $target): void {
207
+        $this->registerService($alias, function (ContainerInterface $container) use ($target): mixed {
208
+            return $container->get($target);
209
+        }, false);
210
+    }
211 211
 
212
-	protected function registerDeprecatedAlias(string $alias, string $target): void {
213
-		$this->registerService($alias, function (ContainerInterface $container) use ($target, $alias): mixed {
214
-			try {
215
-				$logger = $container->get(LoggerInterface::class);
216
-				$logger->debug('The requested alias "' . $alias . '" is deprecated. Please request "' . $target . '" directly. This alias will be removed in a future Nextcloud version.', [
217
-					'app' => $this->appName ?? 'serverDI',
218
-				]);
219
-			} catch (ContainerExceptionInterface $e) {
220
-				// Could not get logger. Continue
221
-			}
212
+    protected function registerDeprecatedAlias(string $alias, string $target): void {
213
+        $this->registerService($alias, function (ContainerInterface $container) use ($target, $alias): mixed {
214
+            try {
215
+                $logger = $container->get(LoggerInterface::class);
216
+                $logger->debug('The requested alias "' . $alias . '" is deprecated. Please request "' . $target . '" directly. This alias will be removed in a future Nextcloud version.', [
217
+                    'app' => $this->appName ?? 'serverDI',
218
+                ]);
219
+            } catch (ContainerExceptionInterface $e) {
220
+                // Could not get logger. Continue
221
+            }
222 222
 
223
-			return $container->get($target);
224
-		}, false);
225
-	}
223
+            return $container->get($target);
224
+        }, false);
225
+    }
226 226
 
227
-	/**
228
-	 * @param string $name
229
-	 * @return string
230
-	 */
231
-	protected function sanitizeName($name) {
232
-		if (isset($name[0]) && $name[0] === '\\') {
233
-			return ltrim($name, '\\');
234
-		}
235
-		return $name;
236
-	}
227
+    /**
228
+     * @param string $name
229
+     * @return string
230
+     */
231
+    protected function sanitizeName($name) {
232
+        if (isset($name[0]) && $name[0] === '\\') {
233
+            return ltrim($name, '\\');
234
+        }
235
+        return $name;
236
+    }
237 237
 
238
-	/**
239
-	 * @deprecated 20.0.0 use \Psr\Container\ContainerInterface::has
240
-	 */
241
-	public function offsetExists($id): bool {
242
-		return $this->container->offsetExists($id);
243
-	}
238
+    /**
239
+     * @deprecated 20.0.0 use \Psr\Container\ContainerInterface::has
240
+     */
241
+    public function offsetExists($id): bool {
242
+        return $this->container->offsetExists($id);
243
+    }
244 244
 
245
-	/**
246
-	 * @deprecated 20.0.0 use \Psr\Container\ContainerInterface::get
247
-	 * @return mixed
248
-	 */
249
-	#[\ReturnTypeWillChange]
250
-	public function offsetGet($id) {
251
-		return $this->container->offsetGet($id);
252
-	}
245
+    /**
246
+     * @deprecated 20.0.0 use \Psr\Container\ContainerInterface::get
247
+     * @return mixed
248
+     */
249
+    #[\ReturnTypeWillChange]
250
+    public function offsetGet($id) {
251
+        return $this->container->offsetGet($id);
252
+    }
253 253
 
254
-	/**
255
-	 * @deprecated 20.0.0 use \OCP\IContainer::registerService
256
-	 */
257
-	public function offsetSet($offset, $value): void {
258
-		$this->container->offsetSet($offset, $value);
259
-	}
254
+    /**
255
+     * @deprecated 20.0.0 use \OCP\IContainer::registerService
256
+     */
257
+    public function offsetSet($offset, $value): void {
258
+        $this->container->offsetSet($offset, $value);
259
+    }
260 260
 
261
-	/**
262
-	 * @deprecated 20.0.0
263
-	 */
264
-	public function offsetUnset($offset): void {
265
-		$this->container->offsetUnset($offset);
266
-	}
261
+    /**
262
+     * @deprecated 20.0.0
263
+     */
264
+    public function offsetUnset($offset): void {
265
+        $this->container->offsetUnset($offset);
266
+    }
267 267
 }
Please login to merge, or discard this patch.
Spacing   +12 added lines, -12 removed lines patch added patch discarded remove patch
@@ -66,7 +66,7 @@  discard block
 block discarded – undo
66 66
 		if (PHP_VERSION_ID >= 80400 && self::$useLazyObjects && !$class->isInternal()) {
67 67
 			/* For PHP>=8.4, use a lazy ghost to delay constructor and dependency resolving */
68 68
 			/** @psalm-suppress UndefinedMethod */
69
-			return $class->newLazyGhost(function (object $object) use ($constructor, $chain): void {
69
+			return $class->newLazyGhost(function(object $object) use ($constructor, $chain): void {
70 70
 				/** @psalm-suppress DirectConstructorCall For lazy ghosts we have to call the constructor directly */
71 71
 				$object->__construct(...$this->buildClassConstructorParameters($constructor, $chain));
72 72
 			});
@@ -79,7 +79,7 @@  discard block
 block discarded – undo
79 79
 	 * @param list<class-string> $chain
80 80
 	 */
81 81
 	private function buildClassConstructorParameters(\ReflectionMethod $constructor, array $chain): array {
82
-		return array_map(function (ReflectionParameter $parameter) use ($chain) {
82
+		return array_map(function(ReflectionParameter $parameter) use ($chain) {
83 83
 			$parameterType = $parameter->getType();
84 84
 
85 85
 			$resolveName = $parameter->getName();
@@ -110,7 +110,7 @@  discard block
 block discarded – undo
110 110
 						}
111 111
 
112 112
 						// don't lose the error we got while trying to query by type
113
-						throw new QueryException($e->getMessage(), (int)$e->getCode(), $e);
113
+						throw new QueryException($e->getMessage(), (int) $e->getCode(), $e);
114 114
 					}
115 115
 				}
116 116
 
@@ -124,7 +124,7 @@  discard block
 block discarded – undo
124 124
 	 * @param list<class-string> $chain
125 125
 	 */
126 126
 	public function resolve($name, array $chain = []) {
127
-		$baseMsg = 'Could not resolve ' . $name . '!';
127
+		$baseMsg = 'Could not resolve '.$name.'!';
128 128
 		try {
129 129
 			$class = new ReflectionClass($name);
130 130
 			if ($class->isInstantiable()) {
@@ -135,7 +135,7 @@  discard block
 block discarded – undo
135 135
 			}
136 136
 		} catch (ReflectionException $e) {
137 137
 			// Class does not exist
138
-			throw new QueryNotFoundException($baseMsg . ' ' . $e->getMessage());
138
+			throw new QueryNotFoundException($baseMsg.' '.$e->getMessage());
139 139
 		}
140 140
 	}
141 141
 
@@ -151,17 +151,17 @@  discard block
 block discarded – undo
151 151
 
152 152
 		if ($autoload) {
153 153
 			if (in_array($name, $chain, true)) {
154
-				throw new RuntimeException('Tried to query ' . $name . ', but it is already in the chain: ' . implode(', ', $chain));
154
+				throw new RuntimeException('Tried to query '.$name.', but it is already in the chain: '.implode(', ', $chain));
155 155
 			}
156 156
 
157 157
 			$object = $this->resolve($name, array_merge($chain, [$name]));
158
-			$this->registerService($name, function () use ($object) {
158
+			$this->registerService($name, function() use ($object) {
159 159
 				return $object;
160 160
 			});
161 161
 			return $object;
162 162
 		}
163 163
 
164
-		throw new QueryNotFoundException('Could not resolve ' . $name . '!');
164
+		throw new QueryNotFoundException('Could not resolve '.$name.'!');
165 165
 	}
166 166
 
167 167
 	/**
@@ -182,7 +182,7 @@  discard block
 block discarded – undo
182 182
 	 * @param bool $shared
183 183
 	 */
184 184
 	public function registerService($name, Closure $closure, $shared = true) {
185
-		$wrapped = function () use ($closure) {
185
+		$wrapped = function() use ($closure) {
186 186
 			return $closure($this);
187 187
 		};
188 188
 		$name = $this->sanitizeName($name);
@@ -204,16 +204,16 @@  discard block
 block discarded – undo
204 204
 	 * @param string $target the target that should be resolved instead
205 205
 	 */
206 206
 	public function registerAlias($alias, $target): void {
207
-		$this->registerService($alias, function (ContainerInterface $container) use ($target): mixed {
207
+		$this->registerService($alias, function(ContainerInterface $container) use ($target): mixed {
208 208
 			return $container->get($target);
209 209
 		}, false);
210 210
 	}
211 211
 
212 212
 	protected function registerDeprecatedAlias(string $alias, string $target): void {
213
-		$this->registerService($alias, function (ContainerInterface $container) use ($target, $alias): mixed {
213
+		$this->registerService($alias, function(ContainerInterface $container) use ($target, $alias): mixed {
214 214
 			try {
215 215
 				$logger = $container->get(LoggerInterface::class);
216
-				$logger->debug('The requested alias "' . $alias . '" is deprecated. Please request "' . $target . '" directly. This alias will be removed in a future Nextcloud version.', [
216
+				$logger->debug('The requested alias "'.$alias.'" is deprecated. Please request "'.$target.'" directly. This alias will be removed in a future Nextcloud version.', [
217 217
 					'app' => $this->appName ?? 'serverDI',
218 218
 				]);
219 219
 			} catch (ContainerExceptionInterface $e) {
Please login to merge, or discard this patch.
lib/private/ServerContainer.php 1 patch
Indentation   +151 added lines, -151 removed lines patch added patch discarded remove patch
@@ -21,155 +21,155 @@
 block discarded – undo
21 21
  * @package OC
22 22
  */
23 23
 class ServerContainer extends SimpleContainer {
24
-	/** @var DIContainer[] */
25
-	protected $appContainers;
26
-
27
-	/** @var string[] */
28
-	protected $hasNoAppContainer;
29
-
30
-	/** @var string[] */
31
-	protected $namespaces;
32
-
33
-	/**
34
-	 * ServerContainer constructor.
35
-	 */
36
-	public function __construct() {
37
-		parent::__construct();
38
-		$this->appContainers = [];
39
-		$this->namespaces = [];
40
-		$this->hasNoAppContainer = [];
41
-	}
42
-
43
-	/**
44
-	 * @param string $appName
45
-	 * @param string $appNamespace
46
-	 */
47
-	public function registerNamespace(string $appName, string $appNamespace): void {
48
-		// Cut of OCA\ and lowercase
49
-		$appNamespace = strtolower(substr($appNamespace, strrpos($appNamespace, '\\') + 1));
50
-		$this->namespaces[$appNamespace] = $appName;
51
-	}
52
-
53
-	/**
54
-	 * @param string $appName
55
-	 * @param DIContainer $container
56
-	 */
57
-	public function registerAppContainer(string $appName, DIContainer $container): void {
58
-		$this->appContainers[strtolower(App::buildAppNamespace($appName, ''))] = $container;
59
-	}
60
-
61
-	/**
62
-	 * @param string $appName
63
-	 * @return DIContainer
64
-	 * @throws QueryException
65
-	 */
66
-	public function getRegisteredAppContainer(string $appName): DIContainer {
67
-		if (isset($this->appContainers[strtolower(App::buildAppNamespace($appName, ''))])) {
68
-			return $this->appContainers[strtolower(App::buildAppNamespace($appName, ''))];
69
-		}
70
-
71
-		throw new QueryException();
72
-	}
73
-
74
-	/**
75
-	 * @param string $namespace
76
-	 * @param string $sensitiveNamespace
77
-	 * @return DIContainer
78
-	 * @throws QueryException
79
-	 */
80
-	protected function getAppContainer(string $namespace, string $sensitiveNamespace): DIContainer {
81
-		if (isset($this->appContainers[$namespace])) {
82
-			return $this->appContainers[$namespace];
83
-		}
84
-
85
-		if (isset($this->namespaces[$namespace])) {
86
-			if (!isset($this->hasNoAppContainer[$namespace])) {
87
-				$applicationClassName = 'OCA\\' . $sensitiveNamespace . '\\AppInfo\\Application';
88
-				if (class_exists($applicationClassName)) {
89
-					$app = new $applicationClassName();
90
-					if (isset($this->appContainers[$namespace])) {
91
-						$this->appContainers[$namespace]->offsetSet($applicationClassName, $app);
92
-						return $this->appContainers[$namespace];
93
-					}
94
-				}
95
-				$this->hasNoAppContainer[$namespace] = true;
96
-			}
97
-
98
-			return new DIContainer($this->namespaces[$namespace]);
99
-		}
100
-		throw new QueryException();
101
-	}
102
-
103
-	public function has($id, bool $noRecursion = false): bool {
104
-		if (!$noRecursion && ($appContainer = $this->getAppContainerForService($id)) !== null) {
105
-			return $appContainer->has($id);
106
-		}
107
-
108
-		return parent::has($id);
109
-	}
110
-
111
-	/**
112
-	 * @template T
113
-	 * @param class-string<T>|string $name
114
-	 * @param list<class-string> $chain
115
-	 * @return T|mixed
116
-	 * @psalm-template S as class-string<T>|string
117
-	 * @psalm-param S $name
118
-	 * @psalm-return (S is class-string<T> ? T : mixed)
119
-	 * @throws QueryException
120
-	 * @deprecated 20.0.0 use \Psr\Container\ContainerInterface::get
121
-	 */
122
-	public function query(string $name, bool $autoload = true, array $chain = []) {
123
-		$name = $this->sanitizeName($name);
124
-
125
-		if (str_starts_with($name, 'OCA\\')) {
126
-			// Skip server container query for app namespace classes
127
-			try {
128
-				return parent::query($name, false, $chain);
129
-			} catch (QueryException $e) {
130
-				// Continue with general autoloading then
131
-			}
132
-			// In case the service starts with OCA\ we try to find the service in
133
-			// the apps container first.
134
-			if (($appContainer = $this->getAppContainerForService($name)) !== null) {
135
-				try {
136
-					return $appContainer->queryNoFallback($name, $chain);
137
-				} catch (QueryException $e) {
138
-					// Didn't find the service or the respective app container
139
-					// In this case the service won't be part of the core container,
140
-					// so we can throw directly
141
-					throw $e;
142
-				}
143
-			}
144
-		} elseif (str_starts_with($name, 'OC\\Settings\\') && substr_count($name, '\\') >= 3) {
145
-			$segments = explode('\\', $name);
146
-			try {
147
-				$appContainer = $this->getAppContainer(strtolower($segments[1]), $segments[1]);
148
-				return $appContainer->queryNoFallback($name, $chain);
149
-			} catch (QueryException $e) {
150
-				// Didn't find the service or the respective app container,
151
-				// ignore it and fall back to the core container.
152
-			}
153
-		}
154
-
155
-		return parent::query($name, $autoload, $chain);
156
-	}
157
-
158
-	/**
159
-	 * @internal
160
-	 * @param string $id
161
-	 * @return DIContainer|null
162
-	 */
163
-	public function getAppContainerForService(string $id): ?DIContainer {
164
-		if (!str_starts_with($id, 'OCA\\') || substr_count($id, '\\') < 2) {
165
-			return null;
166
-		}
167
-
168
-		try {
169
-			[,$namespace,] = explode('\\', $id);
170
-			return $this->getAppContainer(strtolower($namespace), $namespace);
171
-		} catch (QueryException $e) {
172
-			return null;
173
-		}
174
-	}
24
+    /** @var DIContainer[] */
25
+    protected $appContainers;
26
+
27
+    /** @var string[] */
28
+    protected $hasNoAppContainer;
29
+
30
+    /** @var string[] */
31
+    protected $namespaces;
32
+
33
+    /**
34
+     * ServerContainer constructor.
35
+     */
36
+    public function __construct() {
37
+        parent::__construct();
38
+        $this->appContainers = [];
39
+        $this->namespaces = [];
40
+        $this->hasNoAppContainer = [];
41
+    }
42
+
43
+    /**
44
+     * @param string $appName
45
+     * @param string $appNamespace
46
+     */
47
+    public function registerNamespace(string $appName, string $appNamespace): void {
48
+        // Cut of OCA\ and lowercase
49
+        $appNamespace = strtolower(substr($appNamespace, strrpos($appNamespace, '\\') + 1));
50
+        $this->namespaces[$appNamespace] = $appName;
51
+    }
52
+
53
+    /**
54
+     * @param string $appName
55
+     * @param DIContainer $container
56
+     */
57
+    public function registerAppContainer(string $appName, DIContainer $container): void {
58
+        $this->appContainers[strtolower(App::buildAppNamespace($appName, ''))] = $container;
59
+    }
60
+
61
+    /**
62
+     * @param string $appName
63
+     * @return DIContainer
64
+     * @throws QueryException
65
+     */
66
+    public function getRegisteredAppContainer(string $appName): DIContainer {
67
+        if (isset($this->appContainers[strtolower(App::buildAppNamespace($appName, ''))])) {
68
+            return $this->appContainers[strtolower(App::buildAppNamespace($appName, ''))];
69
+        }
70
+
71
+        throw new QueryException();
72
+    }
73
+
74
+    /**
75
+     * @param string $namespace
76
+     * @param string $sensitiveNamespace
77
+     * @return DIContainer
78
+     * @throws QueryException
79
+     */
80
+    protected function getAppContainer(string $namespace, string $sensitiveNamespace): DIContainer {
81
+        if (isset($this->appContainers[$namespace])) {
82
+            return $this->appContainers[$namespace];
83
+        }
84
+
85
+        if (isset($this->namespaces[$namespace])) {
86
+            if (!isset($this->hasNoAppContainer[$namespace])) {
87
+                $applicationClassName = 'OCA\\' . $sensitiveNamespace . '\\AppInfo\\Application';
88
+                if (class_exists($applicationClassName)) {
89
+                    $app = new $applicationClassName();
90
+                    if (isset($this->appContainers[$namespace])) {
91
+                        $this->appContainers[$namespace]->offsetSet($applicationClassName, $app);
92
+                        return $this->appContainers[$namespace];
93
+                    }
94
+                }
95
+                $this->hasNoAppContainer[$namespace] = true;
96
+            }
97
+
98
+            return new DIContainer($this->namespaces[$namespace]);
99
+        }
100
+        throw new QueryException();
101
+    }
102
+
103
+    public function has($id, bool $noRecursion = false): bool {
104
+        if (!$noRecursion && ($appContainer = $this->getAppContainerForService($id)) !== null) {
105
+            return $appContainer->has($id);
106
+        }
107
+
108
+        return parent::has($id);
109
+    }
110
+
111
+    /**
112
+     * @template T
113
+     * @param class-string<T>|string $name
114
+     * @param list<class-string> $chain
115
+     * @return T|mixed
116
+     * @psalm-template S as class-string<T>|string
117
+     * @psalm-param S $name
118
+     * @psalm-return (S is class-string<T> ? T : mixed)
119
+     * @throws QueryException
120
+     * @deprecated 20.0.0 use \Psr\Container\ContainerInterface::get
121
+     */
122
+    public function query(string $name, bool $autoload = true, array $chain = []) {
123
+        $name = $this->sanitizeName($name);
124
+
125
+        if (str_starts_with($name, 'OCA\\')) {
126
+            // Skip server container query for app namespace classes
127
+            try {
128
+                return parent::query($name, false, $chain);
129
+            } catch (QueryException $e) {
130
+                // Continue with general autoloading then
131
+            }
132
+            // In case the service starts with OCA\ we try to find the service in
133
+            // the apps container first.
134
+            if (($appContainer = $this->getAppContainerForService($name)) !== null) {
135
+                try {
136
+                    return $appContainer->queryNoFallback($name, $chain);
137
+                } catch (QueryException $e) {
138
+                    // Didn't find the service or the respective app container
139
+                    // In this case the service won't be part of the core container,
140
+                    // so we can throw directly
141
+                    throw $e;
142
+                }
143
+            }
144
+        } elseif (str_starts_with($name, 'OC\\Settings\\') && substr_count($name, '\\') >= 3) {
145
+            $segments = explode('\\', $name);
146
+            try {
147
+                $appContainer = $this->getAppContainer(strtolower($segments[1]), $segments[1]);
148
+                return $appContainer->queryNoFallback($name, $chain);
149
+            } catch (QueryException $e) {
150
+                // Didn't find the service or the respective app container,
151
+                // ignore it and fall back to the core container.
152
+            }
153
+        }
154
+
155
+        return parent::query($name, $autoload, $chain);
156
+    }
157
+
158
+    /**
159
+     * @internal
160
+     * @param string $id
161
+     * @return DIContainer|null
162
+     */
163
+    public function getAppContainerForService(string $id): ?DIContainer {
164
+        if (!str_starts_with($id, 'OCA\\') || substr_count($id, '\\') < 2) {
165
+            return null;
166
+        }
167
+
168
+        try {
169
+            [,$namespace,] = explode('\\', $id);
170
+            return $this->getAppContainer(strtolower($namespace), $namespace);
171
+        } catch (QueryException $e) {
172
+            return null;
173
+        }
174
+    }
175 175
 }
Please login to merge, or discard this patch.