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