Blame view

sources/lib/private/appframework/dependencyinjection/dicontainer.php 5.03 KB
31b7f2792   Kload   Upgrade to ownclo...
1
2
3
4
5
6
  <?php
  
  /**
   * ownCloud - App Framework
   *
   * @author Bernhard Posselt
6d9380f96   Cédric Dupont   Update sources OC...
7
   * @copyright 2012 Bernhard Posselt <dev@bernhard-posselt.com>
31b7f2792   Kload   Upgrade to ownclo...
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
   *
   * This library is free software; you can redistribute it and/or
   * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
   * License as published by the Free Software Foundation; either
   * version 3 of the License, or any later version.
   *
   * This library is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
   *
   * You should have received a copy of the GNU Affero General Public
   * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
   *
   */
  
  
  namespace OC\AppFramework\DependencyInjection;
  
  use OC\AppFramework\Http;
  use OC\AppFramework\Http\Request;
  use OC\AppFramework\Http\Dispatcher;
  use OC\AppFramework\Core\API;
  use OC\AppFramework\Middleware\MiddlewareDispatcher;
  use OC\AppFramework\Middleware\Security\SecurityMiddleware;
6d9380f96   Cédric Dupont   Update sources OC...
33
  use OC\AppFramework\Middleware\Security\CORSMiddleware;
31b7f2792   Kload   Upgrade to ownclo...
34
35
  use OC\AppFramework\Utility\SimpleContainer;
  use OC\AppFramework\Utility\TimeFactory;
6d9380f96   Cédric Dupont   Update sources OC...
36
  use OC\AppFramework\Utility\ControllerMethodReflector;
31b7f2792   Kload   Upgrade to ownclo...
37
38
  use OCP\AppFramework\IApi;
  use OCP\AppFramework\IAppContainer;
