Completed
Pull Request — master (#7870)
by Joas
22:20
created
lib/private/AppFramework/DependencyInjection/DIContainer.php 2 patches
Indentation   +383 added lines, -383 removed lines patch added patch discarded remove patch
@@ -65,387 +65,387 @@
 block discarded – undo
65 65
 
66 66
 class DIContainer extends SimpleContainer implements IAppContainer {
67 67
 
68
-	/**
69
-	 * @var array
70
-	 */
71
-	private $middleWares = array();
72
-
73
-	/** @var ServerContainer */
74
-	private $server;
75
-
76
-	/**
77
-	 * Put your class dependencies in here
78
-	 * @param string $appName the name of the app
79
-	 * @param array $urlParams
80
-	 * @param ServerContainer|null $server
81
-	 */
82
-	public function __construct($appName, $urlParams = array(), ServerContainer $server = null){
83
-		parent::__construct();
84
-		$this['AppName'] = $appName;
85
-		$this['urlParams'] = $urlParams;
86
-
87
-		/** @var \OC\ServerContainer $server */
88
-		if ($server === null) {
89
-			$server = \OC::$server;
90
-		}
91
-		$this->server = $server;
92
-		$this->server->registerAppContainer($appName, $this);
93
-
94
-		// aliases
95
-		$this->registerAlias('appName', 'AppName');
96
-		$this->registerAlias('webRoot', 'WebRoot');
97
-		$this->registerAlias('userId', 'UserId');
98
-
99
-		/**
100
-		 * Core services
101
-		 */
102
-		$this->registerService(IOutput::class, function($c){
103
-			return new Output($this->getServer()->getWebRoot());
104
-		});
105
-
106
-		$this->registerService(Folder::class, function() {
107
-			return $this->getServer()->getUserFolder();
108
-		});
109
-
110
-		$this->registerService(IAppData::class, function (SimpleContainer $c) {
111
-			return $this->getServer()->getAppDataDir($c->query('AppName'));
112
-		});
113
-
114
-		$this->registerService(IL10N::class, function($c) {
115
-			return $this->getServer()->getL10N($c->query('AppName'));
116
-		});
117
-
118
-		$this->registerAlias(\OCP\AppFramework\Utility\IControllerMethodReflector::class, \OC\AppFramework\Utility\ControllerMethodReflector::class);
119
-		$this->registerAlias('ControllerMethodReflector', \OCP\AppFramework\Utility\IControllerMethodReflector::class);
120
-
121
-		$this->registerService(IRequest::class, function() {
122
-			return $this->getServer()->query(IRequest::class);
123
-		});
124
-		$this->registerAlias('Request', IRequest::class);
125
-
126
-		$this->registerAlias(\OCP\AppFramework\Utility\ITimeFactory::class, \OC\AppFramework\Utility\TimeFactory::class);
127
-		$this->registerAlias('TimeFactory', \OCP\AppFramework\Utility\ITimeFactory::class);
128
-
129
-		$this->registerAlias(\OC\User\Session::class, \OCP\IUserSession::class);
130
-
131
-		$this->registerService(IServerContainer::class, function ($c) {
132
-			return $this->getServer();
133
-		});
134
-		$this->registerAlias('ServerContainer', IServerContainer::class);
135
-
136
-		$this->registerService(\OCP\WorkflowEngine\IManager::class, function ($c) {
137
-			return $c->query('OCA\WorkflowEngine\Manager');
138
-		});
139
-
140
-		$this->registerService(\OCP\AppFramework\IAppContainer::class, function ($c) {
141
-			return $c;
142
-		});
143
-
144
-		// commonly used attributes
145
-		$this->registerService('UserId', function ($c) {
146
-			return $c->query('OCP\\IUserSession')->getSession()->get('user_id');
147
-		});
148
-
149
-		$this->registerService('WebRoot', function ($c) {
150
-			return $c->query('ServerContainer')->getWebRoot();
151
-		});
152
-
153
-		$this->registerService('fromMailAddress', function() {
154
-			return Util::getDefaultEmailAddress('no-reply');
155
-		});
156
-
157
-		$this->registerService('OC_Defaults', function ($c) {
158
-			return $c->getServer()->getThemingDefaults();
159
-		});
160
-
161
-		$this->registerService('OCP\Encryption\IManager', function ($c) {
162
-			return $this->getServer()->getEncryptionManager();
163
-		});
164
-
165
-		$this->registerService(IConfig::class, function ($c) {
166
-			return $c->query(OC\GlobalScale\Config::class);
167
-		});
168
-
169
-		$this->registerService(IValidator::class, function($c) {
170
-			return $c->query(Validator::class);
171
-		});
172
-
173
-		$this->registerService(\OC\Security\IdentityProof\Manager::class, function ($c) {
174
-			return new \OC\Security\IdentityProof\Manager(
175
-				$this->getServer()->query(\OC\Files\AppData\Factory::class),
176
-				$this->getServer()->getCrypto(),
177
-				$this->getServer()->getConfig()
178
-			);
179
-		});
180
-
181
-		$this->registerService('Protocol', function($c){
182
-			/** @var \OC\Server $server */
183
-			$server = $c->query('ServerContainer');
184
-			$protocol = $server->getRequest()->getHttpProtocol();
185
-			return new Http($_SERVER, $protocol);
186
-		});
187
-
188
-		$this->registerService('Dispatcher', function($c) {
189
-			return new Dispatcher(
190
-				$c['Protocol'],
191
-				$c['MiddlewareDispatcher'],
192
-				$c['ControllerMethodReflector'],
193
-				$c['Request']
194
-			);
195
-		});
196
-
197
-		/**
198
-		 * App Framework default arguments
199
-		 */
200
-		$this->registerParameter('corsMethods', 'PUT, POST, GET, DELETE, PATCH');
201
-		$this->registerParameter('corsAllowedHeaders', 'Authorization, Content-Type, Accept');
202
-		$this->registerParameter('corsMaxAge', 1728000);
203
-
204
-		/**
205
-		 * Middleware
206
-		 */
207
-		$app = $this;
208
-		$this->registerService('SecurityMiddleware', function($c) use ($app){
209
-			/** @var \OC\Server $server */
210
-			$server = $app->getServer();
211
-
212
-			return new SecurityMiddleware(
213
-				$c['Request'],
214
-				$c['ControllerMethodReflector'],
215
-				$server->getNavigationManager(),
216
-				$server->getURLGenerator(),
217
-				$server->getLogger(),
218
-				$c['AppName'],
219
-				$server->getUserSession()->isLoggedIn(),
220
-				$server->getGroupManager()->isAdmin($this->getUserId()),
221
-				$server->getContentSecurityPolicyManager(),
222
-				$server->getCsrfTokenManager(),
223
-				$server->getContentSecurityPolicyNonceManager(),
224
-				$server->getAppManager()
225
-			);
226
-		});
227
-
228
-		$this->registerService(OC\AppFramework\Middleware\Security\PasswordConfirmationMiddleware::class, function ($c) use ($app) {
229
-			/** @var \OC\Server $server */
230
-			$server = $app->getServer();
231
-
232
-			return new OC\AppFramework\Middleware\Security\PasswordConfirmationMiddleware(
233
-				$c['ControllerMethodReflector'],
234
-				$server->getSession(),
235
-				$server->getUserSession(),
236
-				$server->query(ITimeFactory::class)
237
-			);
238
-		});
239
-
240
-		$this->registerService('BruteForceMiddleware', function($c) use ($app) {
241
-			/** @var \OC\Server $server */
242
-			$server = $app->getServer();
243
-
244
-			return new OC\AppFramework\Middleware\Security\BruteForceMiddleware(
245
-				$c['ControllerMethodReflector'],
246
-				$server->getBruteForceThrottler(),
247
-				$server->getRequest()
248
-			);
249
-		});
250
-
251
-		$this->registerService('RateLimitingMiddleware', function($c) use ($app) {
252
-			/** @var \OC\Server $server */
253
-			$server = $app->getServer();
254
-
255
-			return new RateLimitingMiddleware(
256
-				$server->getRequest(),
257
-				$server->getUserSession(),
258
-				$c['ControllerMethodReflector'],
259
-				$c->query(OC\Security\RateLimiting\Limiter::class)
260
-			);
261
-		});
262
-
263
-		$this->registerService('CORSMiddleware', function($c) {
264
-			return new CORSMiddleware(
265
-				$c['Request'],
266
-				$c['ControllerMethodReflector'],
267
-				$c->query(IUserSession::class),
268
-				$c->getServer()->getBruteForceThrottler()
269
-			);
270
-		});
271
-
272
-		$this->registerService('SessionMiddleware', function($c) use ($app) {
273
-			return new SessionMiddleware(
274
-				$c['Request'],
275
-				$c['ControllerMethodReflector'],
276
-				$app->getServer()->getSession()
277
-			);
278
-		});
279
-
280
-		$this->registerService('TwoFactorMiddleware', function (SimpleContainer $c) use ($app) {
281
-			$twoFactorManager = $c->getServer()->getTwoFactorAuthManager();
282
-			$userSession = $app->getServer()->getUserSession();
283
-			$session = $app->getServer()->getSession();
284
-			$urlGenerator = $app->getServer()->getURLGenerator();
285
-			$reflector = $c['ControllerMethodReflector'];
286
-			$request = $app->getServer()->getRequest();
287
-			return new TwoFactorMiddleware($twoFactorManager, $userSession, $session, $urlGenerator, $reflector, $request);
288
-		});
289
-
290
-		$this->registerService('OCSMiddleware', function (SimpleContainer $c) {
291
-			return new OCSMiddleware(
292
-				$c['Request']
293
-			);
294
-		});
295
-
296
-		$this->registerService(OC\AppFramework\Middleware\Security\SameSiteCookieMiddleware::class, function (SimpleContainer $c) {
297
-			return new OC\AppFramework\Middleware\Security\SameSiteCookieMiddleware(
298
-				$c['Request'],
299
-				$c['ControllerMethodReflector']
300
-			);
301
-		});
302
-
303
-		$middleWares = &$this->middleWares;
304
-		$this->registerService('MiddlewareDispatcher', function($c) use (&$middleWares) {
305
-			$dispatcher = new MiddlewareDispatcher();
306
-			$dispatcher->registerMiddleware($c[OC\AppFramework\Middleware\Security\SameSiteCookieMiddleware::class]);
307
-			$dispatcher->registerMiddleware($c['CORSMiddleware']);
308
-			$dispatcher->registerMiddleware($c['OCSMiddleware']);
309
-			$dispatcher->registerMiddleware($c['SecurityMiddleware']);
310
-			$dispatcher->registerMiddleware($c[OC\AppFramework\Middleware\Security\PasswordConfirmationMiddleware::class]);
311
-			$dispatcher->registerMiddleware($c['TwoFactorMiddleware']);
312
-			$dispatcher->registerMiddleware($c['BruteForceMiddleware']);
313
-			$dispatcher->registerMiddleware($c['RateLimitingMiddleware']);
314
-
315
-			foreach($middleWares as $middleWare) {
316
-				$dispatcher->registerMiddleware($c[$middleWare]);
317
-			}
318
-
319
-			$dispatcher->registerMiddleware($c['SessionMiddleware']);
320
-			return $dispatcher;
321
-		});
322
-
323
-	}
324
-
325
-	/**
326
-	 * @return \OCP\IServerContainer
327
-	 */
328
-	public function getServer()
329
-	{
330
-		return $this->server;
331
-	}
332
-
333
-	/**
334
-	 * @param string $middleWare
335
-	 * @return boolean|null
336
-	 */
337
-	public function registerMiddleWare($middleWare) {
338
-		array_push($this->middleWares, $middleWare);
339
-	}
340
-
341
-	/**
342
-	 * used to return the appname of the set application
343
-	 * @return string the name of your application
344
-	 */
345
-	public function getAppName() {
346
-		return $this->query('AppName');
347
-	}
348
-
349
-	/**
350
-	 * @deprecated use IUserSession->isLoggedIn()
351
-	 * @return boolean
352
-	 */
353
-	public function isLoggedIn() {
354
-		return \OC::$server->getUserSession()->isLoggedIn();
355
-	}
356
-
357
-	/**
358
-	 * @deprecated use IGroupManager->isAdmin($userId)
359
-	 * @return boolean
360
-	 */
361
-	public function isAdminUser() {
362
-		$uid = $this->getUserId();
363
-		return \OC_User::isAdminUser($uid);
364
-	}
365
-
366
-	private function getUserId() {
367
-		return $this->getServer()->getSession()->get('user_id');
368
-	}
369
-
370
-	/**
371
-	 * @deprecated use the ILogger instead
372
-	 * @param string $message
373
-	 * @param string $level
374
-	 * @return mixed
375
-	 */
376
-	public function log($message, $level) {
377
-		switch($level){
378
-			case 'debug':
379
-				$level = \OCP\Util::DEBUG;
380
-				break;
381
-			case 'info':
382
-				$level = \OCP\Util::INFO;
383
-				break;
384
-			case 'warn':
385
-				$level = \OCP\Util::WARN;
386
-				break;
387
-			case 'fatal':
388
-				$level = \OCP\Util::FATAL;
389
-				break;
390
-			default:
391
-				$level = \OCP\Util::ERROR;
392
-				break;
393
-		}
394
-		\OCP\Util::writeLog($this->getAppName(), $message, $level);
395
-	}
396
-
397
-	/**
398
-	 * Register a capability
399
-	 *
400
-	 * @param string $serviceName e.g. 'OCA\Files\Capabilities'
401
-	 */
402
-	public function registerCapability($serviceName) {
403
-		$this->query('OC\CapabilitiesManager')->registerCapability(function() use ($serviceName) {
404
-			return $this->query($serviceName);
405
-		});
406
-	}
407
-
408
-	/**
409
-	 * @param string $name
410
-	 * @return mixed
411
-	 * @throws QueryException if the query could not be resolved
412
-	 */
413
-	public function query($name) {
414
-		try {
415
-			return $this->queryNoFallback($name);
416
-		} catch (QueryException $firstException) {
417
-			try {
418
-				return $this->getServer()->query($name);
419
-			} catch (QueryException $secondException) {
420
-				if ($firstException->getCode() === 1) {
421
-					throw $secondException;
422
-				}
423
-				throw $firstException;
424
-			}
425
-		}
426
-	}
427
-
428
-	/**
429
-	 * @param string $name
430
-	 * @return mixed
431
-	 * @throws QueryException if the query could not be resolved
432
-	 */
433
-	public function queryNoFallback($name) {
434
-		$name = $this->sanitizeName($name);
435
-
436
-		if ($this->offsetExists($name)) {
437
-			return parent::query($name);
438
-		} else {
439
-			if ($this['AppName'] === 'settings' && strpos($name, 'OC\\Settings\\') === 0) {
440
-				return parent::query($name);
441
-			} else if ($this['AppName'] === 'core' && strpos($name, 'OC\\Core\\') === 0) {
442
-				return parent::query($name);
443
-			} else if (strpos($name, \OC\AppFramework\App::buildAppNamespace($this['AppName']) . '\\') === 0) {
444
-				return parent::query($name);
445
-			}
446
-		}
447
-
448
-		throw new QueryException('Could not resolve ' . $name . '!' .
449
-			' Class can not be instantiated', 1);
450
-	}
68
+    /**
69
+     * @var array
70
+     */
71
+    private $middleWares = array();
72
+
73
+    /** @var ServerContainer */
74
+    private $server;
75
+
76
+    /**
77
+     * Put your class dependencies in here
78
+     * @param string $appName the name of the app
79
+     * @param array $urlParams
80
+     * @param ServerContainer|null $server
81
+     */
82
+    public function __construct($appName, $urlParams = array(), ServerContainer $server = null){
83
+        parent::__construct();
84
+        $this['AppName'] = $appName;
85
+        $this['urlParams'] = $urlParams;
86
+
87
+        /** @var \OC\ServerContainer $server */
88
+        if ($server === null) {
89
+            $server = \OC::$server;
90
+        }
91
+        $this->server = $server;
92
+        $this->server->registerAppContainer($appName, $this);
93
+
94
+        // aliases
95
+        $this->registerAlias('appName', 'AppName');
96
+        $this->registerAlias('webRoot', 'WebRoot');
97
+        $this->registerAlias('userId', 'UserId');
98
+
99
+        /**
100
+         * Core services
101
+         */
102
+        $this->registerService(IOutput::class, function($c){
103
+            return new Output($this->getServer()->getWebRoot());
104
+        });
105
+
106
+        $this->registerService(Folder::class, function() {
107
+            return $this->getServer()->getUserFolder();
108
+        });
109
+
110
+        $this->registerService(IAppData::class, function (SimpleContainer $c) {
111
+            return $this->getServer()->getAppDataDir($c->query('AppName'));
112
+        });
113
+
114
+        $this->registerService(IL10N::class, function($c) {
115
+            return $this->getServer()->getL10N($c->query('AppName'));
116
+        });
117
+
118
+        $this->registerAlias(\OCP\AppFramework\Utility\IControllerMethodReflector::class, \OC\AppFramework\Utility\ControllerMethodReflector::class);
119
+        $this->registerAlias('ControllerMethodReflector', \OCP\AppFramework\Utility\IControllerMethodReflector::class);
120
+
121
+        $this->registerService(IRequest::class, function() {
122
+            return $this->getServer()->query(IRequest::class);
123
+        });
124
+        $this->registerAlias('Request', IRequest::class);
125
+
126
+        $this->registerAlias(\OCP\AppFramework\Utility\ITimeFactory::class, \OC\AppFramework\Utility\TimeFactory::class);
127
+        $this->registerAlias('TimeFactory', \OCP\AppFramework\Utility\ITimeFactory::class);
128
+
129
+        $this->registerAlias(\OC\User\Session::class, \OCP\IUserSession::class);
130
+
131
+        $this->registerService(IServerContainer::class, function ($c) {
132
+            return $this->getServer();
133
+        });
134
+        $this->registerAlias('ServerContainer', IServerContainer::class);
135
+
136
+        $this->registerService(\OCP\WorkflowEngine\IManager::class, function ($c) {
137
+            return $c->query('OCA\WorkflowEngine\Manager');
138
+        });
139
+
140
+        $this->registerService(\OCP\AppFramework\IAppContainer::class, function ($c) {
141
+            return $c;
142
+        });
143
+
144
+        // commonly used attributes
145
+        $this->registerService('UserId', function ($c) {
146
+            return $c->query('OCP\\IUserSession')->getSession()->get('user_id');
147
+        });
148
+
149
+        $this->registerService('WebRoot', function ($c) {
150
+            return $c->query('ServerContainer')->getWebRoot();
151
+        });
152
+
153
+        $this->registerService('fromMailAddress', function() {
154
+            return Util::getDefaultEmailAddress('no-reply');
155
+        });
156
+
157
+        $this->registerService('OC_Defaults', function ($c) {
158
+            return $c->getServer()->getThemingDefaults();
159
+        });
160
+
161
+        $this->registerService('OCP\Encryption\IManager', function ($c) {
162
+            return $this->getServer()->getEncryptionManager();
163
+        });
164
+
165
+        $this->registerService(IConfig::class, function ($c) {
166
+            return $c->query(OC\GlobalScale\Config::class);
167
+        });
168
+
169
+        $this->registerService(IValidator::class, function($c) {
170
+            return $c->query(Validator::class);
171
+        });
172
+
173
+        $this->registerService(\OC\Security\IdentityProof\Manager::class, function ($c) {
174
+            return new \OC\Security\IdentityProof\Manager(
175
+                $this->getServer()->query(\OC\Files\AppData\Factory::class),
176
+                $this->getServer()->getCrypto(),
177
+                $this->getServer()->getConfig()
178
+            );
179
+        });
180
+
181
+        $this->registerService('Protocol', function($c){
182
+            /** @var \OC\Server $server */
183
+            $server = $c->query('ServerContainer');
184
+            $protocol = $server->getRequest()->getHttpProtocol();
185
+            return new Http($_SERVER, $protocol);
186
+        });
187
+
188
+        $this->registerService('Dispatcher', function($c) {
189
+            return new Dispatcher(
190
+                $c['Protocol'],
191
+                $c['MiddlewareDispatcher'],
192
+                $c['ControllerMethodReflector'],
193
+                $c['Request']
194
+            );
195
+        });
196
+
197
+        /**
198
+         * App Framework default arguments
199
+         */
200
+        $this->registerParameter('corsMethods', 'PUT, POST, GET, DELETE, PATCH');
201
+        $this->registerParameter('corsAllowedHeaders', 'Authorization, Content-Type, Accept');
202
+        $this->registerParameter('corsMaxAge', 1728000);
203
+
204
+        /**
205
+         * Middleware
206
+         */
207
+        $app = $this;
208
+        $this->registerService('SecurityMiddleware', function($c) use ($app){
209
+            /** @var \OC\Server $server */
210
+            $server = $app->getServer();
211
+
212
+            return new SecurityMiddleware(
213
+                $c['Request'],
214
+                $c['ControllerMethodReflector'],
215
+                $server->getNavigationManager(),
216
+                $server->getURLGenerator(),
217
+                $server->getLogger(),
218
+                $c['AppName'],
219
+                $server->getUserSession()->isLoggedIn(),
220
+                $server->getGroupManager()->isAdmin($this->getUserId()),
221
+                $server->getContentSecurityPolicyManager(),
222
+                $server->getCsrfTokenManager(),
223
+                $server->getContentSecurityPolicyNonceManager(),
224
+                $server->getAppManager()
225
+            );
226
+        });
227
+
228
+        $this->registerService(OC\AppFramework\Middleware\Security\PasswordConfirmationMiddleware::class, function ($c) use ($app) {
229
+            /** @var \OC\Server $server */
230
+            $server = $app->getServer();
231
+
232
+            return new OC\AppFramework\Middleware\Security\PasswordConfirmationMiddleware(
233
+                $c['ControllerMethodReflector'],
234
+                $server->getSession(),
235
+                $server->getUserSession(),
236
+                $server->query(ITimeFactory::class)
237
+            );
238
+        });
239
+
240
+        $this->registerService('BruteForceMiddleware', function($c) use ($app) {
241
+            /** @var \OC\Server $server */
242
+            $server = $app->getServer();
243
+
244
+            return new OC\AppFramework\Middleware\Security\BruteForceMiddleware(
245
+                $c['ControllerMethodReflector'],
246
+                $server->getBruteForceThrottler(),
247
+                $server->getRequest()
248
+            );
249
+        });
250
+
251
+        $this->registerService('RateLimitingMiddleware', function($c) use ($app) {
252
+            /** @var \OC\Server $server */
253
+            $server = $app->getServer();
254
+
255
+            return new RateLimitingMiddleware(
256
+                $server->getRequest(),
257
+                $server->getUserSession(),
258
+                $c['ControllerMethodReflector'],
259
+                $c->query(OC\Security\RateLimiting\Limiter::class)
260
+            );
261
+        });
262
+
263
+        $this->registerService('CORSMiddleware', function($c) {
264
+            return new CORSMiddleware(
265
+                $c['Request'],
266
+                $c['ControllerMethodReflector'],
267
+                $c->query(IUserSession::class),
268
+                $c->getServer()->getBruteForceThrottler()
269
+            );
270
+        });
271
+
272
+        $this->registerService('SessionMiddleware', function($c) use ($app) {
273
+            return new SessionMiddleware(
274
+                $c['Request'],
275
+                $c['ControllerMethodReflector'],
276
+                $app->getServer()->getSession()
277
+            );
278
+        });
279
+
280
+        $this->registerService('TwoFactorMiddleware', function (SimpleContainer $c) use ($app) {
281
+            $twoFactorManager = $c->getServer()->getTwoFactorAuthManager();
282
+            $userSession = $app->getServer()->getUserSession();
283
+            $session = $app->getServer()->getSession();
284
+            $urlGenerator = $app->getServer()->getURLGenerator();
285
+            $reflector = $c['ControllerMethodReflector'];
286
+            $request = $app->getServer()->getRequest();
287
+            return new TwoFactorMiddleware($twoFactorManager, $userSession, $session, $urlGenerator, $reflector, $request);
288
+        });
289
+
290
+        $this->registerService('OCSMiddleware', function (SimpleContainer $c) {
291
+            return new OCSMiddleware(
292
+                $c['Request']
293
+            );
294
+        });
295
+
296
+        $this->registerService(OC\AppFramework\Middleware\Security\SameSiteCookieMiddleware::class, function (SimpleContainer $c) {
297
+            return new OC\AppFramework\Middleware\Security\SameSiteCookieMiddleware(
298
+                $c['Request'],
299
+                $c['ControllerMethodReflector']
300
+            );
301
+        });
302
+
303
+        $middleWares = &$this->middleWares;
304
+        $this->registerService('MiddlewareDispatcher', function($c) use (&$middleWares) {
305
+            $dispatcher = new MiddlewareDispatcher();
306
+            $dispatcher->registerMiddleware($c[OC\AppFramework\Middleware\Security\SameSiteCookieMiddleware::class]);
307
+            $dispatcher->registerMiddleware($c['CORSMiddleware']);
308
+            $dispatcher->registerMiddleware($c['OCSMiddleware']);
309
+            $dispatcher->registerMiddleware($c['SecurityMiddleware']);
310
+            $dispatcher->registerMiddleware($c[OC\AppFramework\Middleware\Security\PasswordConfirmationMiddleware::class]);
311
+            $dispatcher->registerMiddleware($c['TwoFactorMiddleware']);
312
+            $dispatcher->registerMiddleware($c['BruteForceMiddleware']);
313
+            $dispatcher->registerMiddleware($c['RateLimitingMiddleware']);
314
+
315
+            foreach($middleWares as $middleWare) {
316
+                $dispatcher->registerMiddleware($c[$middleWare]);
317
+            }
318
+
319
+            $dispatcher->registerMiddleware($c['SessionMiddleware']);
320
+            return $dispatcher;
321
+        });
322
+
323
+    }
324
+
325
+    /**
326
+     * @return \OCP\IServerContainer
327
+     */
328
+    public function getServer()
329
+    {
330
+        return $this->server;
331
+    }
332
+
333
+    /**
334
+     * @param string $middleWare
335
+     * @return boolean|null
336
+     */
337
+    public function registerMiddleWare($middleWare) {
338
+        array_push($this->middleWares, $middleWare);
339
+    }
340
+
341
+    /**
342
+     * used to return the appname of the set application
343
+     * @return string the name of your application
344
+     */
345
+    public function getAppName() {
346
+        return $this->query('AppName');
347
+    }
348
+
349
+    /**
350
+     * @deprecated use IUserSession->isLoggedIn()
351
+     * @return boolean
352
+     */
353
+    public function isLoggedIn() {
354
+        return \OC::$server->getUserSession()->isLoggedIn();
355
+    }
356
+
357
+    /**
358
+     * @deprecated use IGroupManager->isAdmin($userId)
359
+     * @return boolean
360
+     */
361
+    public function isAdminUser() {
362
+        $uid = $this->getUserId();
363
+        return \OC_User::isAdminUser($uid);
364
+    }
365
+
366
+    private function getUserId() {
367
+        return $this->getServer()->getSession()->get('user_id');
368
+    }
369
+
370
+    /**
371
+     * @deprecated use the ILogger instead
372
+     * @param string $message
373
+     * @param string $level
374
+     * @return mixed
375
+     */
376
+    public function log($message, $level) {
377
+        switch($level){
378
+            case 'debug':
379
+                $level = \OCP\Util::DEBUG;
380
+                break;
381
+            case 'info':
382
+                $level = \OCP\Util::INFO;
383
+                break;
384
+            case 'warn':
385
+                $level = \OCP\Util::WARN;
386
+                break;
387
+            case 'fatal':
388
+                $level = \OCP\Util::FATAL;
389
+                break;
390
+            default:
391
+                $level = \OCP\Util::ERROR;
392
+                break;
393
+        }
394
+        \OCP\Util::writeLog($this->getAppName(), $message, $level);
395
+    }
396
+
397
+    /**
398
+     * Register a capability
399
+     *
400
+     * @param string $serviceName e.g. 'OCA\Files\Capabilities'
401
+     */
402
+    public function registerCapability($serviceName) {
403
+        $this->query('OC\CapabilitiesManager')->registerCapability(function() use ($serviceName) {
404
+            return $this->query($serviceName);
405
+        });
406
+    }
407
+
408
+    /**
409
+     * @param string $name
410
+     * @return mixed
411
+     * @throws QueryException if the query could not be resolved
412
+     */
413
+    public function query($name) {
414
+        try {
415
+            return $this->queryNoFallback($name);
416
+        } catch (QueryException $firstException) {
417
+            try {
418
+                return $this->getServer()->query($name);
419
+            } catch (QueryException $secondException) {
420
+                if ($firstException->getCode() === 1) {
421
+                    throw $secondException;
422
+                }
423
+                throw $firstException;
424
+            }
425
+        }
426
+    }
427
+
428
+    /**
429
+     * @param string $name
430
+     * @return mixed
431
+     * @throws QueryException if the query could not be resolved
432
+     */
433
+    public function queryNoFallback($name) {
434
+        $name = $this->sanitizeName($name);
435
+
436
+        if ($this->offsetExists($name)) {
437
+            return parent::query($name);
438
+        } else {
439
+            if ($this['AppName'] === 'settings' && strpos($name, 'OC\\Settings\\') === 0) {
440
+                return parent::query($name);
441
+            } else if ($this['AppName'] === 'core' && strpos($name, 'OC\\Core\\') === 0) {
442
+                return parent::query($name);
443
+            } else if (strpos($name, \OC\AppFramework\App::buildAppNamespace($this['AppName']) . '\\') === 0) {
444
+                return parent::query($name);
445
+            }
446
+        }
447
+
448
+        throw new QueryException('Could not resolve ' . $name . '!' .
449
+            ' Class can not be instantiated', 1);
450
+    }
451 451
 }
Please login to merge, or discard this patch.
Spacing   +21 added lines, -21 removed lines patch added patch discarded remove patch
@@ -79,7 +79,7 @@  discard block
 block discarded – undo
79 79
 	 * @param array $urlParams
80 80
 	 * @param ServerContainer|null $server
81 81
 	 */
82
-	public function __construct($appName, $urlParams = array(), ServerContainer $server = null){
82
+	public function __construct($appName, $urlParams = array(), ServerContainer $server = null) {
83 83
 		parent::__construct();
84 84
 		$this['AppName'] = $appName;
85 85
 		$this['urlParams'] = $urlParams;
@@ -99,7 +99,7 @@  discard block
 block discarded – undo
99 99
 		/**
100 100
 		 * Core services
101 101
 		 */
102
-		$this->registerService(IOutput::class, function($c){
102
+		$this->registerService(IOutput::class, function($c) {
103 103
 			return new Output($this->getServer()->getWebRoot());
104 104
 		});
105 105
 
@@ -107,7 +107,7 @@  discard block
 block discarded – undo
107 107
 			return $this->getServer()->getUserFolder();
108 108
 		});
109 109
 
110
-		$this->registerService(IAppData::class, function (SimpleContainer $c) {
110
+		$this->registerService(IAppData::class, function(SimpleContainer $c) {
111 111
 			return $this->getServer()->getAppDataDir($c->query('AppName'));
112 112
 		});
113 113
 
@@ -128,25 +128,25 @@  discard block
 block discarded – undo
128 128
 
129 129
 		$this->registerAlias(\OC\User\Session::class, \OCP\IUserSession::class);
130 130
 
131
-		$this->registerService(IServerContainer::class, function ($c) {
131
+		$this->registerService(IServerContainer::class, function($c) {
132 132
 			return $this->getServer();
133 133
 		});
134 134
 		$this->registerAlias('ServerContainer', IServerContainer::class);
135 135
 
136
-		$this->registerService(\OCP\WorkflowEngine\IManager::class, function ($c) {
136
+		$this->registerService(\OCP\WorkflowEngine\IManager::class, function($c) {
137 137
 			return $c->query('OCA\WorkflowEngine\Manager');
138 138
 		});
139 139
 
140
-		$this->registerService(\OCP\AppFramework\IAppContainer::class, function ($c) {
140
+		$this->registerService(\OCP\AppFramework\IAppContainer::class, function($c) {
141 141
 			return $c;
142 142
 		});
143 143
 
144 144
 		// commonly used attributes
145
-		$this->registerService('UserId', function ($c) {
145
+		$this->registerService('UserId', function($c) {
146 146
 			return $c->query('OCP\\IUserSession')->getSession()->get('user_id');
147 147
 		});
148 148
 
149
-		$this->registerService('WebRoot', function ($c) {
149
+		$this->registerService('WebRoot', function($c) {
150 150
 			return $c->query('ServerContainer')->getWebRoot();
151 151
 		});
152 152
 
@@ -154,15 +154,15 @@  discard block
 block discarded – undo
154 154
 			return Util::getDefaultEmailAddress('no-reply');
155 155
 		});
156 156
 
157
-		$this->registerService('OC_Defaults', function ($c) {
157
+		$this->registerService('OC_Defaults', function($c) {
158 158
 			return $c->getServer()->getThemingDefaults();
159 159
 		});
160 160
 
161
-		$this->registerService('OCP\Encryption\IManager', function ($c) {
161
+		$this->registerService('OCP\Encryption\IManager', function($c) {
162 162
 			return $this->getServer()->getEncryptionManager();
163 163
 		});
164 164
 
165
-		$this->registerService(IConfig::class, function ($c) {
165
+		$this->registerService(IConfig::class, function($c) {
166 166
 			return $c->query(OC\GlobalScale\Config::class);
167 167
 		});
168 168
 
@@ -170,7 +170,7 @@  discard block
 block discarded – undo
170 170
 			return $c->query(Validator::class);
171 171
 		});
172 172
 
173
-		$this->registerService(\OC\Security\IdentityProof\Manager::class, function ($c) {
173
+		$this->registerService(\OC\Security\IdentityProof\Manager::class, function($c) {
174 174
 			return new \OC\Security\IdentityProof\Manager(
175 175
 				$this->getServer()->query(\OC\Files\AppData\Factory::class),
176 176
 				$this->getServer()->getCrypto(),
@@ -178,7 +178,7 @@  discard block
 block discarded – undo
178 178
 			);
179 179
 		});
180 180
 
181
-		$this->registerService('Protocol', function($c){
181
+		$this->registerService('Protocol', function($c) {
182 182
 			/** @var \OC\Server $server */
183 183
 			$server = $c->query('ServerContainer');
184 184
 			$protocol = $server->getRequest()->getHttpProtocol();
@@ -225,7 +225,7 @@  discard block
 block discarded – undo
225 225
 			);
226 226
 		});
227 227
 
228
-		$this->registerService(OC\AppFramework\Middleware\Security\PasswordConfirmationMiddleware::class, function ($c) use ($app) {
228
+		$this->registerService(OC\AppFramework\Middleware\Security\PasswordConfirmationMiddleware::class, function($c) use ($app) {
229 229
 			/** @var \OC\Server $server */
230 230
 			$server = $app->getServer();
231 231
 
@@ -277,7 +277,7 @@  discard block
 block discarded – undo
277 277
 			);
278 278
 		});
279 279
 
280
-		$this->registerService('TwoFactorMiddleware', function (SimpleContainer $c) use ($app) {
280
+		$this->registerService('TwoFactorMiddleware', function(SimpleContainer $c) use ($app) {
281 281
 			$twoFactorManager = $c->getServer()->getTwoFactorAuthManager();
282 282
 			$userSession = $app->getServer()->getUserSession();
283 283
 			$session = $app->getServer()->getSession();
@@ -287,13 +287,13 @@  discard block
 block discarded – undo
287 287
 			return new TwoFactorMiddleware($twoFactorManager, $userSession, $session, $urlGenerator, $reflector, $request);
288 288
 		});
289 289
 
290
-		$this->registerService('OCSMiddleware', function (SimpleContainer $c) {
290
+		$this->registerService('OCSMiddleware', function(SimpleContainer $c) {
291 291
 			return new OCSMiddleware(
292 292
 				$c['Request']
293 293
 			);
294 294
 		});
295 295
 
296
-		$this->registerService(OC\AppFramework\Middleware\Security\SameSiteCookieMiddleware::class, function (SimpleContainer $c) {
296
+		$this->registerService(OC\AppFramework\Middleware\Security\SameSiteCookieMiddleware::class, function(SimpleContainer $c) {
297 297
 			return new OC\AppFramework\Middleware\Security\SameSiteCookieMiddleware(
298 298
 				$c['Request'],
299 299
 				$c['ControllerMethodReflector']
@@ -312,7 +312,7 @@  discard block
 block discarded – undo
312 312
 			$dispatcher->registerMiddleware($c['BruteForceMiddleware']);
313 313
 			$dispatcher->registerMiddleware($c['RateLimitingMiddleware']);
314 314
 
315
-			foreach($middleWares as $middleWare) {
315
+			foreach ($middleWares as $middleWare) {
316 316
 				$dispatcher->registerMiddleware($c[$middleWare]);
317 317
 			}
318 318
 
@@ -374,7 +374,7 @@  discard block
 block discarded – undo
374 374
 	 * @return mixed
375 375
 	 */
376 376
 	public function log($message, $level) {
377
-		switch($level){
377
+		switch ($level) {
378 378
 			case 'debug':
379 379
 				$level = \OCP\Util::DEBUG;
380 380
 				break;
@@ -440,12 +440,12 @@  discard block
 block discarded – undo
440 440
 				return parent::query($name);
441 441
 			} else if ($this['AppName'] === 'core' && strpos($name, 'OC\\Core\\') === 0) {
442 442
 				return parent::query($name);
443
-			} else if (strpos($name, \OC\AppFramework\App::buildAppNamespace($this['AppName']) . '\\') === 0) {
443
+			} else if (strpos($name, \OC\AppFramework\App::buildAppNamespace($this['AppName']).'\\') === 0) {
444 444
 				return parent::query($name);
445 445
 			}
446 446
 		}
447 447
 
448
-		throw new QueryException('Could not resolve ' . $name . '!' .
448
+		throw new QueryException('Could not resolve '.$name.'!'.
449 449
 			' Class can not be instantiated', 1);
450 450
 	}
451 451
 }
Please login to merge, or discard this patch.