Completed
Push — stable9 ( 485cb1...e094cf )
by Lukas
26:41 queued 26:23
created

appframework/dependencyinjection/dicontainer.php (12 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Bernhard Posselt <[email protected]>
6
 * @author Joas Schilling <[email protected]>
7
 * @author Jörn Friedrich Dreyer <[email protected]>
8
 * @author Lukas Reschke <[email protected]>
9
 * @author Morris Jobke <[email protected]>
10
 * @author Robin McCorkell <[email protected]>
11
 * @author Roeland Jago Douma <[email protected]>
12
 * @author Thomas Müller <[email protected]>
13
 * @author Thomas Tanghus <[email protected]>
14
 *
15
 * @license AGPL-3.0
16
 *
17
 * This code is free software: you can redistribute it and/or modify
18
 * it under the terms of the GNU Affero General Public License, version 3,
19
 * as published by the Free Software Foundation.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24
 * GNU Affero General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU Affero General Public License, version 3,
27
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
28
 *
29
 */
30
31
32
namespace OC\AppFramework\DependencyInjection;
33
34
use OC;
35
use OC\AppFramework\Http;
36
use OC\AppFramework\Http\Dispatcher;
37
use OC\AppFramework\Http\Output;
38
use OC\AppFramework\Core\API;
39
use OC\AppFramework\Middleware\MiddlewareDispatcher;
40
use OC\AppFramework\Middleware\Security\SecurityMiddleware;
41
use OC\AppFramework\Middleware\Security\CORSMiddleware;
42
use OC\AppFramework\Middleware\SessionMiddleware;
43
use OC\AppFramework\Utility\SimpleContainer;
44
use OCP\AppFramework\IApi;
45
use OCP\AppFramework\IAppContainer;
46
47
48
class DIContainer extends SimpleContainer implements IAppContainer {
49
50
	/**
51
	 * @var array
52
	 */
53
	private $middleWares = array();
54
55
	/**
56
	 * Put your class dependencies in here
57
	 * @param string $appName the name of the app
58
	 */
59
	public function __construct($appName, $urlParams = array()){
60
		parent::__construct();
61
		$this['AppName'] = $appName;
62
		$this['urlParams'] = $urlParams;
63
64
		/** @var \OC\ServerContainer $server */
65
		$server = $this->getServer();
66
		$server->registerAppContainer($appName, $this);
67
68
		// aliases
69
		$this->registerAlias('appName', 'AppName');
70
		$this->registerAlias('webRoot', 'WebRoot');
71
		$this->registerAlias('userId', 'UserId');
72
73
		/**
74
		 * Core services
75
		 */
76
		$this->registerService('OCP\\IAppConfig', function($c) {
77
			return $this->getServer()->getAppConfig();
78
		});
79
80
		$this->registerService('OCP\\App\\IAppManager', function($c) {
81
			return $this->getServer()->getAppManager();
82
		});
83
84
		$this->registerService('OCP\\AppFramework\\Http\\IOutput', function($c){
85
			return new Output($this->getServer()->getWebRoot());
86
		});
87
88
		$this->registerService('OCP\\IAvatarManager', function($c) {
89
			return $this->getServer()->getAvatarManager();
90
		});
91
92
		$this->registerService('OCP\\Activity\\IManager', function($c) {
93
			return $this->getServer()->getActivityManager();
94
		});
95
96
		$this->registerService('OCP\\ICache', function($c) {
97
			return $this->getServer()->getCache();
98
		});
99
100
		$this->registerService('OCP\\ICacheFactory', function($c) {
101
			return $this->getServer()->getMemCacheFactory();
102
		});
103
104
		$this->registerService('OC\\CapabilitiesManager', function($c) {
105
			return $this->getServer()->getCapabilitiesManager();
106
		});
107
108
		$this->registerService('OCP\Comments\ICommentsManager', function($c) {
109
			return $this->getServer()->getCommentsManager();
110
		});
111
112
		$this->registerService('OCP\\IConfig', function($c) {
113
			return $this->getServer()->getConfig();
114
		});
115
116
		$this->registerService('OCP\\Contacts\\IManager', function($c) {
117
			return $this->getServer()->getContactsManager();
118
		});
119
120
		$this->registerService('OCP\\IDateTimeZone', function($c) {
121
			return $this->getServer()->getDateTimeZone();
122
		});
123
124
		$this->registerService('OCP\\IDb', function($c) {
125
			return $this->getServer()->getDb();
126
		});
127
128
		$this->registerService('OCP\\IDBConnection', function($c) {
129
			return $this->getServer()->getDatabaseConnection();
130
		});
131
132
		$this->registerService('OCP\\Diagnostics\\IEventLogger', function($c) {
133
			return $this->getServer()->getEventLogger();
134
		});
135
136
		$this->registerService('OCP\\Diagnostics\\IQueryLogger', function($c) {
137
			return $this->getServer()->getQueryLogger();
138
		});
139
140
		$this->registerService('OCP\\Files\\IMimeTypeDetector', function($c) {
141
			return $this->getServer()->getMimeTypeDetector();
142
		});
143
144
		$this->registerService('OCP\\Files\\Config\\IMountProviderCollection', function($c) {
145
			return $this->getServer()->getMountProviderCollection();
146
		});
147
148
		$this->registerService('OCP\\Files\\IRootFolder', function($c) {
149
			return $this->getServer()->getRootFolder();
150
		});
151
152
		$this->registerService('OCP\\IGroupManager', function($c) {
153
			return $this->getServer()->getGroupManager();
154
		});
155
156
		$this->registerService('OCP\\IL10N', function($c) {
157
			return $this->getServer()->getL10N($c->query('AppName'));
158
		});
159
160
		$this->registerService('OCP\\L10N\\IFactory', function($c) {
161
			return $this->getServer()->getL10NFactory();
162
		});
163
164
		$this->registerService('OCP\\ILogger', function($c) {
165
			return $this->getServer()->getLogger();
166
		});
167
168
		$this->registerService('OCP\\BackgroundJob\\IJobList', function($c) {
169
			return $this->getServer()->getJobList();
170
		});
171
172
		$this->registerAlias('OCP\\AppFramework\\Utility\\IControllerMethodReflector', 'OC\AppFramework\Utility\ControllerMethodReflector');
173
		$this->registerAlias('ControllerMethodReflector', 'OCP\\AppFramework\\Utility\\IControllerMethodReflector');
174
175
		$this->registerService('OCP\\Files\\IMimeTypeDetector', function($c) {
176
			return $this->getServer()->getMimeTypeDetector();
177
		});
178
179
		$this->registerService('OCP\\INavigationManager', function($c) {
180
			return $this->getServer()->getNavigationManager();
181
		});
182
183
		$this->registerService('OCP\\Notification\IManager', function($c) {
184
			return $this->getServer()->getNotificationManager();
185
		});
186
187
		$this->registerService('OCP\\IPreview', function($c) {
188
			return $this->getServer()->getPreviewManager();
189
		});
190
191
		$this->registerService('OCP\\IRequest', function () {
192
			return $this->getServer()->getRequest();
193
		});
194
		$this->registerAlias('Request', 'OCP\\IRequest');
195
196
		$this->registerService('OCP\\ITagManager', function($c) {
0 ignored issues
show
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
197
			return $this->getServer()->getTagManager();
198
		});
199
200
		$this->registerService('OCP\\ITempManager', function($c) {
0 ignored issues
show
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
201
			return $this->getServer()->getTempManager();
202
		});
203
204
		$this->registerAlias('OCP\\AppFramework\\Utility\\ITimeFactory', 'OC\AppFramework\Utility\TimeFactory');
205
		$this->registerAlias('TimeFactory', 'OCP\\AppFramework\\Utility\\ITimeFactory');
206
207
208
		$this->registerService('OCP\\Route\\IRouter', function($c) {
0 ignored issues
show
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
209
			return $this->getServer()->getRouter();
210
		});
211
212
		$this->registerService('OCP\\ISearch', function($c) {
0 ignored issues
show
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
213
			return $this->getServer()->getSearch();
214
		});
215
216
		$this->registerService('OCP\\ISearch', function($c) {
0 ignored issues
show
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
217
			return $this->getServer()->getSearch();
218
		});
219
220
		$this->registerService('OCP\\Security\\ICrypto', function($c) {
0 ignored issues
show
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
221
			return $this->getServer()->getCrypto();
222
		});
223
224
		$this->registerService('OCP\\Security\\IHasher', function($c) {
0 ignored issues
show
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
225
			return $this->getServer()->getHasher();
226
		});
227
228
		$this->registerService('OCP\\Security\\ICredentialsManager', function($c) {
0 ignored issues
show
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
229
			return $this->getServer()->getCredentialsManager();
230
		});
231
232
		$this->registerService('OCP\\Security\\ISecureRandom', function($c) {
0 ignored issues
show
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
233
			return $this->getServer()->getSecureRandom();
234
		});
235
236
		$this->registerService('OCP\\Share\\IManager', function($c) {
0 ignored issues
show
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
237
			return $this->getServer()->getShareManager();
238
		});
239
240
		$this->registerService('OCP\\SystemTag\\ISystemTagManager', function() {
241
			return $this->getServer()->getSystemTagManager();
242
		});
243
244
		$this->registerService('OCP\\SystemTag\\ISystemTagObjectMapper', function() {
245
			return $this->getServer()->getSystemTagObjectMapper();
246
		});
247
248
		$this->registerService('OCP\\IURLGenerator', function($c) {
0 ignored issues
show
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
249
			return $this->getServer()->getURLGenerator();
250
		});
251
252
		$this->registerService('OCP\\IUserManager', function($c) {
253
			return $this->getServer()->getUserManager();
254
		});
255
256
		$this->registerService('OCP\\IUserSession', function($c) {
257
			return $this->getServer()->getUserSession();
258
		});
259
260
		$this->registerService('OCP\\ISession', function($c) {
261
			return $this->getServer()->getSession();
262
		});
263
264
		$this->registerService('OCP\\Security\\IContentSecurityPolicyManager', function($c) {
265
			return $this->getServer()->getContentSecurityPolicyManager();
266
		});
267
268
		$this->registerService('ServerContainer', function ($c) {
269
			return $this->getServer();
270
		});
271
		$this->registerAlias('OCP\\IServerContainer', 'ServerContainer');
272
273
		$this->registerService('Symfony\Component\EventDispatcher\EventDispatcherInterface', function ($c) {
274
			return $this->getServer()->getEventDispatcher();
275
		});
276
277
		$this->registerService('OCP\\AppFramework\\IAppContainer', function ($c) {
278
			return $c;
279
		});
280
281
		// commonly used attributes
282
		$this->registerService('UserId', function ($c) {
283
			return $c->query('OCP\\IUserSession')->getSession()->get('user_id');
284
		});
285
286
		$this->registerService('WebRoot', function ($c) {
287
			return $c->query('ServerContainer')->getWebRoot();
288
		});
289
290
291
		/**
292
		 * App Framework APIs
293
		 */
294
		$this->registerService('API', function($c){
295
			$c->query('OCP\\ILogger')->debug(
296
				'Accessing the API class is deprecated! Use the appropriate ' .
297
				'services instead!'
298
			);
299
			return new API($c['AppName']);
300
		});
301
302
		$this->registerService('Protocol', function($c){
303
			/** @var \OC\Server $server */
304
			$server = $c->query('ServerContainer');
305
			$protocol = $server->getRequest()->getHttpProtocol();
306
			return new Http($_SERVER, $protocol);
307
		});
308
309
		$this->registerService('Dispatcher', function($c) {
310
			return new Dispatcher(
311
				$c['Protocol'],
312
				$c['MiddlewareDispatcher'],
313
				$c['ControllerMethodReflector'],
314
				$c['Request']
315
			);
316
		});
317
318
319
		/**
320
		 * Middleware
321
		 */
322
		$app = $this;
323
		$this->registerService('SecurityMiddleware', function($c) use ($app){
324
			return new SecurityMiddleware(
325
				$c['Request'],
326
				$c['ControllerMethodReflector'],
327
				$app->getServer()->getNavigationManager(),
328
				$app->getServer()->getURLGenerator(),
329
				$app->getServer()->getLogger(),
330
				$c['AppName'],
331
				$app->isLoggedIn(),
332
				$app->isAdminUser(),
333
				$app->getServer()->getContentSecurityPolicyManager()
334
			);
335
		});
336
337
		$this->registerService('CORSMiddleware', function($c) {
338
			return new CORSMiddleware(
339
				$c['Request'],
340
				$c['ControllerMethodReflector'],
341
				$c['OCP\IUserSession']
342
			);
343
		});
344
345
		$this->registerService('SessionMiddleware', function($c) use ($app) {
346
			return new SessionMiddleware(
347
				$c['Request'],
348
				$c['ControllerMethodReflector'],
349
				$app->getServer()->getSession()
350
			);
351
		});
352
353
		$middleWares = &$this->middleWares;
354
		$this->registerService('MiddlewareDispatcher', function($c) use (&$middleWares) {
355
			$dispatcher = new MiddlewareDispatcher();
356
			$dispatcher->registerMiddleware($c['CORSMiddleware']);
357
			$dispatcher->registerMiddleware($c['SecurityMiddleware']);
358
359
			foreach($middleWares as $middleWare) {
360
				$dispatcher->registerMiddleware($c[$middleWare]);
361
			}
362
363
			$dispatcher->registerMiddleware($c['SessionMiddleware']);
364
			return $dispatcher;
365
		});
366
367
	}
368
369
370
	/**
371
	 * @deprecated implements only deprecated methods
372
	 * @return IApi
373
	 */
374
	function getCoreApi()
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
375
	{
376
		return $this->query('API');
377
	}
378
379
	/**
380
	 * @return \OCP\IServerContainer
381
	 */
382
	function getServer()
383
	{
384
		return OC::$server;
385
	}
386
387
	/**
388
	 * @param string $middleWare
389
	 * @return boolean|null
390
	 */
391
	function registerMiddleWare($middleWare) {
392
		array_push($this->middleWares, $middleWare);
393
	}
394
395
	/**
396
	 * used to return the appname of the set application
397
	 * @return string the name of your application
398
	 */
399
	function getAppName() {
400
		return $this->query('AppName');
401
	}
402
403
	/**
404
	 * @deprecated use IUserSession->isLoggedIn()
405
	 * @return boolean
406
	 */
407
	function isLoggedIn() {
408
		return \OC_User::isLoggedIn();
409
	}
410
411
	/**
412
	 * @deprecated use IGroupManager->isAdmin($userId)
413
	 * @return boolean
414
	 */
415
	function isAdminUser() {
416
		$uid = $this->getUserId();
417
		return \OC_User::isAdminUser($uid);
418
	}
419
420
	private function getUserId() {
421
		return $this->getServer()->getSession()->get('user_id');
422
	}
423
424
	/**
425
	 * @deprecated use the ILogger instead
426
	 * @param string $message
427
	 * @param string $level
428
	 * @return mixed
429
	 */
430
	function log($message, $level) {
431
		switch($level){
432
			case 'debug':
433
				$level = \OCP\Util::DEBUG;
434
				break;
435
			case 'info':
436
				$level = \OCP\Util::INFO;
437
				break;
438
			case 'warn':
439
				$level = \OCP\Util::WARN;
440
				break;
441
			case 'fatal':
442
				$level = \OCP\Util::FATAL;
443
				break;
444
			default:
445
				$level = \OCP\Util::ERROR;
446
				break;
447
		}
448
		\OCP\Util::writeLog($this->getAppName(), $message, $level);
449
	}
450
451
	/**
452
	 * Register a capability
453
	 *
454
	 * @param string $serviceName e.g. 'OCA\Files\Capabilities'
455
	 */
456
	public function registerCapability($serviceName) {
457
		$this->query('OC\CapabilitiesManager')->registerCapability(function() use ($serviceName) {
458
			return $this->query($serviceName);
459
		});
460
461
	}
462
}
463