31b7f2792   Kload   Upgrade to ownclo...
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
  use OCP\AppFramework\Middleware;
  use OCP\IServerContainer;
  
  
  class DIContainer extends SimpleContainer implements IAppContainer{
  
  	/**
  	 * @var array
  	 */
  	private $middleWares = array();
  
  	/**
  	 * Put your class dependencies in here
  	 * @param string $appName the name of the app
  	 */
  	public function __construct($appName, $urlParams = array()){
  
  		$this['AppName'] = $appName;
  		$this['urlParams'] = $urlParams;
  
  		$this->registerParameter('ServerContainer', \OC::$server);
  
  		$this['API'] = $this->share(function($c){
  			return new API($c['AppName']);
  		});
  
  		/**
  		 * Http
  		 */
  		$this['Request'] = $this->share(function($c) {
  			/** @var $c SimpleContainer */
  			/** @var $server IServerContainer */
  			$server = $c->query('ServerContainer');
  			$server->registerParameter('urlParams', $c['urlParams']);
  			return $server->getRequest();
  		});
  
  		$this['Protocol'] = $this->share(function($c){
  			if(isset($_SERVER['SERVER_PROTOCOL'])) {
  				return new Http($_SERVER, $_SERVER['SERVER_PROTOCOL']);
  			} else {
  				return new Http($_SERVER);
  			}
  		});
  
  		$this['Dispatcher'] = $this->share(function($c) {
6d9380f96   Cédric Dupont   Update sources OC...
85
86
87
88
89
90
  			return new Dispatcher(
  				$c['Protocol'],
  				$c['MiddlewareDispatcher'],
  				$c['ControllerMethodReflector'],
  				$c['Request']
  			);
31b7f2792   Kload   Upgrade to ownclo...
91
92
93
94
95
96
97
98
  		});
  
  
  		/**
  		 * Middleware
  		 */
  		$app = $this;
  		$this['SecurityMiddleware'] = $this->share(function($c) use ($app){
6d9380f96   Cédric Dupont   Update sources OC...
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
  			return new SecurityMiddleware(
  				$c['Request'],
  				$c['ControllerMethodReflector'],
  				$app->getServer()->getNavigationManager(),
  				$app->getServer()->getURLGenerator(),
  				$app->getServer()->getLogger(),
  				$c['AppName'],
  				$app->isLoggedIn(),
  				$app->isAdminUser()
  			);
  		});
  
  		$this['CORSMiddleware'] = $this->share(function($c) {
  			return new CORSMiddleware(
  				$c['Request'],
  				$c['ControllerMethodReflector']
  			);
31b7f2792   Kload   Upgrade to ownclo...
116
  		});
837968727   Kload   [enh] Upgrade to ...
117
118
  		$middleWares = &$this->middleWares;
  		$this['MiddlewareDispatcher'] = $this->share(function($c) use (&$middleWares) {
31b7f2792   Kload   Upgrade to ownclo...
119
120
  			$dispatcher = new MiddlewareDispatcher();
  			$dispatcher->registerMiddleware($c['SecurityMiddleware']);
6d9380f96   Cédric Dupont   Update sources OC...
121
  			$dispatcher->registerMiddleware($c['CORSMiddleware']);
31b7f2792   Kload   Upgrade to ownclo...
122
123
  
  			foreach($middleWares as $middleWare) {
837968727   Kload   [enh] Upgrade to ...
124
  				$dispatcher->registerMiddleware($c[$middleWare]);
31b7f2792   Kload   Upgrade to ownclo...
125
126
127
128
129
130
131
132
133
134
135
136
  			}
  
  			return $dispatcher;
  		});
  
  
  		/**
  		 * Utilities
  		 */
  		$this['TimeFactory'] = $this->share(function($c){
  			return new TimeFactory();
  		});
6d9380f96   Cédric Dupont   Update sources OC...
137
138
139
  		$this['ControllerMethodReflector'] = $this->share(function($c) {
  			return new ControllerMethodReflector();
  		});
31b7f2792   Kload   Upgrade to ownclo...
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
  
  	}
  
  
  	/**
  	 * @return IApi
  	 */
  	function getCoreApi()
  	{
  		return $this->query('API');
  	}
  
  	/**
  	 * @return \OCP\IServerContainer
  	 */
  	function getServer()
  	{
  		return $this->query('ServerContainer');
  	}
  
  	/**
837968727   Kload   [enh] Upgrade to ...
161
162
  	 * @param string $middleWare
  	 * @return boolean|null
31b7f2792   Kload   Upgrade to ownclo...
163
  	 */
837968727   Kload   [enh] Upgrade to ...
164
  	function registerMiddleWare($middleWare) {
31b7f2792   Kload   Upgrade to ownclo...
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
  		array_push($this->middleWares, $middleWare);
  	}
  
  	/**
  	 * used to return the appname of the set application
  	 * @return string the name of your application
  	 */
  	function getAppName() {
  		return $this->query('AppName');
  	}
  
  	/**
  	 * @return boolean
  	 */
  	function isLoggedIn() {
  		return \OC_User::isLoggedIn();
  	}
  
  	/**
  	 * @return boolean
  	 */
  	function isAdminUser() {
  		$uid = $this->getUserId();
  		return \OC_User::isAdminUser($uid);
  	}
  
  	private function getUserId() {
  		return \OC::$session->get('user_id');
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
196
197
  	 * @param string $message
  	 * @param string $level
31b7f2792   Kload   Upgrade to ownclo...
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
  	 * @return mixed
  	 */
  	function log($message, $level) {
  		switch($level){
  			case 'debug':
  				$level = \OCP\Util::DEBUG;
  				break;
  			case 'info':
  				$level = \OCP\Util::INFO;
  				break;
  			case 'warn':
  				$level = \OCP\Util::WARN;
  				break;
  			case 'fatal':
  				$level = \OCP\Util::FATAL;
  				break;
  			default:
  				$level = \OCP\Util::ERROR;
  				break;
  		}
  		\OCP\Util::writeLog($this->getAppName(), $message, $level);
  	}
  }