Completed
Pull Request — master (#3977)
by Joas
13:07
created
lib/private/ServerContainer.php 1 patch
Indentation   +47 added lines, -47 removed lines patch added patch discarded remove patch
@@ -34,58 +34,58 @@
 block discarded – undo
34 34
  * @package OC
35 35
  */
36 36
 class ServerContainer extends SimpleContainer {
37
-	/** @var DIContainer[] */
38
-	protected $appContainers;
37
+    /** @var DIContainer[] */
38
+    protected $appContainers;
39 39
 
40
-	/**
41
-	 * ServerContainer constructor.
42
-	 */
43
-	public function __construct() {
44
-		parent::__construct();
45
-		$this->appContainers = [];
46
-	}
40
+    /**
41
+     * ServerContainer constructor.
42
+     */
43
+    public function __construct() {
44
+        parent::__construct();
45
+        $this->appContainers = [];
46
+    }
47 47
 
48
-	/**
49
-	 * @param string $appName
50
-	 * @param DIContainer $container
51
-	 */
52
-	public function registerAppContainer($appName, DIContainer $container) {
53
-		$this->appContainers[strtolower(App::buildAppNamespace($appName, ''))] = $container;
54
-	}
48
+    /**
49
+     * @param string $appName
50
+     * @param DIContainer $container
51
+     */
52
+    public function registerAppContainer($appName, DIContainer $container) {
53
+        $this->appContainers[strtolower(App::buildAppNamespace($appName, ''))] = $container;
54
+    }
55 55
 
56
-	/**
57
-	 * @param string $appName
58
-	 * @return DIContainer
59
-	 */
60
-	public function getAppContainer($appName) {
61
-		if (isset($this->appContainers[$appName])) {
62
-			return $this->appContainers[$appName];
63
-		}
56
+    /**
57
+     * @param string $appName
58
+     * @return DIContainer
59
+     */
60
+    public function getAppContainer($appName) {
61
+        if (isset($this->appContainers[$appName])) {
62
+            return $this->appContainers[$appName];
63
+        }
64 64
 
65
-		return new DIContainer($appName);
66
-	}
65
+        return new DIContainer($appName);
66
+    }
67 67
 
68
-	/**
69
-	 * @param string $name name of the service to query for
70
-	 * @return mixed registered service for the given $name
71
-	 * @throws QueryException if the query could not be resolved
72
-	 */
73
-	public function query($name) {
74
-		$name = $this->sanitizeName($name);
68
+    /**
69
+     * @param string $name name of the service to query for
70
+     * @return mixed registered service for the given $name
71
+     * @throws QueryException if the query could not be resolved
72
+     */
73
+    public function query($name) {
74
+        $name = $this->sanitizeName($name);
75 75
 
76
-		// In case the service starts with OCA\ we try to find the service in
77
-		// the apps container first.
78
-		if (strpos($name, 'OCA\\') === 0 && substr_count($name, '\\') >= 2) {
79
-			$segments = explode('\\', $name);
80
-			$appContainer = $this->getAppContainer(strtolower($segments[1]));
81
-			try {
82
-				return $appContainer->query($name, false);
83
-			} catch (QueryException $e) {
84
-				// Didn't find the service in the respective app container,
85
-				// ignore it and fall back to the core container.
86
-			}
87
-		}
76
+        // In case the service starts with OCA\ we try to find the service in
77
+        // the apps container first.
78
+        if (strpos($name, 'OCA\\') === 0 && substr_count($name, '\\') >= 2) {
79
+            $segments = explode('\\', $name);
80
+            $appContainer = $this->getAppContainer(strtolower($segments[1]));
81
+            try {
82
+                return $appContainer->query($name, false);
83
+            } catch (QueryException $e) {
84
+                // Didn't find the service in the respective app container,
85
+                // ignore it and fall back to the core container.
86
+            }
87
+        }
88 88
 
89
-		return parent::query($name);
90
-	}
89
+        return parent::query($name);
90
+    }
91 91
 }
Please login to merge, or discard this patch.
lib/private/AppFramework/DependencyInjection/DIContainer.php 2 patches
Spacing   +19 added lines, -19 removed lines patch added patch discarded remove patch
@@ -74,7 +74,7 @@  discard block
 block discarded – undo
74 74
 	 * @param array $urlParams
75 75
 	 * @param ServerContainer $server
76 76
 	 */
77
-	public function __construct($appName, $urlParams = array(), ServerContainer $server = null){
77
+	public function __construct($appName, $urlParams = array(), ServerContainer $server = null) {
78 78
 		parent::__construct();
79 79
 		$this['AppName'] = $appName;
80 80
 		$this['urlParams'] = $urlParams;
@@ -94,7 +94,7 @@  discard block
 block discarded – undo
94 94
 		/**
95 95
 		 * Core services
96 96
 		 */
97
-		$this->registerService(IOutput::class, function($c){
97
+		$this->registerService(IOutput::class, function($c) {
98 98
 			return new Output($this->getServer()->getWebRoot());
99 99
 		});
100 100
 
@@ -102,7 +102,7 @@  discard block
 block discarded – undo
102 102
 			return $this->getServer()->getUserFolder();
103 103
 		});
104 104
 
105
-		$this->registerService(IAppData::class, function (SimpleContainer $c) {
105
+		$this->registerService(IAppData::class, function(SimpleContainer $c) {
106 106
 			return $this->getServer()->getAppDataDir($c->query('AppName'));
107 107
 		});
108 108
 
@@ -123,25 +123,25 @@  discard block
 block discarded – undo
123 123
 
124 124
 		$this->registerAlias(\OC\User\Session::class, \OCP\IUserSession::class);
125 125
 
126
-		$this->registerService(IServerContainer::class, function ($c) {
126
+		$this->registerService(IServerContainer::class, function($c) {
127 127
 			return $this->getServer();
128 128
 		});
129 129
 		$this->registerAlias('ServerContainer', IServerContainer::class);
130 130
 
131
-		$this->registerService(\OCP\WorkflowEngine\IManager::class, function ($c) {
131
+		$this->registerService(\OCP\WorkflowEngine\IManager::class, function($c) {
132 132
 			return $c->query('OCA\WorkflowEngine\Manager');
133 133
 		});
134 134
 
135
-		$this->registerService(\OCP\AppFramework\IAppContainer::class, function ($c) {
135
+		$this->registerService(\OCP\AppFramework\IAppContainer::class, function($c) {
136 136
 			return $c;
137 137
 		});
138 138
 
139 139
 		// commonly used attributes
140
-		$this->registerService('UserId', function ($c) {
140
+		$this->registerService('UserId', function($c) {
141 141
 			return $c->query('OCP\\IUserSession')->getSession()->get('user_id');
142 142
 		});
143 143
 
144
-		$this->registerService('WebRoot', function ($c) {
144
+		$this->registerService('WebRoot', function($c) {
145 145
 			return $c->query('ServerContainer')->getWebRoot();
146 146
 		});
147 147
 
@@ -149,11 +149,11 @@  discard block
 block discarded – undo
149 149
 			return Util::getDefaultEmailAddress('no-reply');
150 150
 		});
151 151
 
152
-		$this->registerService('OC_Defaults', function ($c) {
152
+		$this->registerService('OC_Defaults', function($c) {
153 153
 			return $c->getServer()->getThemingDefaults();
154 154
 		});
155 155
 
156
-		$this->registerService('OCP\Encryption\IManager', function ($c) {
156
+		$this->registerService('OCP\Encryption\IManager', function($c) {
157 157
 			return $this->getServer()->getEncryptionManager();
158 158
 		});
159 159
 
@@ -161,7 +161,7 @@  discard block
 block discarded – undo
161 161
 			return $c->query(Validator::class);
162 162
 		});
163 163
 
164
-		$this->registerService(\OC\Security\IdentityProof\Manager::class, function ($c) {
164
+		$this->registerService(\OC\Security\IdentityProof\Manager::class, function($c) {
165 165
 			return new \OC\Security\IdentityProof\Manager(
166 166
 				$this->getServer()->getAppDataDir('identityproof'),
167 167
 				$this->getServer()->getCrypto()
@@ -172,15 +172,15 @@  discard block
 block discarded – undo
172 172
 		/**
173 173
 		 * App Framework APIs
174 174
 		 */
175
-		$this->registerService('API', function($c){
175
+		$this->registerService('API', function($c) {
176 176
 			$c->query('OCP\\ILogger')->debug(
177
-				'Accessing the API class is deprecated! Use the appropriate ' .
177
+				'Accessing the API class is deprecated! Use the appropriate '.
178 178
 				'services instead!'
179 179
 			);
180 180
 			return new API($c['AppName']);
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();
@@ -246,7 +246,7 @@  discard block
 block discarded – undo
246 246
 			);
247 247
 		});
248 248
 
249
-		$this->registerService('TwoFactorMiddleware', function (SimpleContainer $c) use ($app) {
249
+		$this->registerService('TwoFactorMiddleware', function(SimpleContainer $c) use ($app) {
250 250
 			$twoFactorManager = $c->getServer()->getTwoFactorAuthManager();
251 251
 			$userSession = $app->getServer()->getUserSession();
252 252
 			$session = $app->getServer()->getSession();
@@ -256,7 +256,7 @@  discard block
 block discarded – undo
256 256
 			return new TwoFactorMiddleware($twoFactorManager, $userSession, $session, $urlGenerator, $reflector, $request);
257 257
 		});
258 258
 
259
-		$this->registerService('OCSMiddleware', function (SimpleContainer $c) {
259
+		$this->registerService('OCSMiddleware', function(SimpleContainer $c) {
260 260
 			return new OCSMiddleware(
261 261
 				$c['Request']
262 262
 			);
@@ -270,7 +270,7 @@  discard block
 block discarded – undo
270 270
 			$dispatcher->registerMiddleware($c['SecurityMiddleware']);
271 271
 			$dispatcher->registerMiddleWare($c['TwoFactorMiddleware']);
272 272
 
273
-			foreach($middleWares as $middleWare) {
273
+			foreach ($middleWares as $middleWare) {
274 274
 				$dispatcher->registerMiddleware($c[$middleWare]);
275 275
 			}
276 276
 
@@ -342,7 +342,7 @@  discard block
 block discarded – undo
342 342
 	 * @return mixed
343 343
 	 */
344 344
 	function log($message, $level) {
345
-		switch($level){
345
+		switch ($level) {
346 346
 			case 'debug':
347 347
 				$level = \OCP\Util::DEBUG;
348 348
 				break;
@@ -383,7 +383,7 @@  discard block
 block discarded – undo
383 383
 				return parent::query($name);
384 384
 			} else if ($this['AppName'] === 'core' && strpos($name, 'OC\\Core\\') === 0) {
385 385
 				return parent::query($name);
386
-			} else if (strpos($name, \OC\AppFramework\App::buildAppNamespace($this['AppName']) . '\\') === 0) {
386
+			} else if (strpos($name, \OC\AppFramework\App::buildAppNamespace($this['AppName']).'\\') === 0) {
387 387
 				return parent::query($name);
388 388
 			}
389 389
 		}
Please login to merge, or discard this patch.
Indentation   +339 added lines, -339 removed lines patch added patch discarded remove patch
@@ -61,343 +61,343 @@
 block discarded – undo
61 61
 
62 62
 class DIContainer extends SimpleContainer implements IAppContainer {
63 63
 
64
-	/**
65
-	 * @var array
66
-	 */
67
-	private $middleWares = array();
68
-
69
-	/** @var ServerContainer */
70
-	private $server;
71
-
72
-	/**
73
-	 * Put your class dependencies in here
74
-	 * @param string $appName the name of the app
75
-	 * @param array $urlParams
76
-	 * @param ServerContainer $server
77
-	 */
78
-	public function __construct($appName, $urlParams = array(), ServerContainer $server = null){
79
-		parent::__construct();
80
-		$this['AppName'] = $appName;
81
-		$this['urlParams'] = $urlParams;
82
-
83
-		/** @var \OC\ServerContainer $server */
84
-		if ($server === null) {
85
-			$server = \OC::$server;
86
-		}
87
-		$this->server = $server;
88
-		$this->server->registerAppContainer($appName, $this);
89
-
90
-		// aliases
91
-		$this->registerAlias('appName', 'AppName');
92
-		$this->registerAlias('webRoot', 'WebRoot');
93
-		$this->registerAlias('userId', 'UserId');
94
-
95
-		/**
96
-		 * Core services
97
-		 */
98
-		$this->registerService(IOutput::class, function($c){
99
-			return new Output($this->getServer()->getWebRoot());
100
-		});
101
-
102
-		$this->registerService(Folder::class, function() {
103
-			return $this->getServer()->getUserFolder();
104
-		});
105
-
106
-		$this->registerService(IAppData::class, function (SimpleContainer $c) {
107
-			return $this->getServer()->getAppDataDir($c->query('AppName'));
108
-		});
109
-
110
-		$this->registerService(IL10N::class, function($c) {
111
-			return $this->getServer()->getL10N($c->query('AppName'));
112
-		});
113
-
114
-		$this->registerAlias(\OCP\AppFramework\Utility\IControllerMethodReflector::class, \OC\AppFramework\Utility\ControllerMethodReflector::class);
115
-		$this->registerAlias('ControllerMethodReflector', \OCP\AppFramework\Utility\IControllerMethodReflector::class);
116
-
117
-		$this->registerService(IRequest::class, function() {
118
-			return $this->getServer()->query(IRequest::class);
119
-		});
120
-		$this->registerAlias('Request', IRequest::class);
121
-
122
-		$this->registerAlias(\OCP\AppFramework\Utility\ITimeFactory::class, \OC\AppFramework\Utility\TimeFactory::class);
123
-		$this->registerAlias('TimeFactory', \OCP\AppFramework\Utility\ITimeFactory::class);
124
-
125
-		$this->registerAlias(\OC\User\Session::class, \OCP\IUserSession::class);
126
-
127
-		$this->registerService(IServerContainer::class, function ($c) {
128
-			return $this->getServer();
129
-		});
130
-		$this->registerAlias('ServerContainer', IServerContainer::class);
131
-
132
-		$this->registerService(\OCP\WorkflowEngine\IManager::class, function ($c) {
133
-			return $c->query('OCA\WorkflowEngine\Manager');
134
-		});
135
-
136
-		$this->registerService(\OCP\AppFramework\IAppContainer::class, function ($c) {
137
-			return $c;
138
-		});
139
-
140
-		// commonly used attributes
141
-		$this->registerService('UserId', function ($c) {
142
-			return $c->query('OCP\\IUserSession')->getSession()->get('user_id');
143
-		});
144
-
145
-		$this->registerService('WebRoot', function ($c) {
146
-			return $c->query('ServerContainer')->getWebRoot();
147
-		});
148
-
149
-		$this->registerService('fromMailAddress', function() {
150
-			return Util::getDefaultEmailAddress('no-reply');
151
-		});
152
-
153
-		$this->registerService('OC_Defaults', function ($c) {
154
-			return $c->getServer()->getThemingDefaults();
155
-		});
156
-
157
-		$this->registerService('OCP\Encryption\IManager', function ($c) {
158
-			return $this->getServer()->getEncryptionManager();
159
-		});
160
-
161
-		$this->registerService(IValidator::class, function($c) {
162
-			return $c->query(Validator::class);
163
-		});
164
-
165
-		$this->registerService(\OC\Security\IdentityProof\Manager::class, function ($c) {
166
-			return new \OC\Security\IdentityProof\Manager(
167
-				$this->getServer()->getAppDataDir('identityproof'),
168
-				$this->getServer()->getCrypto()
169
-			);
170
-		});
171
-
172
-
173
-		/**
174
-		 * App Framework APIs
175
-		 */
176
-		$this->registerService('API', function($c){
177
-			$c->query('OCP\\ILogger')->debug(
178
-				'Accessing the API class is deprecated! Use the appropriate ' .
179
-				'services instead!'
180
-			);
181
-			return new API($c['AppName']);
182
-		});
183
-
184
-		$this->registerService('Protocol', function($c){
185
-			/** @var \OC\Server $server */
186
-			$server = $c->query('ServerContainer');
187
-			$protocol = $server->getRequest()->getHttpProtocol();
188
-			return new Http($_SERVER, $protocol);
189
-		});
190
-
191
-		$this->registerService('Dispatcher', function($c) {
192
-			return new Dispatcher(
193
-				$c['Protocol'],
194
-				$c['MiddlewareDispatcher'],
195
-				$c['ControllerMethodReflector'],
196
-				$c['Request']
197
-			);
198
-		});
199
-
200
-		/**
201
-		 * App Framework default arguments
202
-		 */
203
-		$this->registerParameter('corsMethods', 'PUT, POST, GET, DELETE, PATCH');
204
-		$this->registerParameter('corsAllowedHeaders', 'Authorization, Content-Type, Accept');
205
-		$this->registerParameter('corsMaxAge', 1728000);
206
-
207
-		/**
208
-		 * Middleware
209
-		 */
210
-		$app = $this;
211
-		$this->registerService('SecurityMiddleware', function($c) use ($app){
212
-			/** @var \OC\Server $server */
213
-			$server = $app->getServer();
214
-
215
-			return new SecurityMiddleware(
216
-				$c['Request'],
217
-				$c['ControllerMethodReflector'],
218
-				$server->getNavigationManager(),
219
-				$server->getURLGenerator(),
220
-				$server->getLogger(),
221
-				$server->getSession(),
222
-				$c['AppName'],
223
-				$app->isLoggedIn(),
224
-				$app->isAdminUser(),
225
-				$server->getContentSecurityPolicyManager(),
226
-				$server->getCsrfTokenManager(),
227
-				$server->getContentSecurityPolicyNonceManager(),
228
-				$server->getBruteForceThrottler()
229
-			);
230
-
231
-		});
232
-
233
-		$this->registerService('CORSMiddleware', function($c) {
234
-			return new CORSMiddleware(
235
-				$c['Request'],
236
-				$c['ControllerMethodReflector'],
237
-				$c->query(IUserSession::class),
238
-				$c->getServer()->getBruteForceThrottler()
239
-			);
240
-		});
241
-
242
-		$this->registerService('SessionMiddleware', function($c) use ($app) {
243
-			return new SessionMiddleware(
244
-				$c['Request'],
245
-				$c['ControllerMethodReflector'],
246
-				$app->getServer()->getSession()
247
-			);
248
-		});
249
-
250
-		$this->registerService('TwoFactorMiddleware', function (SimpleContainer $c) use ($app) {
251
-			$twoFactorManager = $c->getServer()->getTwoFactorAuthManager();
252
-			$userSession = $app->getServer()->getUserSession();
253
-			$session = $app->getServer()->getSession();
254
-			$urlGenerator = $app->getServer()->getURLGenerator();
255
-			$reflector = $c['ControllerMethodReflector'];
256
-			$request = $app->getServer()->getRequest();
257
-			return new TwoFactorMiddleware($twoFactorManager, $userSession, $session, $urlGenerator, $reflector, $request);
258
-		});
259
-
260
-		$this->registerService('OCSMiddleware', function (SimpleContainer $c) {
261
-			return new OCSMiddleware(
262
-				$c['Request']
263
-			);
264
-		});
265
-
266
-		$middleWares = &$this->middleWares;
267
-		$this->registerService('MiddlewareDispatcher', function($c) use (&$middleWares) {
268
-			$dispatcher = new MiddlewareDispatcher();
269
-			$dispatcher->registerMiddleware($c['CORSMiddleware']);
270
-			$dispatcher->registerMiddleware($c['OCSMiddleware']);
271
-			$dispatcher->registerMiddleware($c['SecurityMiddleware']);
272
-			$dispatcher->registerMiddleWare($c['TwoFactorMiddleware']);
273
-
274
-			foreach($middleWares as $middleWare) {
275
-				$dispatcher->registerMiddleware($c[$middleWare]);
276
-			}
277
-
278
-			$dispatcher->registerMiddleware($c['SessionMiddleware']);
279
-			return $dispatcher;
280
-		});
281
-
282
-	}
283
-
284
-
285
-	/**
286
-	 * @deprecated implements only deprecated methods
287
-	 * @return IApi
288
-	 */
289
-	function getCoreApi()
290
-	{
291
-		return $this->query('API');
292
-	}
293
-
294
-	/**
295
-	 * @return \OCP\IServerContainer
296
-	 */
297
-	function getServer()
298
-	{
299
-		return $this->server;
300
-	}
301
-
302
-	/**
303
-	 * @param string $middleWare
304
-	 * @return boolean|null
305
-	 */
306
-	function registerMiddleWare($middleWare) {
307
-		array_push($this->middleWares, $middleWare);
308
-	}
309
-
310
-	/**
311
-	 * used to return the appname of the set application
312
-	 * @return string the name of your application
313
-	 */
314
-	function getAppName() {
315
-		return $this->query('AppName');
316
-	}
317
-
318
-	/**
319
-	 * @deprecated use IUserSession->isLoggedIn()
320
-	 * @return boolean
321
-	 */
322
-	function isLoggedIn() {
323
-		return \OC::$server->getUserSession()->isLoggedIn();
324
-	}
325
-
326
-	/**
327
-	 * @deprecated use IGroupManager->isAdmin($userId)
328
-	 * @return boolean
329
-	 */
330
-	function isAdminUser() {
331
-		$uid = $this->getUserId();
332
-		return \OC_User::isAdminUser($uid);
333
-	}
334
-
335
-	private function getUserId() {
336
-		return $this->getServer()->getSession()->get('user_id');
337
-	}
338
-
339
-	/**
340
-	 * @deprecated use the ILogger instead
341
-	 * @param string $message
342
-	 * @param string $level
343
-	 * @return mixed
344
-	 */
345
-	function log($message, $level) {
346
-		switch($level){
347
-			case 'debug':
348
-				$level = \OCP\Util::DEBUG;
349
-				break;
350
-			case 'info':
351
-				$level = \OCP\Util::INFO;
352
-				break;
353
-			case 'warn':
354
-				$level = \OCP\Util::WARN;
355
-				break;
356
-			case 'fatal':
357
-				$level = \OCP\Util::FATAL;
358
-				break;
359
-			default:
360
-				$level = \OCP\Util::ERROR;
361
-				break;
362
-		}
363
-		\OCP\Util::writeLog($this->getAppName(), $message, $level);
364
-	}
365
-
366
-	/**
367
-	 * Register a capability
368
-	 *
369
-	 * @param string $serviceName e.g. 'OCA\Files\Capabilities'
370
-	 */
371
-	public function registerCapability($serviceName) {
372
-		$this->query('OC\CapabilitiesManager')->registerCapability(function() use ($serviceName) {
373
-			return $this->query($serviceName);
374
-		});
375
-	}
376
-
377
-	/**
378
-	 * @param string $name
379
-	 * @param bool $allowServerFallback
380
-	 * @return mixed
381
-	 * @throws QueryException if the query could not be resolved
382
-	 */
383
-	public function query($name, $allowServerFallback = true) {
384
-		$name = $this->sanitizeName($name);
385
-
386
-		if ($this->offsetExists($name)) {
387
-			return parent::query($name);
388
-		} else {
389
-			if ($this['AppName'] === 'settings' && strpos($name, 'OC\\Settings\\') === 0) {
390
-				return parent::query($name);
391
-			} else if ($this['AppName'] === 'core' && strpos($name, 'OC\\Core\\') === 0) {
392
-				return parent::query($name);
393
-			} else if (strpos($name, \OC\AppFramework\App::buildAppNamespace($this['AppName']) . '\\') === 0) {
394
-				return parent::query($name);
395
-			}
396
-		}
397
-
398
-		if ($allowServerFallback) {
399
-			return $this->getServer()->query($name);
400
-		}
401
-		return parent::query($name);
402
-	}
64
+    /**
65
+     * @var array
66
+     */
67
+    private $middleWares = array();
68
+
69
+    /** @var ServerContainer */
70
+    private $server;
71
+
72
+    /**
73
+     * Put your class dependencies in here
74
+     * @param string $appName the name of the app
75
+     * @param array $urlParams
76
+     * @param ServerContainer $server
77
+     */
78
+    public function __construct($appName, $urlParams = array(), ServerContainer $server = null){
79
+        parent::__construct();
80
+        $this['AppName'] = $appName;
81
+        $this['urlParams'] = $urlParams;
82
+
83
+        /** @var \OC\ServerContainer $server */
84
+        if ($server === null) {
85
+            $server = \OC::$server;
86
+        }
87
+        $this->server = $server;
88
+        $this->server->registerAppContainer($appName, $this);
89
+
90
+        // aliases
91
+        $this->registerAlias('appName', 'AppName');
92
+        $this->registerAlias('webRoot', 'WebRoot');
93
+        $this->registerAlias('userId', 'UserId');
94
+
95
+        /**
96
+         * Core services
97
+         */
98
+        $this->registerService(IOutput::class, function($c){
99
+            return new Output($this->getServer()->getWebRoot());
100
+        });
101
+
102
+        $this->registerService(Folder::class, function() {
103
+            return $this->getServer()->getUserFolder();
104
+        });
105
+
106
+        $this->registerService(IAppData::class, function (SimpleContainer $c) {
107
+            return $this->getServer()->getAppDataDir($c->query('AppName'));
108
+        });
109
+
110
+        $this->registerService(IL10N::class, function($c) {
111
+            return $this->getServer()->getL10N($c->query('AppName'));
112
+        });
113
+
114
+        $this->registerAlias(\OCP\AppFramework\Utility\IControllerMethodReflector::class, \OC\AppFramework\Utility\ControllerMethodReflector::class);
115
+        $this->registerAlias('ControllerMethodReflector', \OCP\AppFramework\Utility\IControllerMethodReflector::class);
116
+
117
+        $this->registerService(IRequest::class, function() {
118
+            return $this->getServer()->query(IRequest::class);
119
+        });
120
+        $this->registerAlias('Request', IRequest::class);
121
+
122
+        $this->registerAlias(\OCP\AppFramework\Utility\ITimeFactory::class, \OC\AppFramework\Utility\TimeFactory::class);
123
+        $this->registerAlias('TimeFactory', \OCP\AppFramework\Utility\ITimeFactory::class);
124
+
125
+        $this->registerAlias(\OC\User\Session::class, \OCP\IUserSession::class);
126
+
127
+        $this->registerService(IServerContainer::class, function ($c) {
128
+            return $this->getServer();
129
+        });
130
+        $this->registerAlias('ServerContainer', IServerContainer::class);
131
+
132
+        $this->registerService(\OCP\WorkflowEngine\IManager::class, function ($c) {
133
+            return $c->query('OCA\WorkflowEngine\Manager');
134
+        });
135
+
136
+        $this->registerService(\OCP\AppFramework\IAppContainer::class, function ($c) {
137
+            return $c;
138
+        });
139
+
140
+        // commonly used attributes
141
+        $this->registerService('UserId', function ($c) {
142
+            return $c->query('OCP\\IUserSession')->getSession()->get('user_id');
143
+        });
144
+
145
+        $this->registerService('WebRoot', function ($c) {
146
+            return $c->query('ServerContainer')->getWebRoot();
147
+        });
148
+
149
+        $this->registerService('fromMailAddress', function() {
150
+            return Util::getDefaultEmailAddress('no-reply');
151
+        });
152
+
153
+        $this->registerService('OC_Defaults', function ($c) {
154
+            return $c->getServer()->getThemingDefaults();
155
+        });
156
+
157
+        $this->registerService('OCP\Encryption\IManager', function ($c) {
158
+            return $this->getServer()->getEncryptionManager();
159
+        });
160
+
161
+        $this->registerService(IValidator::class, function($c) {
162
+            return $c->query(Validator::class);
163
+        });
164
+
165
+        $this->registerService(\OC\Security\IdentityProof\Manager::class, function ($c) {
166
+            return new \OC\Security\IdentityProof\Manager(
167
+                $this->getServer()->getAppDataDir('identityproof'),
168
+                $this->getServer()->getCrypto()
169
+            );
170
+        });
171
+
172
+
173
+        /**
174
+         * App Framework APIs
175
+         */
176
+        $this->registerService('API', function($c){
177
+            $c->query('OCP\\ILogger')->debug(
178
+                'Accessing the API class is deprecated! Use the appropriate ' .
179
+                'services instead!'
180
+            );
181
+            return new API($c['AppName']);
182
+        });
183
+
184
+        $this->registerService('Protocol', function($c){
185
+            /** @var \OC\Server $server */
186
+            $server = $c->query('ServerContainer');
187
+            $protocol = $server->getRequest()->getHttpProtocol();
188
+            return new Http($_SERVER, $protocol);
189
+        });
190
+
191
+        $this->registerService('Dispatcher', function($c) {
192
+            return new Dispatcher(
193
+                $c['Protocol'],
194
+                $c['MiddlewareDispatcher'],
195
+                $c['ControllerMethodReflector'],
196
+                $c['Request']
197
+            );
198
+        });
199
+
200
+        /**
201
+         * App Framework default arguments
202
+         */
203
+        $this->registerParameter('corsMethods', 'PUT, POST, GET, DELETE, PATCH');
204
+        $this->registerParameter('corsAllowedHeaders', 'Authorization, Content-Type, Accept');
205
+        $this->registerParameter('corsMaxAge', 1728000);
206
+
207
+        /**
208
+         * Middleware
209
+         */
210
+        $app = $this;
211
+        $this->registerService('SecurityMiddleware', function($c) use ($app){
212
+            /** @var \OC\Server $server */
213
+            $server = $app->getServer();
214
+
215
+            return new SecurityMiddleware(
216
+                $c['Request'],
217
+                $c['ControllerMethodReflector'],
218
+                $server->getNavigationManager(),
219
+                $server->getURLGenerator(),
220
+                $server->getLogger(),
221
+                $server->getSession(),
222
+                $c['AppName'],
223
+                $app->isLoggedIn(),
224
+                $app->isAdminUser(),
225
+                $server->getContentSecurityPolicyManager(),
226
+                $server->getCsrfTokenManager(),
227
+                $server->getContentSecurityPolicyNonceManager(),
228
+                $server->getBruteForceThrottler()
229
+            );
230
+
231
+        });
232
+
233
+        $this->registerService('CORSMiddleware', function($c) {
234
+            return new CORSMiddleware(
235
+                $c['Request'],
236
+                $c['ControllerMethodReflector'],
237
+                $c->query(IUserSession::class),
238
+                $c->getServer()->getBruteForceThrottler()
239
+            );
240
+        });
241
+
242
+        $this->registerService('SessionMiddleware', function($c) use ($app) {
243
+            return new SessionMiddleware(
244
+                $c['Request'],
245
+                $c['ControllerMethodReflector'],
246
+                $app->getServer()->getSession()
247
+            );
248
+        });
249
+
250
+        $this->registerService('TwoFactorMiddleware', function (SimpleContainer $c) use ($app) {
251
+            $twoFactorManager = $c->getServer()->getTwoFactorAuthManager();
252
+            $userSession = $app->getServer()->getUserSession();
253
+            $session = $app->getServer()->getSession();
254
+            $urlGenerator = $app->getServer()->getURLGenerator();
255
+            $reflector = $c['ControllerMethodReflector'];
256
+            $request = $app->getServer()->getRequest();
257
+            return new TwoFactorMiddleware($twoFactorManager, $userSession, $session, $urlGenerator, $reflector, $request);
258
+        });
259
+
260
+        $this->registerService('OCSMiddleware', function (SimpleContainer $c) {
261
+            return new OCSMiddleware(
262
+                $c['Request']
263
+            );
264
+        });
265
+
266
+        $middleWares = &$this->middleWares;
267
+        $this->registerService('MiddlewareDispatcher', function($c) use (&$middleWares) {
268
+            $dispatcher = new MiddlewareDispatcher();
269
+            $dispatcher->registerMiddleware($c['CORSMiddleware']);
270
+            $dispatcher->registerMiddleware($c['OCSMiddleware']);
271
+            $dispatcher->registerMiddleware($c['SecurityMiddleware']);
272
+            $dispatcher->registerMiddleWare($c['TwoFactorMiddleware']);
273
+
274
+            foreach($middleWares as $middleWare) {
275
+                $dispatcher->registerMiddleware($c[$middleWare]);
276
+            }
277
+
278
+            $dispatcher->registerMiddleware($c['SessionMiddleware']);
279
+            return $dispatcher;
280
+        });
281
+
282
+    }
283
+
284
+
285
+    /**
286
+     * @deprecated implements only deprecated methods
287
+     * @return IApi
288
+     */
289
+    function getCoreApi()
290
+    {
291
+        return $this->query('API');
292
+    }
293
+
294
+    /**
295
+     * @return \OCP\IServerContainer
296
+     */
297
+    function getServer()
298
+    {
299
+        return $this->server;
300
+    }
301
+
302
+    /**
303
+     * @param string $middleWare
304
+     * @return boolean|null
305
+     */
306
+    function registerMiddleWare($middleWare) {
307
+        array_push($this->middleWares, $middleWare);
308
+    }
309
+
310
+    /**
311
+     * used to return the appname of the set application
312
+     * @return string the name of your application
313
+     */
314
+    function getAppName() {
315
+        return $this->query('AppName');
316
+    }
317
+
318
+    /**
319
+     * @deprecated use IUserSession->isLoggedIn()
320
+     * @return boolean
321
+     */
322
+    function isLoggedIn() {
323
+        return \OC::$server->getUserSession()->isLoggedIn();
324
+    }
325
+
326
+    /**
327
+     * @deprecated use IGroupManager->isAdmin($userId)
328
+     * @return boolean
329
+     */
330
+    function isAdminUser() {
331
+        $uid = $this->getUserId();
332
+        return \OC_User::isAdminUser($uid);
333
+    }
334
+
335
+    private function getUserId() {
336
+        return $this->getServer()->getSession()->get('user_id');
337
+    }
338
+
339
+    /**
340
+     * @deprecated use the ILogger instead
341
+     * @param string $message
342
+     * @param string $level
343
+     * @return mixed
344
+     */
345
+    function log($message, $level) {
346
+        switch($level){
347
+            case 'debug':
348
+                $level = \OCP\Util::DEBUG;
349
+                break;
350
+            case 'info':
351
+                $level = \OCP\Util::INFO;
352
+                break;
353
+            case 'warn':
354
+                $level = \OCP\Util::WARN;
355
+                break;
356
+            case 'fatal':
357
+                $level = \OCP\Util::FATAL;
358
+                break;
359
+            default:
360
+                $level = \OCP\Util::ERROR;
361
+                break;
362
+        }
363
+        \OCP\Util::writeLog($this->getAppName(), $message, $level);
364
+    }
365
+
366
+    /**
367
+     * Register a capability
368
+     *
369
+     * @param string $serviceName e.g. 'OCA\Files\Capabilities'
370
+     */
371
+    public function registerCapability($serviceName) {
372
+        $this->query('OC\CapabilitiesManager')->registerCapability(function() use ($serviceName) {
373
+            return $this->query($serviceName);
374
+        });
375
+    }
376
+
377
+    /**
378
+     * @param string $name
379
+     * @param bool $allowServerFallback
380
+     * @return mixed
381
+     * @throws QueryException if the query could not be resolved
382
+     */
383
+    public function query($name, $allowServerFallback = true) {
384
+        $name = $this->sanitizeName($name);
385
+
386
+        if ($this->offsetExists($name)) {
387
+            return parent::query($name);
388
+        } else {
389
+            if ($this['AppName'] === 'settings' && strpos($name, 'OC\\Settings\\') === 0) {
390
+                return parent::query($name);
391
+            } else if ($this['AppName'] === 'core' && strpos($name, 'OC\\Core\\') === 0) {
392
+                return parent::query($name);
393
+            } else if (strpos($name, \OC\AppFramework\App::buildAppNamespace($this['AppName']) . '\\') === 0) {
394
+                return parent::query($name);
395
+            }
396
+        }
397
+
398
+        if ($allowServerFallback) {
399
+            return $this->getServer()->query($name);
400
+        }
401
+        return parent::query($name);
402
+    }
403 403
 }
Please login to merge, or discard this patch.
lib/public/IContainer.php 1 patch
Indentation   +50 added lines, -50 removed lines patch added patch discarded remove patch
@@ -47,58 +47,58 @@
 block discarded – undo
47 47
  */
48 48
 interface IContainer {
49 49
 
50
-	/**
51
-	 * If a parameter is not registered in the container try to instantiate it
52
-	 * by using reflection to find out how to build the class
53
-	 * @param string $name the class name to resolve
54
-	 * @return \stdClass
55
-	 * @since 8.2.0
56
-	 * @throws QueryException if the class could not be found or instantiated
57
-	 */
58
-	public function resolve($name);
50
+    /**
51
+     * If a parameter is not registered in the container try to instantiate it
52
+     * by using reflection to find out how to build the class
53
+     * @param string $name the class name to resolve
54
+     * @return \stdClass
55
+     * @since 8.2.0
56
+     * @throws QueryException if the class could not be found or instantiated
57
+     */
58
+    public function resolve($name);
59 59
 
60
-	/**
61
-	 * Look up a service for a given name in the container.
62
-	 *
63
-	 * @param string $name
64
-	 * @return mixed
65
-	 * @throws QueryException if the query could not be resolved
66
-	 * @since 6.0.0
67
-	 */
68
-	public function query($name);
60
+    /**
61
+     * Look up a service for a given name in the container.
62
+     *
63
+     * @param string $name
64
+     * @return mixed
65
+     * @throws QueryException if the query could not be resolved
66
+     * @since 6.0.0
67
+     */
68
+    public function query($name);
69 69
 
70
-	/**
71
-	 * A value is stored in the container with it's corresponding name
72
-	 *
73
-	 * @param string $name
74
-	 * @param mixed $value
75
-	 * @return void
76
-	 * @since 6.0.0
77
-	 */
78
-	public function registerParameter($name, $value);
70
+    /**
71
+     * A value is stored in the container with it's corresponding name
72
+     *
73
+     * @param string $name
74
+     * @param mixed $value
75
+     * @return void
76
+     * @since 6.0.0
77
+     */
78
+    public function registerParameter($name, $value);
79 79
 
80
-	/**
81
-	 * A service is registered in the container where a closure is passed in which will actually
82
-	 * create the service on demand.
83
-	 * In case the parameter $shared is set to true (the default usage) the once created service will remain in
84
-	 * memory and be reused on subsequent calls.
85
-	 * In case the parameter is false the service will be recreated on every call.
86
-	 *
87
-	 * @param string $name
88
-	 * @param \Closure $closure
89
-	 * @param bool $shared
90
-	 * @return void
91
-	 * @since 6.0.0
92
-	 */
93
-	public function registerService($name, Closure $closure, $shared = true);
80
+    /**
81
+     * A service is registered in the container where a closure is passed in which will actually
82
+     * create the service on demand.
83
+     * In case the parameter $shared is set to true (the default usage) the once created service will remain in
84
+     * memory and be reused on subsequent calls.
85
+     * In case the parameter is false the service will be recreated on every call.
86
+     *
87
+     * @param string $name
88
+     * @param \Closure $closure
89
+     * @param bool $shared
90
+     * @return void
91
+     * @since 6.0.0
92
+     */
93
+    public function registerService($name, Closure $closure, $shared = true);
94 94
 
95
-	/**
96
-	 * Shortcut for returning a service from a service under a different key,
97
-	 * e.g. to tell the container to return a class when queried for an
98
-	 * interface
99
-	 * @param string $alias the alias that should be registered
100
-	 * @param string $target the target that should be resolved instead
101
-	 * @since 8.2.0
102
-	 */
103
-	public function registerAlias($alias, $target);
95
+    /**
96
+     * Shortcut for returning a service from a service under a different key,
97
+     * e.g. to tell the container to return a class when queried for an
98
+     * interface
99
+     * @param string $alias the alias that should be registered
100
+     * @param string $target the target that should be resolved instead
101
+     * @since 8.2.0
102
+     */
103
+    public function registerAlias($alias, $target);
104 104
 }
Please login to merge, or discard this patch.