Ejemplo n.º 1
0
 /**
  * Will execute the called request, also checks if the request
  * parameters are correct (like: Method and Middleware)
  *
  * @uses Stativo\Core\Middleware
  * @uses Stativo\Core\Route
  * @uses Stativo\Helpers\HttpException
  * @uses Stativo\Helpers\MiddlewareException
  * @return this
  */
 public function execute()
 {
     self::$_server = $_SERVER;
     self::$_get = $_GET;
     self::$_post = $_POST;
     self::$_put = $_POST;
     self::$_delete = $_POST;
     // Check if HTTP methods are set
     if (isset(self::$_post['__method'])) {
         // if __method isset, then use them
         if (strtolower(self::$_post['__method']) == strtolower(Route::PUT) or strtolower(self::$_post['__method']) == strtolower(Route::DELETE)) {
             self::$_method = self::$_post['__method'];
         } else {
             // Use default method
             self::$_method = $_SERVER['REQUEST_METHOD'];
         }
     } else {
         // Use default method
         self::$_method = $_SERVER['REQUEST_METHOD'];
     }
     unset(self::$_post['__method']);
     unset(self::$_put['__method']);
     unset(self::$_delete['__method']);
     // Match URI's in Stativo\Core\Route
     $match = $this->process_uri(Request::instance());
     // Check matches
     if ($match === null) {
         // Throw Stativo\Helpers\HttpException (404)
         throw new HttpException(404, 'URI /:uri is not found!', array(':uri' => $this->uri()));
     } else {
         // Match successful!
         $controller = '';
         $action = 'index';
         if (isset($match['params']['directory'])) {
             $controller .= str_replace('/', '\\', $match['params']['directory'] . '/');
             unset($match['params']['directory']);
         }
         if (isset($match['params']['controller'])) {
             $controller .= $match['params']['controller'];
             unset($match['params']['controller']);
         }
         if (isset($match['params']['action'])) {
             $action = $match['params']['action'];
             unset($match['params']['action']);
         }
         // Get current route
         $route = Route::current();
         // Check methods, if they compare then launch class and action
         if ($route->_method === Route::ANY or in_array(self::$_method, !is_array($route->_method) ? array($route->_method) : $route->_method)) {
             if ($middleware = Middleware::boot($route)) {
                 self::$_params = $match['params'];
                 // Find required controller
                 $controller = File::find($controller, 'Controller');
                 // Call classes
                 $class = new $controller();
                 $class->{$action}();
                 \Stativo\Helpers\Power::runOff();
             } else {
                 throw new MiddlewareException("':middleware' encountered an error!", [':middleware' => Middleware::$name]);
             }
         } else {
             // Throw Stativo\Helpers\HttpException, method mismatch
             throw new HttpException(500, 'Route method mismatch, Expected: :method request', [':method' => is_array($route->_method) ? implode(', ', $route->_method) : $route->_method]);
         }
     }
     return Response::instance();
 }