This commit is contained in:
Yuri Kuznetsov
2013-11-19 18:02:35 +02:00
parent b999d7018d
commit 9246c4bd12
7 changed files with 131 additions and 125 deletions

View File

@@ -7,7 +7,7 @@ use Espo\Core\Utils as Utils;
class Layout extends \Espo\Core\Controllers\Base
{
public function read($params, $data)
public function actionRead($params, $data)
{
$data = $this->getContainer()->get('layout')->get($params['controller'], $params['name']);
@@ -15,7 +15,7 @@ class Layout extends \Espo\Core\Controllers\Base
}
public function update($params, $data)
public function actionUpdate($params, $data)
{
$result= $this->getContainer()->get('layout')->set($data, $params['controller'], $params['name']);
@@ -29,7 +29,7 @@ class Layout extends \Espo\Core\Controllers\Base
}
public function patch($params, $data)
public function actionPatch($params, $data)
{
$result= $this->getContainer()->get('layout')->merge($data, $params['controller'], $params['name']);
@@ -45,4 +45,4 @@ class Layout extends \Espo\Core\Controllers\Base
}
?>
?>

View File

@@ -6,15 +6,15 @@ namespace Espo\Controllers;
class Metadata extends \Espo\Core\Controllers\Base
{
public function read($params, $data)
public function actionRead($params, $data)
{
$data= $this->getContainer()->get('metadata')->get(true);
$data = $this->getContainer()->get('metadata')->get(true);
return array($data, 'Cannot reach metadata data');
}
public function update($params, $data)
public function actionUpdate($params, $data)
{
$result = $this->getContainer()->get('metadata')->set($data, $params['type'], $params['scope']);
@@ -30,4 +30,4 @@ class Metadata extends \Espo\Core\Controllers\Base
}
?>
?>

View File

@@ -7,7 +7,7 @@ use Espo\Core\Utils as Utils;
class Settings extends \Espo\Core\Controllers\Base
{
public function read($params, $data)
public function actionRead($params, $data)
{
$isAdmin = $this->getContainer()->get('user')->isAdmin();
@@ -17,7 +17,7 @@ class Settings extends \Espo\Core\Controllers\Base
}
public function patch($params, $data)
public function actionPatch($params, $data)
{
$isAdmin = $this->getContainer()->get('user')->isAdmin();
@@ -35,4 +35,4 @@ class Settings extends \Espo\Core\Controllers\Base
}
?>
?>

View File

@@ -5,10 +5,6 @@ namespace Espo\Core;
class Application
{
protected static $apps = array();
private $metadata;
private $container;
@@ -63,27 +59,9 @@ class Application
$this->routeHooks();
$this->routes();
$this->getSlim()->run();
static::$apps[$name] = $this;
// TODO place routing HERE
// dispatch which controller to use
// $this->controller = new $controllerClassName($this->container, $this->serviceFactory);
// call needed controller method $this->$method($params, $data)
// dont't return anything here
}
public static function getInstance($name = 'default')
{
return isset(static::$apps[$name]) ? static::$apps[$name] : null;
}
protected function routeHooks()
{
$container = $this->getContainer();
@@ -91,7 +69,7 @@ class Application
$serviceFactory = $this->getServiceFactory();
//check user credentials
$this->getSlim()->add(new \Espo\Core\Utils\Api\Auth( $container ));
$this->getSlim()->add(new \Espo\Core\Utils\Api\Auth($container));
//convert all url params to camel case format
$this->getSlim()->hook('slim.before.dispatch', function () use ($slim, $container) {
@@ -99,68 +77,63 @@ class Application
$conditions = $slim->router()->getCurrentRoute()->getConditions();
$upperList = isset($conditions['upper']) ? $conditions['upper'] : array();
$routeParams= $slim->router()->getCurrentRoute()->getParams();
$routeParams = $slim->router()->getCurrentRoute()->getParams();
if (!empty($routeParams)) {
foreach($routeParams as $name => &$param) {
foreach ($routeParams as $name => &$param) {
$isUpper = in_array($name, $upperList) ? true : false;
$param= \Espo\Core\Utils\Util::toCamelCase($param, $isUpper);
$param = \Espo\Core\Utils\Util::toCamelCase($param, $isUpper);
}
$slim->router()->getCurrentRoute()->setParams($routeParams);
}
});
//END: convert all url params to camel case format
$this->getSlim()->hook('slim.before.dispatch', function () use ($slim, $container, $serviceFactory) {
$currentRoute = $slim->router()->getCurrentRoute();
$conditions = $currentRoute->getConditions();
$route = $slim->router()->getCurrentRoute();
$conditions = $route->getConditions();
if (isset($conditions['useController']) && $conditions['useController'] == false) {
return;
}
$espoController = call_user_func( $slim->router()->getCurrentRoute()->getCallable() );
$espoKeys = is_array($espoController) ? array_keys($espoController) : array();
$routeOptions = call_user_func($route->getCallable());
$routeKeys = is_array($routeOptions) ? array_keys($routeOptions) : array();
if (!in_array('controller', $espoKeys, true)) {
return $container->get('rest')->render($espoController);
if (!in_array('controller', $routeKeys, true)) {
return $container->get('rest')->render($routeOptions);
}
$params = $route->getParams();
$data = $slim->request()->getBody();
$params = $currentRoute->getParams();
$data = $slim->request()->getBody();
//prepare controller Params
$controllerParams = array();
$controllerParams['HttpMethod'] = strtolower($slim->request()->getMethod());
foreach($espoController as $key => $val) {
if (strstr($val, ':')) {
$paramName = str_replace(':', '', $val);
$val = $params[$paramName];
foreach ($routeOptions as $key => $value) {
if (strstr($value, ':')) {
$paramName = str_replace(':', '', $value);
$value = $params[$paramName];
}
$controllerParams[$key] = $val;
$controllerParams[$key] = $value;
}
$controllerName = $controllerParams['controller'];
if (!empty($controllerParams['action'])) {
$actionName = $controllerParams['action'];
} else {
$httpMethod = strtolower($slim->request()->getMethod());
$actionName = $container->get('config')->get('crud')->$httpMethod;
}
$controllerParams['container'] = $container;
$controllerParams['serviceFactory'] = $serviceFactory;
//END: prepare controller Params
$result = $container->get('controllerManager')->call($controllerParams, $params, $data);
$controllerManager = new \Espo\Core\ControllerManager($container);
$result = $controllerManager->execute($controllerName, $actionName, $params, $data);
return $container->get('rest')->render($result->data, $result->errMessage, $result->errCode);
});
//return json response
$this->getSlim()->hook('slim.after.router', function () use (&$slim) {
$slim->contentType('application/json');
//$routes->contentType('text/javascript');
});
//END: return json response
}
@@ -213,7 +186,6 @@ EOT;
return array(
'controller' => 'Layout',
'scope' => ':controller',
'action' => ':name',
);
})->conditions( array('upper' => array('controller')) );
@@ -221,7 +193,6 @@ EOT;
return array(
'controller' => 'Layout',
'scope' => ':controller',
'action' => ':name',
);
})->conditions( array('upper' => array('controller')) );
@@ -229,7 +200,6 @@ EOT;
return array(
'controller' => 'Layout',
'scope' => ':controller',
'action' => ':name',
);
})->via('PATCH')->conditions( array('upper' => array('controller')) );
//END: LAYOUT

View File

@@ -19,10 +19,10 @@ class Container
public function get($name)
{
if (!empty($this->data[$name])) {
return $this->data[$name];
if (empty($this->data[$name])) {
$this->load($name);
}
$this->load($name);
return $this->data[$name];
}
@@ -123,14 +123,6 @@ class Container
);
}
private function loadControllerManager()
{
$this->data['controllerManager'] = new \Espo\Core\Controllers\Manager(
$this->get('config'),
$this->get('metadata')
);
}
private function loadDatetime()
{
$this->data['datetime'] = new \Espo\Core\Utils\Datetime(

View File

@@ -1,23 +1,26 @@
<?php
namespace Espo\Core\Controllers;
namespace Espo\Core;
use \Espo\Core\Utils\Util;
class Manager
class ControllerManager
{
private $config;
private $metadata;
private $container;
public function __construct(\Espo\Core\Utils\Config $config, \Espo\Core\Utils\Metadata $metadata)
{
$this->config = $config;
$this->metadata = $metadata;
public function __construct(\Espo\Core\Container $container)
{
$this->container = $container;
$this->config = $this->container->get('config');
$this->metadata = $this->container->get('metadata');
}
protected function getConfig()
{
return $this->config;
@@ -27,7 +30,55 @@ class Manager
{
return $this->metadata;
}
public function execute($controllerName, $actionName, $params, $data)
{
$customeClassName = '\\Espo\\Custom\\Controllers\\' . $controllerName;
if (class_exists($customeClassName)) {
$controllerClassName = $customeClassName;
} else {
$moduleName = $this->metadata->getScopeModuleName($controllerName);
if ($moduleName) {
$controllerClassName = '\\Espo\\Modules\\' . $moduleName . '\\Controllers\\' . $controllerName;
} else {
$controllerClassName = '\\Espo\\Controllers\\' . $controllerName;
}
}
$controller = new $controllerClassName($this->container, $this->serviceFactory);
$actionNameUcfirst = ucfirst($actionName);
$beforeMethodName = 'before' . $actionNameUcfirst;
if (method_exists($controller, $beforeMethodName)) {
$controller->$beforeMethodName($params, $data);
}
$actionMethodName = 'action' . $actionNameUcfirst;
$result = $controller->$actionMethodName($params, $data);
$afterMethodName = 'after' . $actionNameUcfirst;
if (method_exists($controller, $afterMethodName)) {
$controller->$afterMethodName($params, $data);
}
if (is_array($result)) {
$returnResult = array_values($result);
if (!empty($returnResult[2])) {
return $this->response($returnResult[0], $returnResult[1], $returnResult[2]);
}
if (!empty($returnResult[1])) {
return $this->response($returnResult[0], $returnResult[1]);
}
if (!empty($returnResult[0])) {
return $this->response($returnResult[0]);
}
return $this->response(false, 'Cannot find requested controller', 404);
}
return $this->response($result);
}
/**
* Manage of all controllers
@@ -38,17 +89,17 @@ class Manager
*
* @return array
*/
public function call($controllerParams, $params, $data = '')
/*public function call($controllerParams, $params, $data = '')
{
$config = $this->getConfig();
$espoPath = $this->getConfig()->get('espoPath');
$controllerPath = Util::concatPath($espoPath, $this->getConfig()->get('controllerPath'));
$espoPath = $config->get('espoPath');
$controllerPath= Util::concatPath($espoPath, $config->get('controllerPath'));
$crud = $config->get('crud');
$baseAction = $crud->$controllerParams['HttpMethod'];
$crud = $this->getConfig()->get('crud');
$baseAction = $crud->$controllerParams['httpMethod'];
if (empty($baseAction)) {
return $this->response(false, 'Cannot find action for HTTP Method ['.$controllerParams['HttpMethod'].']', 404);
return $this->response(false, 'Cannot find action for HTTP Method ['.$controllerParams['httpMethod'].']', 404);
}
$controller = (object) array(
@@ -63,12 +114,11 @@ class Manager
return $this->response(false, 'Controller for Scope ['.$controller->scope.'] does not exist.', 404);
}
//define default values
$classInfo = new \stdClass();
$classInfo->name = $this->getClassName(Util::concatPath($espoPath, 'Core/Base'), 'Controller');
$classInfo->path = $this->getClassPath($classInfo->name);
$classInfo->method = $this->getDefinedMethod($classInfo->name, $controller->baseAction, $controller->action);
$classInfo->method = $this->getDefinedMethod($classInfo->name, $controller->baseAction, $controller->action, 'action');
//Espo\Controlles\Layout and Custom\Espo\Controlles\Layout
@@ -79,7 +129,7 @@ class Manager
if (!empty($controller->scope)) {
//path in Modules dir
$controllerDir = Util::concatPath( $this->getMetadata()->getScopePath($controller->scope), $config->get('controllerPath') );
$controllerDir = Util::concatPath( $this->getMetadata()->getScopePath($controller->scope), $this->getConfig()->get('controllerPath') );
//ex. Modules\Crm\Controllers\Layout and Cusom\Modules\Crm\Controllers\Layout
$controllerClass = $this->getClassName($controllerDir, $controller->name);
@@ -105,8 +155,7 @@ class Manager
//call class method
$className = $classInfo->name;
$classMethod = $classInfo->method;
require_once($classInfo->path);
$class = new $className($controllerParams['container'], $controllerParams['serviceFactory']);
//call before method if exists: beforeRead, beforeDetailSmall, beforeReadDetailSmall
@@ -145,7 +194,7 @@ class Manager
}
return $this->response($result);
}
}*/
@@ -156,7 +205,7 @@ class Manager
*
* @return sting
*/
function getDefinedMethod($className, $baseAction, $action, $prefix = '')
/*function getDefinedMethod($className, $baseAction, $action, $prefix = '')
{
$allActions= get_class_methods($className);
$classMethod = '';
@@ -167,7 +216,7 @@ class Manager
//method as 'read'
$prefixBaseAction = Util::toCamelCase($prefix.$baseAction);
if ( method_exists($className, $prefixBaseAction) ) {
if (method_exists($className, $prefixBaseAction) ) {
$classMethod = $prefixBaseAction;
}
@@ -177,7 +226,7 @@ class Manager
//method as 'detailSmall'
$prefixAction = Util::toCamelCase($prefix.$action);
if ( method_exists($className, $prefixAction) ) {
if (method_exists($className, $prefixAction) ) {
$classMethod = $prefixAction;
}
@@ -188,7 +237,7 @@ class Manager
}
return $classMethod;
}
}*/
/**
* If method exists, then redefine classInfo
@@ -200,10 +249,10 @@ class Manager
*
* @return object
*/
protected function setClassInfo($className, \stdClass $classInfo, \stdClass $controller, $isCustom = true)
/*protected function setClassInfo($className, \stdClass $classInfo, \stdClass $controller, $isCustom = true)
{
$classPath = $this->getClassPath($className);
$classMethod = $this->getDefinedMethod($className, $controller->baseAction, $controller->action);
$classMethod = $this->getDefinedMethod($className, $controller->baseAction, $controller->action, 'action');
if (file_exists($classPath) && !empty($classMethod) ) {
$classInfo->name = $className;
@@ -218,7 +267,7 @@ class Manager
}
return $classInfo;
}
}*/
@@ -230,14 +279,14 @@ class Manager
*
* @return string
*/
protected function getClassName($path, $name = '')
/*protected function getClassName($path, $name = '')
{
if (!empty($name)) {
$path = Util::concatPath($path, Util::toCamelCase($name, true));
}
return Util::toFormat($path, '\\');
}
}*/
/**
@@ -248,14 +297,14 @@ class Manager
*
* @return string
*/
protected function getClassPath($path, $name = '')
/*protected function getClassPath($path, $name = '')
{
if (!empty($name)) {
$path = Util::concatPath($path, Util::toCamelCase($name, true));
}
return Util::concatPath('application', Util::toFormat($path, '/').'.php');
}
}*/
/**
@@ -277,9 +326,4 @@ class Manager
}
}
?>

View File

@@ -25,27 +25,27 @@ class Base
public function read($params, $data)
public function actionRead($params, $data)
{
}
public function update($params, $data)
public function actionUpdate($params, $data)
{
}
public function patch($params, $data)
public function actionPatch($params, $data)
{
}
public function create($params, $data)
public function actionCreate($params, $data)
{
}
public function delete($params, $data)
public function actionDelete($params, $data)
{
}
@@ -54,4 +54,4 @@ class Base
}
?>
?>