/** * Generates a new request. The request is then set to be the active * request. If this is the first request, then save that as the main * request for the app. * * Usage: * * <code>Request::factory('hello/world');</code> * * @access public * @param string The URI of the request * @param bool if true use routes to process the URI * @return object The new request */ public static function factory($uri = null, $route = true) { logger(Fuel::L_INFO, 'Creating a new Request with URI = "' . $uri . '"', __METHOD__); static::$active = new static($uri, $route); if (!static::$main) { logger(Fuel::L_INFO, 'Setting main Request', __METHOD__); static::$main = static::$active; } return static::$active; }
/** * Generates a new request. The request is then set to be the active * request. If this is the first request, then save that as the main * request for the app. * * Usage: * * Request::factory('hello/world'); * * @param string The URI of the request * @param bool Whether to use the routes to determine the Controller and Action * @return Request The new request object */ public static function factory($uri = null, $route = true) { logger(Fuel::L_INFO, 'Creating a new Request with URI = "' . $uri . '"', __METHOD__); $request = new static($uri, $route); $request->parent = static::$active; static::$active->children[] = $request; static::$active = $request; if (!static::$main) { logger(Fuel::L_INFO, 'Setting main Request', __METHOD__); static::$main = $request; } return $request; }
/** * Factory method for instantiating the unit object, and executing its * main algorithm. * * @return UnitInterface */ public static function factory(OpenStack $connection, array $includedUnits) { $unit = new static(); $unit->setConnection($connection)->setIncludedUnits($includedUnits); if (!($service = $unit->setupService())) { return false; } $unit->setService($service); // Run execution... $unit->main(); // Clean stuff up if necessary... $unit->teardown(); return $unit; }
/** * Generates a new `curl` request without going through HTTP connection, * this allow user session can be shared between both request `client` and `server`. * * The request is then set to be the active request. * * Usage: * * <code>\Hybrid\Request::connect('GET controller/method?hello=world');</code> * * @access public * @param string $uri - The URI of the request * @param array $dataset - Set a dataset for GET, POST, PUT or DELETE * @return object The new request */ public static function connect($uri, $dataset = array()) { $uri_segments = explode(' ', $uri); $type = 'GET'; if (in_array(strtoupper($uri_segments[0]), array('DELETE', 'POST', 'PUT', 'GET'))) { $uri = $uri_segments[1]; $type = $uri_segments[0]; } $query_dataset = array(); $query_string = parse_url($uri); if (isset($query_string['query'])) { $uri = $query_string['path']; parse_str($query_string['query'], $query_dataset); } $dataset = array_merge($query_dataset, $dataset); logger(\Fuel::L_INFO, 'Creating a new Request with URI = "' . $uri . '"', __METHOD__); static::$active = new static($uri, true, $dataset, $type); if (!static::$main) { logger(\Fuel::L_INFO, 'Setting main Request', __METHOD__); static::$main = static::$active; } return static::$active; }
/** * This executes the request and sets the output to be used later. * * Usage: * * $request = Request::forge('hello/world')->execute(); * * @param array|null $method_params An array of parameters to pass to the method being executed * @return Request This request object */ public function execute($method_params = null) { if (\Fuel::$profiling) { \Profiler::mark(__METHOD__ . ' Start'); } logger(\Fuel::L_INFO, 'Called', __METHOD__); // Make the current request active static::$active = $this; // First request called is also the main request if (!static::$main) { logger(\Fuel::L_INFO, 'Setting main Request', __METHOD__); static::$main = $this; } if (!$this->route) { static::reset_request(); throw new \HttpNotFoundException(); } try { if ($this->route->callable !== null) { $response = call_user_func_array($this->route->callable, array($this)); } else { $method_prefix = 'action_'; $class = $this->controller; // Allow override of method params from execute if (is_array($method_params)) { $this->method_params = array_merge($this->method_params, $method_params); } // If the class doesn't exist then 404 if (!class_exists($class)) { throw new \HttpNotFoundException(); } // Load the controller using reflection $class = new \ReflectionClass($class); if ($class->isAbstract()) { throw new \HttpNotFoundException(); } // Create a new instance of the controller $this->controller_instance = $class->newInstance($this, new \Response()); $this->action = $this->action ?: ($class->hasProperty('default_action') ? $class->getProperty('default_action')->getValue($this->controller_instance) : 'index'); $method = $method_prefix . $this->action; // Allow to do in controller routing if method router(action, params) exists if ($class->hasMethod('router')) { $method = 'router'; $this->method_params = array($this->action, $this->method_params); } if ($class->hasMethod($method)) { $action = $class->getMethod($method); if (!$action->isPublic()) { throw new \HttpNotFoundException(); } $class->hasMethod('before') and $class->getMethod('before')->invoke($this->controller_instance); $response = $action->invokeArgs($this->controller_instance, $this->method_params); $class->hasMethod('after') and $response = $class->getMethod('after')->invoke($this->controller_instance, $response); } else { throw new \HttpNotFoundException(); } } } catch (\Exception $e) { static::reset_request(); throw $e; } // Get the controller's output if (is_null($response)) { throw new \FuelException('The controller action called or it\'s after() method must return a Response object.'); } elseif ($response instanceof \Response) { $this->response = $response; } else { $this->response = \Response::forge($response, 200); } static::reset_request(); if (\Fuel::$profiling) { \Profiler::mark(__METHOD__ . ' End'); } return $this; }
/** * This executes the request and sets the output to be used later. * * Usage: * * $request = Request::forge('hello/world')->execute(); * * @param array|null $method_params An array of parameters to pass to the method being executed * @return Request This request object */ public function execute($method_params = null) { if (\Fuel::$profiling) { \Profiler::mark(__METHOD__ . ' Start'); } logger(\Fuel::L_INFO, 'Called', __METHOD__); // Make the current request active static::$active = $this; // First request called is also the main request if (!static::$main) { logger(\Fuel::L_INFO, 'Setting main Request', __METHOD__); static::$main = $this; } if (!$this->route) { static::reset_request(); throw new \HttpNotFoundException(); } try { if ($this->route->callable !== null) { $response = call_user_func_array($this->route->callable, array($this)); } else { $method_prefix = 'action_'; $class = $this->controller; // Allow override of method params from execute if (is_array($method_params)) { $this->method_params = array_merge($this->method_params, $method_params); } // If the class doesn't exist then 404 if (!class_exists($class)) { throw new \HttpNotFoundException(); } // Load the controller using reflection $class = new \ReflectionClass($class); if ($class->isAbstract()) { throw new \HttpNotFoundException(); } // Create a new instance of the controller $this->controller_instance = $class->newInstance($this, new \Response()); $this->action = $this->action ?: ($class->hasProperty('default_action') ? $class->getProperty('default_action')->getValue($this->controller_instance) : 'index'); $method = $method_prefix . $this->action; // Allow to do in controller routing if method router(action, params) exists if ($class->hasMethod('router')) { $method = 'router'; $this->method_params = array($this->action, $this->method_params); } if ($class->hasMethod($method)) { $action = $class->getMethod($method); if (!$action->isPublic()) { throw new \HttpNotFoundException(); } $class->getMethod('before')->invoke($this->controller_instance); $response = $action->invokeArgs($this->controller_instance, $this->method_params); $response_after = $class->getMethod('after')->invoke($this->controller_instance, $response); // @TODO let the after method set the response directly if (is_null($response_after)) { logger(\Fuel::L_WARNING, 'The ' . $class->getName() . '::after() method should accept and return the Controller\'s response, empty return for the after() method is deprecated.', __METHOD__); } else { $response = $response_after; } } else { throw new \HttpNotFoundException(); } } } catch (\Exception $e) { static::reset_request(); throw $e; } // Get the controller's output if (is_null($response)) { // @TODO remove this in a future version as we will get rid of it. logger(\Fuel::L_WARNING, 'The ' . $class->getName() . ' controller should return a string or a Response object, support for the $controller->response object is deprecated.', __METHOD__); $this->response = $this->controller_instance->response; } elseif ($response instanceof \Response) { $this->response = $response; } else { $this->response = \Response::forge($response, 200); } static::reset_request(); if (\Fuel::$profiling) { \Profiler::mark(__METHOD__ . ' End'); } return $this; }
/** * This executes the request and sets the output to be used later. * * Usage: * * $request = Request::forge('hello/world')->execute(); * * @param array|null $method_params An array of parameters to pass to the method being executed * @return Request This request object */ public function execute($method_params = null) { // fire any request started events \Event::instance()->has_events('request_started') and \Event::instance()->trigger('request_started', '', 'none'); if (\Fuel::$profiling) { \Profiler::mark(__METHOD__ . ': Start of ' . $this->uri->get()); } logger(\Fuel::L_INFO, 'Called', __METHOD__); // Make the current request active static::$active = $this; // First request called is also the main request if (!static::$main) { logger(\Fuel::L_INFO, 'Setting main Request', __METHOD__); static::$main = $this; } if (!$this->route) { static::reset_request(); throw new \HttpNotFoundException(); } // save the current language so we can restore it after the call $current_language = \Config::get('language', 'en'); try { if ($this->route->callable !== null) { $response = call_fuel_func_array($this->route->callable, array($this)); if (!$response instanceof Response) { $response = new \Response($response); } } else { $method_prefix = $this->method . '_'; $class = $this->controller; // Allow override of method params from execute if (is_array($method_params)) { $this->method_params = array_merge($this->method_params, $method_params); } // If the class doesn't exist then 404 if (!class_exists($class)) { throw new \HttpNotFoundException(); } // Load the controller using reflection $class = new \ReflectionClass($class); if ($class->isAbstract()) { throw new \HttpNotFoundException(); } // Create a new instance of the controller $this->controller_instance = $class->newInstance($this); $this->action = $this->action ?: ($class->hasProperty('default_action') ? $class->getProperty('default_action')->getValue($this->controller_instance) : 'index'); $method = $method_prefix . $this->action; // Allow to do in controller routing if method router(action, params) exists if ($class->hasMethod('router')) { $method = 'router'; $this->method_params = array($this->action, $this->method_params); } if (!$class->hasMethod($method)) { // If they call user, go to $this->post_user(); $method = strtolower(\Input::method()) . '_' . $this->action; // Fall back to action_ if no HTTP request method based method exists if (!$class->hasMethod($method)) { $method = 'action_' . $this->action; } } if ($class->hasMethod($method)) { $action = $class->getMethod($method); if (!$action->isPublic()) { throw new \HttpNotFoundException(); } if (count($this->method_params) < $action->getNumberOfRequiredParameters()) { throw new \HttpNotFoundException(); } // fire any controller started events \Event::instance()->has_events('controller_started') and \Event::instance()->trigger('controller_started', '', 'none'); $class->hasMethod('before') and $class->getMethod('before')->invoke($this->controller_instance); $response = $action->invokeArgs($this->controller_instance, $this->method_params); $class->hasMethod('after') and $response = $class->getMethod('after')->invoke($this->controller_instance, $response); // fire any controller finished events \Event::instance()->has_events('controller_finished') and \Event::instance()->trigger('controller_finished', '', 'none'); } else { throw new \HttpNotFoundException(); } } // restore the language setting \Config::set('language', $current_language); } catch (\Exception $e) { static::reset_request(); // restore the language setting \Config::set('language', $current_language); throw $e; } // Get the controller's output if ($response instanceof Response) { $this->response = $response; } else { throw new \FuelException(get_class($this->controller_instance) . '::' . $method . '() or the controller after() method must return a Response object.'); } // fire any request finished events \Event::instance()->has_events('request_finished') and \Event::instance()->trigger('request_finished', '', 'none'); if (\Fuel::$profiling) { \Profiler::mark(__METHOD__ . ': End of ' . $this->uri->get()); } static::reset_request(); return $this; }
/** * Sets the main template for the current request. * * @param string $template */ public static function setMainTemplate($template) { static::$main = $template; $basename = basename($template, '.php'); static::$basename = $basename === 'index' ? false : $basename; }
/** * This executes the request and sets the output to be used later. * * Usage: * * $request = Request::forge('hello/world')->execute(); * * @param array|null $method_params An array of parameters to pass to the method being executed * @return Request This request object */ public function execute($method_params = null) { if (\Fuel::$profiling) { \Profiler::mark(__METHOD__ . ' Start'); } logger(\Fuel::L_INFO, 'Called', __METHOD__); // Make the current request active static::$active = $this; // First request called is also the main request if (!static::$main) { logger(\Fuel::L_INFO, 'Setting main Request', __METHOD__); static::$main = $this; } if (!$this->route) { static::reset_request(); throw new \HttpNotFoundException(); } if ($this->route->callable !== null) { logger(\Fuel::L_INFO, 'Calling closure', __METHOD__); try { $response = call_user_func_array($this->route->callable, array($this)); } catch (HttpNotFoundException $e) { static::reset_request(); throw $e; } } else { $method_prefix = 'action_'; $class = $this->controller; // If the class doesn't exist then 404 if (!class_exists($class)) { static::reset_request(); throw new \HttpNotFoundException(); } logger(\Fuel::L_INFO, 'Loading controller ' . $class, __METHOD__); $this->controller_instance = $controller = new $class($this, new \Response()); $this->action = $this->action ?: (property_exists($controller, 'default_action') ? $controller->default_action : 'index'); $method = $method_prefix . $this->action; // Allow override of method params from execute if (is_array($method_params)) { $this->method_params = array_merge($this->method_params, $method_params); } // Allow to do in controller routing if method router(action, params) exists if (method_exists($controller, 'router')) { $method = 'router'; $this->method_params = array($this->action, $this->method_params); } if (is_callable(array($controller, $method))) { // Call the before method if it exists if (method_exists($controller, 'before')) { logger(\Fuel::L_INFO, 'Calling ' . $class . '::before', __METHOD__); $controller->before(); } logger(\Fuel::L_INFO, 'Calling ' . $class . '::' . $method, __METHOD__); try { $response = call_user_func_array(array($controller, $method), $this->method_params); } catch (HttpNotFoundException $e) { static::reset_request(); throw $e; } // Call the after method if it exists if (method_exists($controller, 'after')) { logger(\Fuel::L_INFO, 'Calling ' . $class . '::after', __METHOD__); $response_after = $controller->after($response); // @TODO let the after method set the response directly if (is_null($response_after)) { logger(\Fuel::L_WARNING, 'The ' . get_class($controller) . '::after() method should accept and return the Controller\'s response, empty return for the after() method is deprecated.', __METHOD__); } else { $response = $response_after; } } } else { static::reset_request(); throw new \HttpNotFoundException(); } } // Get the controller's output if (is_null($response)) { // @TODO remove this in a future version as we will get rid of it. logger(\Fuel::L_WARNING, 'The ' . get_class($controller) . ' controller should return a string or a Response object, support for the $controller->response object is deprecated.', __METHOD__); $this->response = $controller->response; } elseif ($response instanceof \Response) { $this->response = $response; } else { $this->response = \Response::forge($response, 200); } static::reset_request(); if (\Fuel::$profiling) { \Profiler::mark(__METHOD__ . ' End'); } return $this; }