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