container = new Container(); $GLOBALS['log'] = $this->log = $this->container->get('log'); set_error_handler(array($this->getLog(), 'catchError'), E_ALL); set_exception_handler(array($this->getLog(), 'catchException')); date_default_timezone_set('UTC'); $this->serviceFactory = new ServiceFactory($this->container); $this->slim = $this->container->get('slim'); $this->metadata = $this->container->get('metadata'); $this->initMetadata(); } public function getSlim() { return $this->slim; } public function getMetadata() { return $this->metadata; } public function getContainer() { return $this->container; } public function getLog() { return $this->log; } public function getServiceFactory() { return $this->serviceFactory; } protected function getDoctrineConverter() { return $this->doctrineConverter; } public function run($name = 'default') { $this->routeHooks(); $this->initRoutes(); $this->getSlim()->run(); } protected function initMetadata() { $isNotCached = !$this->getMetadata()->isCached(); $this->getMetadata()->init($isNotCached); } protected function routeHooks() { $container = $this->getContainer(); $slim = $this->getSlim(); $serviceFactory = $this->getServiceFactory(); $auth = new \Espo\Core\Utils\Api\Auth($container->get('entityManager'), $container); $this->getSlim()->add($auth); $this->getSlim()->hook('slim.before.dispatch', function () use ($slim, $container) { $conditions = $slim->router()->getCurrentRoute()->getConditions(); $upperList = isset($conditions['upper']) ? $conditions['upper'] : array(); if (!in_array('controller', $upperList)) { $upperList[] = 'controller'; } $routeParams = $slim->router()->getCurrentRoute()->getParams(); if (!empty($routeParams)) { foreach ($routeParams as $name => &$param) { $isUpper = in_array($name, $upperList) ? true : false; $param = \Espo\Core\Utils\Util::toCamelCase($param, $isUpper); } $slim->router()->getCurrentRoute()->setParams($routeParams); } }); $this->getSlim()->hook('slim.before.dispatch', function () use ($slim, $container, $serviceFactory) { $route = $slim->router()->getCurrentRoute(); $conditions = $route->getConditions(); if (isset($conditions['useController']) && $conditions['useController'] == false) { return; } $routeOptions = call_user_func($route->getCallable()); $routeKeys = is_array($routeOptions) ? array_keys($routeOptions) : array(); if (!in_array('controller', $routeKeys, true)) { return $container->get('output')->render($routeOptions); } $params = $route->getParams(); $data = $slim->request()->getBody(); foreach ($routeOptions as $key => $value) { if (strstr($value, ':')) { $paramName = str_replace(':', '', $value); $value = $params[$paramName]; } $controllerParams[$key] = $value; } $controllerName = ucfirst($controllerParams['controller']); if (!empty($controllerParams['action'])) { $actionName = $controllerParams['action']; } else { $httpMethod = strtolower($slim->request()->getMethod()); $actionName = $container->get('config')->get('crud')->$httpMethod; } try { $controllerManager = new \Espo\Core\ControllerManager($container, $serviceFactory); $result = $controllerManager->process($controllerName, $actionName, $params, $data); $container->get('output')->render($result); } catch (\Exception $e) { $container->get('output')->processError($e->getMessage(), $e->getCode()); } }); $this->getSlim()->hook('slim.after.router', function () use (&$slim) { $slim->contentType('application/json'); }); } protected function initRoutes() { //$this->getSlim()->get('/', '\Espo\Utils\Api\Rest::main')->conditions( array('useController' => false) ); //TODO move routing to metadata $routes = array( array( 'method' => 'get', 'route' => 'metadata', 'params' => array( 'controller' => 'Metadata', ), ), array( 'method' => 'get', 'route' => 'settings', 'params' => array( 'controller' => 'Settings', ), ), ); $this->getSlim()->get('/', function() { return $template = "