/** * Processes the request, executing the controller action that handles this * request, determined by the [Route]. * * 1. Before the controller action is called, the [Controller::before] method * will be called. * 2. Next the controller action will be called. * 3. After the controller action is called, the [Controller::after] method * will be called. * * By default, the output from the controller is captured and returned, and * no headers are sent. * * $request->execute(); * * KoJo Modification: * extension name is prefixed to the prefix instead of directory suffixed to the prefix. * extension name format will be changed to ComAppname, ModAppname, or PlgAppname * * @return $this * @throws Kohana_Exception * @uses [Kohana::$profiling] * @uses [Profiler] */ public function execute() { // Create the class prefix $prefix = 'controller_'; if ($this->extension) { // extension format name should be com_app or mod_app or plg_app $extension = substr($this->extension, 0, 3) . substr($this->extension, 4); // Add the extension name to the class prefix. Add _admin_ depending on the client being called $this->extension_prefix = $this->client == 'admin' ? $extension . '_admin_' : $extension . '_'; // Set the extension prefix for the controller $prefix = $this->extension_prefix . $prefix; } if (Kohana::$profiling) { // Set the benchmark name $benchmark = '"' . $this->uri . '"'; if ($this !== Request::$instance and Request::$current) { // Add the parent request uri $benchmark .= ' « "' . Request::$current->uri . '"'; } // Start benchmarking $benchmark = Profiler::start('Requests', $benchmark); } // Store the currently active request $previous = Request::$current; // Change the current request to this request Request::$current = $this; try { // Load the controller using reflection $class = new ReflectionClass($prefix . $this->controller); // Add the classes path in the CFS $path = KoJo::get_path_from_class($prefix . $this->controller); // Assign the path to this Request $this->path = $path['path']; // Add the path to the CFS under existing paths of parent Requests KoJo::add_path($this->path); if ($class->isAbstract()) { throw new Kohana_Exception('Cannot create instances of abstract :controller', array(':controller' => $prefix . $this->controller)); } // Create a new instance of the controller $controller = $class->newInstance($this); // Execute the "before action" method $class->getMethod('before')->invoke($controller); // Determine the action to use $action = empty($this->action) ? Route::$default_action : $this->action; // Execute the main action with the parameters $class->getMethod('action_' . $action)->invokeArgs($controller, $this->_params); // Execute the "after action" method $class->getMethod('after')->invoke($controller); } catch (Exception $e) { // Restore the previous request Request::$current = $previous; if (isset($benchmark)) { // Delete the benchmark, it is invalid Profiler::delete($benchmark); } if ($e instanceof ReflectionException) { // Reflection will throw exceptions for missing classes or actions $this->status = 404; } else { // All other exceptions are PHP/server errors $this->status = 500; } // Re-throw the exception throw $e; } // Restore the previous request Request::$current = $previous; if (isset($benchmark)) { // Stop the benchmark Profiler::stop($benchmark); } return $this; }