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