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