X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;ds=sidebyside;f=inc%2Fclasses%2Fmain%2Fcontroller%2Fclass_BaseController.php;h=84aaca820f2055682852e51e61930465bd992e54;hb=d26e71af1e28dc1429823bdec244df6303f9b2fb;hp=9fd257c234fa4b908fb15c23e918c486759ead41;hpb=46f92fe63badaf1a2f2cddfaf9d3959f7787bbf2;p=core.git diff --git a/inc/classes/main/controller/class_BaseController.php b/inc/classes/main/controller/class_BaseController.php index 9fd257c2..84aaca82 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,6 +24,13 @@ * 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 */ @@ -39,13 +46,9 @@ 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); @@ -57,19 +60,32 @@ class BaseController extends BaseFrameworkSystem implements Registerable { * @param $filterChain Name of the filter chain * @return void */ - private function initFilterChain ($filterChain) { + 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 add addFilter ($filterGroup, Filterable $filterInstance) { - $this->filterChains[$filterGroup]->addFilter($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[$filterChain])) { + // Throw an exception here + throw new InvalidFilterChainException(array($this, $filterChain), self::EXCEPTION_FILTER_CHAIN_INVALID); + } // END - if + + // Add the filter + $this->filterChains[$filterChain]->addFilter($filterInstance); + //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONTROLLER: ' . $filterChain . ',' . $filterInstance->__toString(). ' add: FINISH'); } /** @@ -80,7 +96,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); } /** @@ -91,19 +107,27 @@ 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 + * Executes given filter chain chain * - * @param $filterGroup Group of the filter to execute + * @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) { - $this->filterChains[$filterGroup]->processFilters($requestInstance, $responseInstance); + protected function executeFilters ($filterChain, Requestable $requestInstance, Responseable $responseInstance) { + // Test if the filter is there + if (!isset($this->filterChains[$filterChain])) { + // Throw an exception here + throw new InvalidFilterChainException(array($this, $filterChain), self::EXCEPTION_FILTER_CHAIN_INVALID); + } // END - if + + // Run all filters + $this->filterChains[$filterChain]->processFilters($requestInstance, $responseInstance); } /** @@ -115,7 +139,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); } /** @@ -127,7 +151,7 @@ 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); } }