X-Git-Url: https://git.mxchange.org/?p=core.git;a=blobdiff_plain;f=inc%2Fclasses%2Fmain%2Fcontroller%2Fclass_BaseController.php;h=9e1c43e0b419c51f9a2744df22a6a50c552972ef;hp=3462effac5fae5a90d4d4f6b58dd4fe939de7f01;hb=2a80c9474b0a0923b80cd2f18e4558f63d73e2f8;hpb=0cd57c3885f00ad77fc599e53ed2f2d5e7ac267f diff --git a/inc/classes/main/controller/class_BaseController.php b/inc/classes/main/controller/class_BaseController.php index 3462effa..9e1c43e0 100644 --- a/inc/classes/main/controller/class_BaseController.php +++ b/inc/classes/main/controller/class_BaseController.php @@ -29,6 +29,9 @@ class BaseController extends BaseFrameworkSystem implements Registerable { */ private $filterChains = array(); + // Exception constants + const EXCEPTION_FILTER_CHAIN_INVALID = 0xf10; + /** * Protected constructor * @@ -57,8 +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: */ $this->debugOutput('CONTROLLER: ' . $filterChain . ' init: START'); $this->filterChains[$filterChain] = ObjectFactory::createObjectByConfiguredName('filter_chain_class'); + //* DEBUG: */ $this->debugOutput('CONTROLLER: ' . $filterChain . ' init: FINISHED'); + } + + /** + * Adds a filter to a given filter chain + * + * @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 ($filterChain, Filterable $filterInstance) { + //* DEBUG: */ $this->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: */ $this->debugOutput('CONTROLLER: ' . $filterChain . ',' . $filterInstance->__toString(). ' add: FINISH'); } /** @@ -69,7 +96,7 @@ class BaseController extends BaseFrameworkSystem implements Registerable { */ public function addPreFilter (Filterable $filterInstance) { // Add the pre filter - $this->filterChains['pre']->addFilter($filterInstance); + $this->addFilter('pre', $filterInstance); } /** @@ -80,7 +107,27 @@ class BaseController extends BaseFrameworkSystem implements Registerable { */ public function addPostFilter (Filterable $filterInstance) { // Add the post filter - $this->filterChains['post']->addFilter($filterInstance); + $this->addFilter('post', $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 ($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); } /** @@ -92,7 +139,7 @@ class BaseController extends BaseFrameworkSystem implements Registerable { */ protected function executePreFilters (Requestable $requestInstance, Responseable $responseInstance) { // Execute all pre filters - $this->filterChains['pre']->processFilters($requestInstance, $responseInstance); + $this->executeFilters('pre', $requestInstance, $responseInstance); } /** @@ -104,7 +151,7 @@ class BaseController extends BaseFrameworkSystem implements Registerable { */ protected function executePostFilters (Requestable $requestInstance, Responseable $responseInstance) { // Execute all post filters - $this->filterChains['post']->processFilters($requestInstance, $responseInstance); + $this->executeFilters('post', $requestInstance, $responseInstance); } }