2013-08-17 13:16:48 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ownCloud - App Framework
|
|
|
|
*
|
|
|
|
* @author Bernhard Posselt
|
2014-05-07 00:25:05 +04:00
|
|
|
* @copyright 2012 Bernhard Posselt <dev@bernhard-posselt.com>
|
2013-08-17 13:16:48 +04:00
|
|
|
*
|
|
|
|
* 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;
|
|
|
|
|
2013-10-23 07:57:34 +04:00
|
|
|
use OC\AppFramework\Http;
|
2013-08-17 13:16:48 +04:00
|
|
|
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;
|
2014-05-08 13:47:18 +04:00
|
|
|
use OC\AppFramework\Middleware\Security\CORSMiddleware;
|
2014-10-21 16:53:10 +04:00
|
|
|
use OC\AppFramework\Middleware\SessionMiddleware;
|
2013-08-20 19:21:14 +04:00
|
|
|
use OC\AppFramework\Utility\SimpleContainer;
|
2013-08-17 13:16:48 +04:00
|
|
|
use OC\AppFramework\Utility\TimeFactory;
|
2014-05-06 18:29:19 +04:00
|
|
|
use OC\AppFramework\Utility\ControllerMethodReflector;
|
2013-08-21 03:02:15 +04:00
|
|
|
use OCP\AppFramework\IApi;
|
|
|
|
use OCP\AppFramework\IAppContainer;
|
2013-10-07 02:33:54 +04:00
|
|
|
use OCP\AppFramework\Middleware;
|
2013-09-25 12:30:48 +04:00
|
|
|
use OCP\IServerContainer;
|
2013-08-17 13:16:48 +04:00
|
|
|
|
|
|
|
|
2013-08-21 03:02:15 +04:00
|
|
|
class DIContainer extends SimpleContainer implements IAppContainer{
|
2013-08-17 13:16:48 +04:00
|
|
|
|
2013-09-27 19:15:26 +04:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2013-09-28 22:40:25 +04:00
|
|
|
private $middleWares = array();
|
2013-08-17 13:16:48 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Put your class dependencies in here
|
|
|
|
* @param string $appName the name of the app
|
|
|
|
*/
|
2013-10-01 21:01:52 +04:00
|
|
|
public function __construct($appName, $urlParams = array()){
|
2013-08-17 13:16:48 +04:00
|
|
|
|
|
|
|
$this['AppName'] = $appName;
|
2013-10-01 21:01:52 +04:00
|
|
|
$this['urlParams'] = $urlParams;
|
2013-08-17 13:16:48 +04:00
|
|
|
|
2013-08-21 03:02:15 +04:00
|
|
|
$this->registerParameter('ServerContainer', \OC::$server);
|
|
|
|
|
2014-11-03 15:10:03 +03:00
|
|
|
$this->registerService('API', function($c){
|
2013-08-17 13:16:48 +04:00
|
|
|
return new API($c['AppName']);
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Http
|
|
|
|
*/
|
2014-11-03 15:10:03 +03:00
|
|
|
$this->registerService('Request', function($c) {
|
2013-09-25 12:30:48 +04:00
|
|
|
/** @var $c SimpleContainer */
|
2014-10-21 16:53:10 +04:00
|
|
|
/** @var $server SimpleContainer */
|
2013-09-25 12:30:48 +04:00
|
|
|
$server = $c->query('ServerContainer');
|
2013-10-01 21:01:52 +04:00
|
|
|
$server->registerParameter('urlParams', $c['urlParams']);
|
2014-10-21 16:53:10 +04:00
|
|
|
/** @var $server IServerContainer */
|
2013-09-25 12:30:48 +04:00
|
|
|
return $server->getRequest();
|
2013-08-17 13:16:48 +04:00
|
|
|
});
|
|
|
|
|
2014-11-03 15:10:03 +03:00
|
|
|
$this->registerService('Protocol', function($c){
|
2013-08-17 13:16:48 +04:00
|
|
|
if(isset($_SERVER['SERVER_PROTOCOL'])) {
|
|
|
|
return new Http($_SERVER, $_SERVER['SERVER_PROTOCOL']);
|
|
|
|
} else {
|
|
|
|
return new Http($_SERVER);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-11-03 15:10:03 +03:00
|
|
|
$this->registerService('Dispatcher', function($c) {
|
2014-05-06 18:29:19 +04:00
|
|
|
return new Dispatcher(
|
2014-05-28 04:12:01 +04:00
|
|
|
$c['Protocol'],
|
|
|
|
$c['MiddlewareDispatcher'],
|
2014-05-06 18:31:08 +04:00
|
|
|
$c['ControllerMethodReflector'],
|
|
|
|
$c['Request']
|
2014-05-06 18:29:19 +04:00
|
|
|
);
|
2013-08-17 13:16:48 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Middleware
|
|
|
|
*/
|
2013-10-22 11:29:17 +04:00
|
|
|
$app = $this;
|
2014-11-03 15:10:03 +03:00
|
|
|
$this->registerService('SecurityMiddleware', function($c) use ($app){
|
2014-05-06 18:29:19 +04:00
|
|
|
return new SecurityMiddleware(
|
2014-05-28 04:12:01 +04:00
|
|
|
$c['Request'],
|
|
|
|
$c['ControllerMethodReflector'],
|
|
|
|
$app->getServer()->getNavigationManager(),
|
|
|
|
$app->getServer()->getURLGenerator(),
|
|
|
|
$app->getServer()->getLogger(),
|
|
|
|
$c['AppName'],
|
|
|
|
$app->isLoggedIn(),
|
|
|
|
$app->isAdminUser()
|
2014-05-06 18:29:19 +04:00
|
|
|
);
|
2013-08-17 13:16:48 +04:00
|
|
|
});
|
|
|
|
|
2014-11-03 15:10:03 +03:00
|
|
|
$this->registerService('CORSMiddleware', function($c) {
|
2014-05-11 19:55:59 +04:00
|
|
|
return new CORSMiddleware(
|
|
|
|
$c['Request'],
|
|
|
|
$c['ControllerMethodReflector']
|
|
|
|
);
|
2014-05-08 13:47:18 +04:00
|
|
|
});
|
|
|
|
|
2014-11-03 15:10:03 +03:00
|
|
|
$this->registerService('SessionMiddleware', function($c) use ($app) {
|
2014-10-21 16:53:10 +04:00
|
|
|
return new SessionMiddleware(
|
|
|
|
$c['Request'],
|
|
|
|
$c['ControllerMethodReflector'],
|
|
|
|
$app->getServer()->getSession()
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2014-04-11 22:50:49 +04:00
|
|
|
$middleWares = &$this->middleWares;
|
2014-11-03 15:10:03 +03:00
|
|
|
$this->registerService('MiddlewareDispatcher', function($c) use (&$middleWares) {
|
2013-08-17 13:16:48 +04:00
|
|
|
$dispatcher = new MiddlewareDispatcher();
|
|
|
|
$dispatcher->registerMiddleware($c['SecurityMiddleware']);
|
2014-05-08 13:47:18 +04:00
|
|
|
$dispatcher->registerMiddleware($c['CORSMiddleware']);
|
2013-08-17 13:16:48 +04:00
|
|
|
|
2014-04-11 22:50:49 +04:00
|
|
|
foreach($middleWares as $middleWare) {
|
2014-04-10 01:59:57 +04:00
|
|
|
$dispatcher->registerMiddleware($c[$middleWare]);
|
2013-09-27 19:15:26 +04:00
|
|
|
}
|
|
|
|
|
2014-10-21 16:53:10 +04:00
|
|
|
$dispatcher->registerMiddleware($c['SessionMiddleware']);
|
2013-08-17 13:16:48 +04:00
|
|
|
return $dispatcher;
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Utilities
|
|
|
|
*/
|
2014-11-03 15:10:03 +03:00
|
|
|
$this->registerService('TimeFactory', function($c){
|
2013-08-17 13:16:48 +04:00
|
|
|
return new TimeFactory();
|
|
|
|
});
|
|
|
|
|
2014-11-03 15:10:03 +03:00
|
|
|
$this->registerService('ControllerMethodReflector', function($c) {
|
2014-05-06 18:29:19 +04:00
|
|
|
return new ControllerMethodReflector();
|
|
|
|
});
|
2013-08-17 13:16:48 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-21 03:02:15 +04:00
|
|
|
/**
|
|
|
|
* @return IApi
|
|
|
|
*/
|
|
|
|
function getCoreApi()
|
|
|
|
{
|
|
|
|
return $this->query('API');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-08-31 23:34:29 +04:00
|
|
|
* @return \OCP\IServerContainer
|
2013-08-21 03:02:15 +04:00
|
|
|
*/
|
|
|
|
function getServer()
|
|
|
|
{
|
|
|
|
return $this->query('ServerContainer');
|
|
|
|
}
|
2013-09-25 12:30:48 +04:00
|
|
|
|
|
|
|
/**
|
2014-04-10 01:59:57 +04:00
|
|
|
* @param string $middleWare
|
2014-02-06 19:30:58 +04:00
|
|
|
* @return boolean|null
|
2013-09-25 12:30:48 +04:00
|
|
|
*/
|
2014-04-10 01:59:57 +04:00
|
|
|
function registerMiddleWare($middleWare) {
|
2013-09-27 19:15:26 +04:00
|
|
|
array_push($this->middleWares, $middleWare);
|
2013-09-25 12:30:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* used to return the appname of the set application
|
|
|
|
* @return string the name of your application
|
|
|
|
*/
|
|
|
|
function getAppName() {
|
|
|
|
return $this->query('AppName');
|
|
|
|
}
|
2013-10-07 02:33:54 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
function isLoggedIn() {
|
|
|
|
return \OC_User::isLoggedIn();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
function isAdminUser() {
|
|
|
|
$uid = $this->getUserId();
|
|
|
|
return \OC_User::isAdminUser($uid);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getUserId() {
|
2014-07-16 21:40:22 +04:00
|
|
|
return \OC::$server->getSession()->get('user_id');
|
2013-10-07 02:33:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-12 00:51:30 +04:00
|
|
|
* @param string $message
|
|
|
|
* @param string $level
|
2013-10-07 02:33:54 +04:00
|
|
|
* @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);
|
|
|
|
}
|
2013-08-17 13:16:48 +04:00
|
|
|
}
|