/** * Attempts to dispatch the supplied Route object * * @param \THCFrame\Router\Route $route * @throws Exception\Module * @throws Exception\Controller * @throws Exception\Action */ public function dispatch(\THCFrame\Router\Route $route) { $module = trim($route->getModule()); $class = trim($route->getController()); $action = trim($route->getAction()); $parameters = $route->getMapArguments(); if ('' === $module) { throw new Exception\Module('Module Name not specified'); } elseif ('' === $class) { throw new Exception\Controller('Class Name not specified'); } elseif ('' === $action) { throw new Exception\Action('Method Name not specified'); } $status = $this->loadConfigFromDb($module . 'status'); if ($status !== null && $status != 1) { throw new Exception\Offline('Application is offline'); } $module = str_replace('\\', '', $module); preg_match('/^[a-zA-Z0-9_]+$/', $module, $matches); if (count($matches) !== 1) { throw new Exception\Module(sprintf('Disallowed characters in module name %s', $module)); } $class = str_replace('\\', '', $class); preg_match('/^[a-zA-Z0-9_]+$/', $class, $matches); if (count($matches) !== 1) { throw new Exception\Controller(sprintf('Disallowed characters in class name %s', $class)); } $file_name = strtolower("./modules/{$module}/controller/{$class}.php"); $class = ucfirst($module) . '_Controller_' . ucfirst($class); if (FALSE === file_exists($file_name)) { throw new Exception\Controller(sprintf('Class file %s not found', $file_name)); } else { require_once $file_name; } $this->_activeModule = $module; Event::fire('framework.dispatcher.controller.before', array($class, $parameters)); try { $instance = new $class(array('parameters' => $parameters)); Registry::set('controller', $instance); } catch (\Exception $e) { throw new Exception\Controller(sprintf('Controller %s error: %s', $class, $e->getMessage())); } Event::fire('framework.dispatcher.controller.after', array($class, $parameters)); if (!method_exists($instance, $action)) { $instance->willRenderLayoutView = false; $instance->willRenderActionView = false; throw new Exception\Action(sprintf('Action %s not found', $action)); } $inspector = new Inspector($instance); $methodMeta = $inspector->getMethodMeta($action); if (!empty($methodMeta['@protected']) || !empty($methodMeta['@private'])) { throw new Exception\Action(sprintf('Action %s not found', $action)); } $hooks = function ($meta, $type) use($inspector, $instance) { if (isset($meta[$type])) { $run = array(); foreach ($meta[$type] as $method) { $hookMeta = $inspector->getMethodMeta($method); if (in_array($method, $run) && !empty($hookMeta['@once'])) { continue; } $instance->{$method}(); $run[] = $method; } } }; Event::fire('framework.dispatcher.beforehooks.before', array($action, $parameters)); $hooks($methodMeta, '@before'); Event::fire('framework.dispatcher.beforehooks.after', array($action, $parameters)); Event::fire('framework.dispatcher.action.before', array($action, $parameters)); call_user_func_array(array($instance, $action), is_array($parameters) ? $parameters : array()); Event::fire('framework.dispatcher.action.after', array($action, $parameters)); Event::fire('framework.dispatcher.afterhooks.before', array($action, $parameters)); $hooks($methodMeta, '@after'); Event::fire('framework.dispatcher.afterhooks.after', array($action, $parameters)); // unset controller Registry::erase('controller'); }
/** * * @param type $key * @param type $value */ protected function _setValue($key, $value) { if (!empty($key)) { $data = Registry::get($this->defaultKey, array()); $data[$key] = $value; Registry::set($this->defaultKey, $data); } }
/** * Initialize router and dispatcher and dispatch request. * If there is some error method tries to find and render error template */ public static function run() { try { //router $router = new \THCFrame\Router\Router(array('url' => urldecode($_SERVER['REQUEST_URI']))); Registry::set('router', $router); //dispatcher $dispatcher = new \THCFrame\Router\Dispatcher(); Registry::set('dispatcher', $dispatcher->initialize()); $dispatcher->dispatch($router->getLastRoute()); unset($router); unset($dispatcher); } catch (\Exception $e) { $exception = get_class($e); // attempt to find the approapriate error template, and render foreach (self::$_exceptions as $template => $classes) { foreach ($classes as $class) { if ($class == $exception) { $defaultErrorFile = MODULES_PATH . "/app/view/errors/{$template}.phtml"; http_response_code($template); header('Content-type: text/html'); include $defaultErrorFile; exit; } } } // render fallback template http_response_code(500); header('Content-type: text/html'); echo 'An error occurred.'; if (ENV == 'dev') { print_r($e); } exit; } }
/** * Extends configuration loaded from config file for configuration loaded * form database */ public function extendForDbConfig() { $ca = Config::all(); if ($ca !== null) { foreach ($ca as $key => $value) { $this->_configArrMerged[$value->xkey] = $value->value; } $this->_parsed = ArrayMethods::toObject($this->_configArrMerged); Registry::set('configuration', $this->_parsed); } }