3 namespace CoreFramework\Filter\Chain;
5 // Import framework stuff
6 use CoreFramework\Object\BaseFrameworkSystem;
9 * A filter chain for pre and post filters
11 * @author Roland Haeder <webmaster@shipsimu.org>
13 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
14 * @license GNU GPL 3.0 or any newer version
15 * @link http://www.shipsimu.org
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 class FilterChain extends BaseFrameworkSystem implements Registerable {
32 * Protected constructor
36 protected function __construct () {
37 // Call parent constructor
38 parent::__construct(__CLASS__);
42 * Creates an instance of this class
44 * @return $chainInstance An instance of this class
46 public static final function createFilterChain () {
48 $chainInstance = new FilterChain();
50 // Return the prepared instance
51 return $chainInstance;
57 * @param $filerInstance An instance of a filter class
60 public final function addFilter (Filterable $filterInstance) {
61 $this->pushValueToGenericArrayKey('filters', 'generic', 'dummy', $filterInstance);
65 * Add a new post-filter
67 * @param $filerInstance An instance of a post-filter class
70 public final function addPostFilter (Filterable $filterInstance) {
71 $this->pushValueToGenericArrayKey('filters', 'post', 'dummy', $filterInstance);
75 * "Getter" for filters array
77 * @return $filters The filters array holding all filter instances
79 protected function getFilters () {
80 // Default is nothing found
83 // Are some filters set?
84 if ($this->isValidGenericArrayKey('filters', 'generic', 'dummy')) {
86 $filters = $this->getGenericArrayKey('filters', 'generic', 'dummy');
94 * "Getter" for post-filters array
96 * @return $filters The filters array holding all post-filter instances
98 protected function getPostFilters () {
99 // Default is nothing found
102 // Are some filters set?
103 if ($this->isValidGenericArrayKey('filters', 'post', 'dummy')) {
105 $filters = $this->getGenericArrayKey('filters', 'post', 'dummy');
113 * Process all added filters. Please note that filters must throw
114 * FilterChainException if they need to interrupt the filter chain.
116 * @param $requestInstance An instance of a request class
117 * @param $responseInstance An instance of a response class
120 public function processFilters (Requestable $requestInstance, Responseable $responseInstance) {
122 //* DEBUG */ self::createDebugInstance(__CLASS__)->debugOutput('COUNT=' . $this->countGenericArray('filters'));
123 foreach ($this->getFilters() as $filterInstance) {
124 // Must be an instance of Filterable
125 assert($filterInstance instanceof Filterable);
127 // Try to execute this filter
129 //* DEBUG */ self::createDebugInstance(__CLASS__)->debugOutput('FILTER: ' . $filterInstance->__toString() . ': Processing started.');
130 $filterInstance->execute($requestInstance, $responseInstance);
131 //* DEBUG */ self::createDebugInstance(__CLASS__)->debugOutput('FILTER: ' . $filterInstance->__toString() . ': Processing ended.');
132 } catch (FilterChainException $e) {
133 // This exception can be thrown to just skip any further processing
134 self::createDebugInstance(__CLASS__)->debugOutput('Failed to execute lase filter ' . $filterInstance->__toString() . ': ' . $e->getMessage());