/** * Reset View to its defaults * * User defined Template Variables are removed leaving only the * pre-assigned variables. * * If, for some reason, it's already empty, simply create them. * * @return Next\View\View * View Object (Fluent Interface) */ public function resetDefaults() { $this->_tplVars['request'] = $this->_application->getRequest(); // Unregistering any possible Exception Message unset($this->_tplVars['__EXCEPTION__']); return $this; }
/** * Retrieve a GET Param * * Grant access to a Request Dynamic Params using Property Notation instead Array Notation * * @param string $param * Desired Param from Dynamic Params * * @return mixed Dynamic Param Value * * @throws Next\Controller\ControllerException * Trying to access internal properties prefixed with an underscore * without use their correct accessors * * @throws Next\Controller\ControllerException * Trying to access non-existent param */ public function __get($param) { $param = trim($param); try { return $this->_application->getRequest()->getQuery($param); } catch (RequestException $e) { // Standardizing Exception throw ControllerException::paramNotFound($e); } }
/** * Dispatches the Controller * * @param Next\Application\Application $application * Application to Configure * * @param stdClass $data * Data to Configure Application * * @return Next\HTTP\Response * Response Object * * @throws Next\Controller\Dispatcher\DispatcherException * ReflectionException was caught */ public function dispatch(Application $application, \stdClass $data) { try { $this->setDispatched(TRUE); // Adding Request Params $application->getRequest()->setQuery($data->params); // Calling Action from Controller at defined Application $reflector = new \ReflectionMethod($data->class, $data->method); $reflector->invoke(new $data->class($application)); return $application->getResponse(); } catch (\ReflectionException $e) { throw DispatcherException::reflection($e); } catch (ControllerException $e) { /** * @internal * ControllerException's came from Application's Controllers * and, as part of Standardization Concept, should be thrown when * something is wrong * * E.g.: Database Query results in FALSE instead of a Recordset Object * * Doesn't matter the level of DEVELOPMENT MODE Constant, we'll * try to create a Template Variable and virtually re-send the Response, * by re-rendering the View * * Now, in Template View, a special variable will be available with * Exception Message * * If the assignment or rendering fails, the Production Handler * will be used as fallback */ try { $application->getView()->assign('__EXCEPTION__', $e->getMessage())->render(); } catch (ViewException $e) { Handlers::production($e); } } catch (ViewException $e) { /** * @internal * Catching ViewException grants a nice view for any sort of * errors triggered by Next View Class, specially when they come from Magic Methods * which is directly related to Template Variables usage. * * And by forcing a Development Handler we warn lazy * programmers they are doing the wrong thing, like trying to hide the error ^^ */ if (ob_get_length()) { // We want ONLY the Exception Template ob_end_clean(); } Handlers::development($e); } }
/** * Finds a Route that matches to an Application AND current Request * * @param Next\Application\Application $application * Application to iterate Controllers * * @return array|object|boolean * * If a Route could be match against current Request URI an * array or an object will be returned (depending on Connection * Driver configuration). * * If none could, FALSE will be returned */ public function find(Application $application) { // Shortening Declarations $request = $application->getRequest(); $URI = $request->getRequestUri(); // Searching the Request in Routes Database $stmt = $this->dbh->prepare('SELECT `requestMethod`, `class`, `method`, `requiredParams`, `optionalParams` FROM `routes` WHERE `application` = ? AND ( `requestMethod` = ? AND ? REGEXP `URI` )'); $stmt->execute(array($application->getClass()->getName(), $request->getRequestMethod(), $URI)); $data = $stmt->fetch(); // Match found, let's prepare everything for a successful Dispatch if ($data !== FALSE) { /** * @internal * Setting Up Found Controller and its action to be used in View, * as part of findFilebySpec() method */ $this->controller =& $data->class; $this->action =& $data->method; // Analyzing Params $requiredParams = unserialize($data->requiredParams); // Lookup for Required Params in URL if (count($requiredParams) > 0) { $this->lookup($requiredParams, $URI, $request->getQuery()); } /** * @internal * Validating Required Params * Only Parameters with a [List|of|Possible|Values] will be validated */ $token = self::LIST_OPEN_TOKEN; $this->validate(array_filter($requiredParams, function ($item) use($token) { return strpos($item, $token) !== FALSE; }), $URI); // Process Dynamic Params, in order to register them as Request Params $params = $this->process(array_merge($requiredParams, unserialize($data->optionalParams)), $URI); // Merging manually defined GET query $data->params = array_merge($params, $request->getQuery()); // Discarding Unnecessary Information unset($data->requiredParams, $data->optionalParams); return $data; } return FALSE; }