container = $container; $this->config = $this->container->get('config'); $this->metadata = $this->container->get('metadata'); $this->serviceFactory = $serviceFactory; } protected function getConfig() { return $this->config; } protected function getMetadata() { return $this->metadata; } public function process($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; } } if ($data) { $data = json_decode($data, true); } if (!class_exists($controllerClassName)) { throw new NotFound("Controller '$controllerName' is not found"); } $controller = new $controllerClassName($this->container, $this->serviceFactory); if ($actionName == 'index') { $actionName = $controller->defaultAction; } $actionNameUcfirst = ucfirst($actionName); $beforeMethodName = 'before' . $actionNameUcfirst; if (method_exists($controller, $beforeMethodName)) { $controller->$beforeMethodName($params, $data); } $actionMethodName = 'action' . $actionNameUcfirst; if (!method_exists($controller, $actionMethodName)) { throw new NotFound("Action '$actionMethodName' does not exist in controller '$controller'"); } $result = $controller->$actionMethodName($params, $data); $afterMethodName = 'after' . $actionNameUcfirst; if (method_exists($controller, $afterMethodName)) { $controller->$afterMethodName($params, $data); } if (is_array($result) || is_bool($result)) { return \Espo\Core\Utils\Json::encode($result); } return $result; } }