X-Git-Url: https://git.mxchange.org/?p=core.git;a=blobdiff_plain;f=inc%2Fclasses%2Fmain%2Fcontroller%2Fclass_BaseController.php;h=d195c9e76bd21972997495f7a2b5f7a39562dc23;hp=95827c5e89eeb3ce87638972f9994ff6433e8471;hb=b75d59b0b03c28c0c142df99bf70692f9d2b9086;hpb=7274fc15fc62c08232a7fad492f445306d17cb3c diff --git a/inc/classes/main/controller/class_BaseController.php b/inc/classes/main/controller/class_BaseController.php index 95827c5e..d195c9e7 100644 --- a/inc/classes/main/controller/class_BaseController.php +++ b/inc/classes/main/controller/class_BaseController.php @@ -4,11 +4,11 @@ * write your own controller. You get the advantage that you can use the pre and * post filters. * - * @author Roland Haeder + * @author Roland Haeder * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Core Developer Team + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Core Developer Team * @license GNU GPL 3.0 or any newer version - * @link http://www.ship-simu.org + * @link http://www.shipsimu.org * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -24,14 +24,18 @@ * along with this program. If not, see . */ class BaseController extends BaseFrameworkSystem implements Registerable { + // Exception constants + const EXCEPTION_FILTER_CHAIN_INVALID = 0xf10; + + // Names of controller's own filter chains + const FILTER_CHAIN_PRE_COMMAND = 'controller_pre_command'; + const FILTER_CHAIN_POST_COMMAND = 'controller_post_command'; + /** * Generic filter chains */ private $filterChains = array(); - // Exception constants - const EXCEPTION_FILTER_CHAIN_INVALID = 0xf10; - /** * Protected constructor * @@ -42,18 +46,90 @@ class BaseController extends BaseFrameworkSystem implements Registerable { // Call parent constructor parent::__construct($className); - // Clean up a little - $this->removeNumberFormaters(); - $this->removeSystemArray(); - // Initialize both filter chains - $this->initFilterChain('pre'); - $this->initFilterChain('post'); + $this->initFilterChain(self::FILTER_CHAIN_PRE_COMMAND); + $this->initFilterChain(self::FILTER_CHAIN_POST_COMMAND); // Add this controller to the registry Registry::getRegistry()->addInstance('controller', $this); } + /** + * Executes a command with pre and post filters + * + * @param $requestInstance A Requestable class + * @param $responseInstance A Responseable class + * @return void + */ + public function executeGenericPrePostCommand (Requestable $requestInstance, Responseable $responseInstance) { + // Get the command instance from the resolver by sending a request instance to the resolver + $commandInstance = $this->getResolverInstance()->resolveCommandByRequest($requestInstance); + + // Add more filters by the command + $commandInstance->addExtraFilters($this, $requestInstance); + + // Run the pre filters + $this->executePreFilters($requestInstance, $responseInstance); + + // This request was valid! :-D + $requestInstance->requestIsValid(); + + // Execute the command + $commandInstance->execute($requestInstance, $responseInstance); + + // Run the post filters + $this->executePostFilters($requestInstance, $responseInstance); + + // Flush the response out + $responseInstance->flushBuffer(); + } + + /** + * Handles the given request and response, redirects to login_failed if + * UserAuthorizationException is thrown. + * + * @param $requestInstance An instance of a request class + * @param $responseInstance An instance of a response class + * @return void + */ + public function genericHanleRequestLoginFailedRedirect (Requestable $requestInstance, Responseable $responseInstance) { + // Get the "form action" + $formAction = $requestInstance->getRequestElement('form'); + + // Get command instance from resolver + $commandInstance = $this->getResolverInstance()->resolveCommand($formAction); + + // Add more filters by the command + $commandInstance->addExtraFilters($this, $requestInstance); + + // Try to run the pre filters, if auth exceptions come through redirect here + try { + // Run the pre filters + $this->executePreFilters($requestInstance, $responseInstance); + } catch (UserAuthorizationException $e) { + // Redirect to main page + $responseInstance->redirectToConfiguredUrl('login_failed'); + + // Exit here + exit(); + } + + /* + * Is the request still valid? Post filters shall only be executed of + * the request is valid + */ + if ($requestInstance->isRequestValid()) { + // Execute the command + $commandInstance->execute($requestInstance, $responseInstance); + + // Execute *very* generic ppost filters + $this->executePostFilters($requestInstance, $responseInstance); + } // END - if + + // Flush the buffer out + $responseInstance->flushBuffer(); + } + /** * Private method to initialize a given filter chain * @@ -61,26 +137,31 @@ class BaseController extends BaseFrameworkSystem implements Registerable { * @return void */ protected function initFilterChain ($filterChain) { + //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONTROLLER: ' . $filterChain . ' init: START'); $this->filterChains[$filterChain] = ObjectFactory::createObjectByConfiguredName('filter_chain_class'); + //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONTROLLER: ' . $filterChain . ' init: FINISHED'); } /** - * Adds a filter to a given filter group + * Adds a filter to a given filter chain * - * @param $filterGroup Group of the filter + * @param $filterChain Chain of the filter * @param $filterInstance An instance of a filter * @return void * @throws InvalidFilterChainException If the filter chain is invalid */ - protected function addFilter ($filterGroup, Filterable $filterInstance) { + protected function addFilter ($filterChain, Filterable $filterInstance) { + //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONTROLLER: ' . $filterChain . ',' . $filterInstance->__toString(). ' add: START'); + // Test if the filter is there - if (!isset($this->filterChains[$filterGroup])) { + if (!isset($this->filterChains[$filterChain])) { // Throw an exception here - throw new InvalidFilterChainException(array($this, $filterGroup), self::EXCEPTION_FILTER_CHAIN_INVALID); + throw new InvalidFilterChainException(array($this, $filterChain), self::EXCEPTION_FILTER_CHAIN_INVALID); } // END - if // Add the filter - $this->filterChains[$filterGroup]->addFilter($filterInstance); + $this->filterChains[$filterChain]->addFilter($filterInstance); + //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONTROLLER: ' . $filterChain . ',' . $filterInstance->__toString(). ' add: FINISH'); } /** @@ -91,7 +172,7 @@ class BaseController extends BaseFrameworkSystem implements Registerable { */ public function addPreFilter (Filterable $filterInstance) { // Add the pre filter - $this->addFilter('pre', $filterInstance); + $this->addFilter(self::FILTER_CHAIN_PRE_COMMAND, $filterInstance); } /** @@ -102,27 +183,37 @@ class BaseController extends BaseFrameworkSystem implements Registerable { */ public function addPostFilter (Filterable $filterInstance) { // Add the post filter - $this->addFilter('post', $filterInstance); + $this->addFilter(self::FILTER_CHAIN_POST_COMMAND, $filterInstance); } /** - * Executes given filter chain group + * Add a shutdown filter * - * @param $filterGroup Group of the filter to execute + * @param $filterInstance A Filterable class + * @return void + */ + public function addShutdownFilter (Filterable $filterInstance) { + $this->addFilter('shutdown', $filterInstance); + } + + /** + * Executes given filter chain chain + * + * @param $filterChain Chain of the filter to execute * @param $requestInstance An instance of a request class * @param $responseInstance An instance of a response class * @return void * @throws InvalidFilterChainException If the filter chain is invalid */ - protected function executeFilters ($filterGroup, Requestable $requestInstance, Responseable $responseInstance) { + protected function executeFilters ($filterChain, Requestable $requestInstance, Responseable $responseInstance) { // Test if the filter is there - if (!isset($this->filterChains[$filterGroup])) { + if (!isset($this->filterChains[$filterChain])) { // Throw an exception here - throw new InvalidFilterChainException(array($this, $filterGroup), self::EXCEPTION_FILTER_CHAIN_INVALID); + throw new InvalidFilterChainException(array($this, $filterChain), self::EXCEPTION_FILTER_CHAIN_INVALID); } // END - if // Run all filters - $this->filterChains[$filterGroup]->processFilters($requestInstance, $responseInstance); + $this->filterChains[$filterChain]->processFilters($requestInstance, $responseInstance); } /** @@ -134,7 +225,7 @@ class BaseController extends BaseFrameworkSystem implements Registerable { */ protected function executePreFilters (Requestable $requestInstance, Responseable $responseInstance) { // Execute all pre filters - $this->executeFilters('pre', $requestInstance, $responseInstance); + $this->executeFilters(self::FILTER_CHAIN_PRE_COMMAND, $requestInstance, $responseInstance); } /** @@ -146,7 +237,18 @@ class BaseController extends BaseFrameworkSystem implements Registerable { */ protected function executePostFilters (Requestable $requestInstance, Responseable $responseInstance) { // Execute all post filters - $this->executeFilters('post', $requestInstance, $responseInstance); + $this->executeFilters(self::FILTER_CHAIN_POST_COMMAND, $requestInstance, $responseInstance); + } + + /** + * Executes all shutdown filters + * + * @param $requestInstance A Requestable class + * @param $responseInstance A Responseable class + * @return void + */ + public function executeShutdownFilters (Requestable $requestInstance, Responseable $responseInstance) { + $this->executeFilters('shutdown', $requestInstance, $responseInstance); } }