/** * trigger the handler * * @return void */ public function handle() { $this->log(); // when not in development we respond using a route if (!ClanCats::in_development() && !ClanCats::is_cli()) { CCResponse::error(500)->send(true); } else { $this->respond(); } }
/** * controller execution * * @param string $action * @param array $params * @return CCResponse */ public function execute($action = null, $params = array()) { $this->set_language_alias($action); // reset the response $this->response = null; $this->output = null; // fix notice $return = null; if (is_null($action)) { $action = static::$_default_action; } // capture all output made in the controller ob_start(); // run wake event if (method_exists($this, 'wake')) { $return = call_user_func_array(array($this, 'wake'), $params); } // do we already have an resposne from the wake event? if (!$return instanceof CCResponse) { // check if we got a custom action handler if (!is_null(static::$_action_handler)) { $return = call_user_func(array($this, static::$_action_handler), $action, $params); } else { // check if the action exists if (method_exists($this, static::$_action_prefix . $action)) { $return = call_user_func_array(array($this, static::$_action_prefix . $action), $params); } else { $return = CCResponse::error(404); } } } // get the output $this->output = ob_get_clean(); // do we got a response now? if ($return instanceof CCResponse) { $this->response = $return; } elseif (!empty($return) && is_string($return)) { $this->response = $this->_respond($return); } // do we got a valid response if not use controller output if (!$this->response instanceof CCResponse) { $this->response = $this->_respond($this->output); } // run sleep event if (method_exists($this, 'sleep')) { call_user_func_array(array($this, 'sleep'), $params); } // return thaaaaat return $this->response; }
/** * Execute the Request * * @param array $action * @param array $params * * @return self */ public function perform() { // set the input if (!is_null($this->input)) { CCIn::instance($this->input); } else { CCIn::instance(CCServer::instance()); } // set current request static::$_current =& $this; // route is invalid show 404 if (!$this->route instanceof CCRoute) { $this->route = CCRouter::resolve('#404'); } /* * call wake events * if one event returns an response all other calls will be skipped also events! */ foreach (CCRouter::events_matching('wake', $this->route->uri) as $callback) { if (($return = CCContainer::call($callback)) instanceof CCResponse) { $this->response = $return; return $this; } } /* * a closure */ if (!is_array($this->route->callback) && is_callable($this->route->callback)) { // execute and capture the output ob_start(); // run the closure $return = call_user_func_array($this->route->callback, $this->route->params); // catch the output $output = ob_get_clean(); // do we got a response? if (!$return instanceof CCResponse) { // if not create one with the captured output $return = CCResponse::create($output); } } elseif (is_callable($this->route->callback)) { // execute the callback and get the return $return = call_user_func_array($this->route->callback, array($this->route->action, $this->route->params)); // do we got a response? if (!$return instanceof CCResponse) { // if not create one with the return as string $return = CCResponse::create((string) $return); } } else { $return = CCResponse::error(404); } // set the response $this->response = $return; /* * call sleep events * if one event returns an response all other calls will be skipped also events! */ foreach (CCRouter::events_matching('sleep', $this->route->uri) as $callback) { if ($return = CCContainer::call($callback, $this->response) instanceof CCResponse) { $this->response = $return; return $this; } } return $this; }