/** * @param $code * @param $message * @return mixed */ public function dispatchToError($code, $message) { if (Safan::handler()->getDebugMode()) { Safan::handler()->getObjectManager()->get('flashMessenger')->set('error', $message); } Safan::handler()->getObjectManager()->get('router')->checkUri('/' . $code); return $this->loadModule(Get::str('module'), Get::str('controller'), Get::str('action')); }
/** * @var $command array */ private function dispatchCommand() { Safan::handler()->getObjectManager()->get('router')->checkCliCommand($this->command); if (Get::exists('module')) { $module = Get::str('module'); } else { return $this->getErrorMessage('Module Global Variable is not exists'); } if (Get::exists('controller')) { $controller = Get::str('controller'); } else { return $this->getErrorMessage('Controller Global Variable is not exists'); } if (Get::exists('action')) { $action = Get::str('action'); } else { return $this->getErrorMessage('Action Global Variable is not exists'); } // get all modules $modules = Safan::handler()->getModules(); if (isset($modules[$module]) && is_dir(APP_BASE_PATH . DS . $modules[$module])) { $nameSpace = '\\' . $module; $this->currentModulePath = $modulePath = APP_BASE_PATH . DS . $modules[$module]; } elseif (isset($modules[ucfirst(strtolower($module))]) && is_dir(APP_BASE_PATH . DS . $modules[ucfirst(strtolower($module))])) { // check case sensitivity $nameSpace = '\\' . ucfirst(strtolower($module)); $this->currentModulePath = $modulePath = APP_BASE_PATH . DS . $modules[ucfirst(strtolower($module))]; } else { return $this->getErrorMessage($module . ' module or path are not exist'); } // Controller Class Name $moduleController = ucfirst(strtolower($controller)) . 'Controller'; $controllerFile = $modulePath . DS . 'Commands' . DS . $moduleController . '.php'; $this->currentController = $controller; if (!file_exists($controllerFile)) { return $this->getErrorMessage($modulePath . DS . 'Commands' . DS . $moduleController . ' controller file is not exist'); } include $controllerFile; // controller class $controllerClass = $nameSpace . '\\Commands\\' . $moduleController; if (!class_exists($controllerClass)) { return $this->getErrorMessage($controllerClass . ' Controller Class is not exist'); } $moduleControllerObject = new $controllerClass(); $actionMethod = strtolower($action) . 'Action'; if (!method_exists($moduleControllerObject, $actionMethod)) { return $this->getErrorMessage($actionMethod . ' Action Method is not exist in Controller Class'); } return $moduleControllerObject->{$actionMethod}(); }
/** * @param $command * @return bool */ public function checkCliCommand($command) { $isMatch = false; foreach ($this->cliRoutes as $rule => $settings) { $matches = array(); if (preg_match($rule, $command, $matches)) { Get::setParams('module', $settings['module']); Get::setParams('controller', $settings['controller']); Get::setParams('action', $settings['action']); $route['matches'] = array(); foreach ($settings['matches'] as $key => $varName) { if (empty($varName)) { continue; } if (isset($matches[$key])) { $_GET[$varName] = $matches[$key]; } } $isMatch = true; return true; } } if (!$isMatch) { $this->checkCliCommand('/404'); } }
/** * @return string */ public function getUri() { $protocol = !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' || $_SERVER['SERVER_PORT'] == 443 ? "https://" : "http://"; $uri = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; $uriRequest = strpos($uri, Safan::handler()->baseUrl); if ($uriRequest !== false) { $uri = substr($uri, strlen(Safan::handler()->baseUrl) - $uriRequest); } //Get Variables if (strpos($uri, '?') !== false) { $uriVars = parse_str(substr(strstr($uri, '?'), 1), $outPutVars); //Generate Get variables foreach ($outPutVars as $key => $value) { if ($key != 'module' && $key != 'controller' && $key != 'action') { Get::setParams($key, $value); } } //Generate main uri $uri = strstr($uri, '?', true); } return $uri; }