3 namespace CoreFramework\Chain\Filter;
5 // Import framework stuff
6 use CoreFramework\Object\BaseFrameworkSystem;
7 use CoreFramework\Registry\Registerable;
8 use CoreFramework\Request\Requestable;
9 use CoreFramework\Response\Responseable;
12 * A filter chain for pre and post filters
14 * @author Roland Haeder <webmaster@shipsimu.org>
16 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
17 * @license GNU GPL 3.0 or any newer version
18 * @link http://www.shipsimu.org
20 * This program is free software: you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation, either version 3 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 class FilterChain extends BaseFrameworkSystem implements Registerable {
35 * Protected constructor
39 protected function __construct () {
40 // Call parent constructor
41 parent::__construct(__CLASS__);
45 * Creates an instance of this class
47 * @return $chainInstance An instance of this class
49 public static final function createFilterChain () {
51 $chainInstance = new FilterChain();
53 // Return the prepared instance
54 return $chainInstance;
60 * @param $filerInstance An instance of a filter class
63 public final function addFilter (Filterable $filterInstance) {
64 $this->pushValueToGenericArrayKey('filters', 'generic', 'dummy', $filterInstance);
68 * Add a new post-filter
70 * @param $filerInstance An instance of a post-filter class
73 public final function addPostFilter (Filterable $filterInstance) {
74 $this->pushValueToGenericArrayKey('filters', 'post', 'dummy', $filterInstance);
78 * "Getter" for filters array
80 * @return $filters The filters array holding all filter instances
82 protected function getFilters () {
83 // Default is nothing found
86 // Are some filters set?
87 if ($this->isValidGenericArrayKey('filters', 'generic', 'dummy')) {
89 $filters = $this->getGenericArrayKey('filters', 'generic', 'dummy');
97 * "Getter" for post-filters array
99 * @return $filters The filters array holding all post-filter instances
101 protected function getPostFilters () {
102 // Default is nothing found
105 // Are some filters set?
106 if ($this->isValidGenericArrayKey('filters', 'post', 'dummy')) {
108 $filters = $this->getGenericArrayKey('filters', 'post', 'dummy');
116 * Process all added filters. Please note that filters must throw
117 * FilterChainException if they need to interrupt the filter chain.
119 * @param $requestInstance An instance of a Requestable class
120 * @param $responseInstance An instance of a Responseable class
123 public function processFilters (Requestable $requestInstance, Responseable $responseInstance) {
125 //* DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('COUNT=' . $this->countGenericArray('filters'));
126 foreach ($this->getFilters() as $filterInstance) {
127 // Must be an instance of Filterable
128 assert($filterInstance instanceof Filterable);
130 // Try to execute this filter
132 //* DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILTER: ' . $filterInstance->__toString() . ': Processing started.');
133 $filterInstance->execute($requestInstance, $responseInstance);
134 //* DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILTER: ' . $filterInstance->__toString() . ': Processing ended.');
135 } catch (FilterChainException $e) {
136 // This exception can be thrown to just skip any further processing
137 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('Failed to execute lase filter ' . $filterInstance->__toString() . ': ' . $e->getMessage());