]> git.mxchange.org Git - shipsimu.git/blobdiff - inc/classes/main/controller/class_BaseController.php
Type-hints fixed, header docs fixed, exceptions deprecated
[shipsimu.git] / inc / classes / main / controller / class_BaseController.php
index 4022a77c9370f1ea3bc5a3b3d31d05a564ec3149..1686f42f7c23238c475019c33017ad2df808915b 100644 (file)
@@ -1,12 +1,14 @@
 <?php
 /**
- * A generic controller class
+ * A generic controller class. You should extend this base class if you want to
+ * write your own controller. You get the advantage that you can use the pre and
+ * post filters.
  *
  * @author             Roland Haeder <webmaster@ship-simu.org>
- * @version            0.3.0
+ * @version            0.0.0
  * @copyright  Copyright(c) 2007, 2008 Roland Haeder, this is free software
  * @license            GNU GPL 3.0 or any newer version
- * @link               http://www.mxchange.org
+ * @link               http://www.ship-simu.org
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
-class BaseController extends BaseFrameworkSystem {
+class BaseController extends BaseFrameworkSystem implements Registerable {
        /**
-        * Private constructor
+        * Pre filter chain instance
+        */
+       private $preFilterChain = null;
+
+       /**
+        * Post filter chain instance
+        */
+       private $postFilterChain = null;
+
+       /**
+        * Protected constructor
         *
+        * @param       $className      Name of the class
         * @return      void
         */
-       private function __construct () {
+       protected function __construct ($className) {
                // Call parent constructor
-               parent::constructor(__CLASS__);
+               parent::__construct($className);
 
                // Clean up a little
                $this->removeNumberFormaters();
+               $this->removeSystemArray();
+
+               // Initialize both filter chains
+               $this->preFilterChain  = ObjectFactory::createObjectByConfiguredName('filter_chain_class');
+               $this->postFilterChain = ObjectFactory::createObjectByConfiguredName('filter_chain_class');
+
+               // Add this controller to the registry
+               Registry::getRegistry()->addInstance('controller', $this);
+       }
+
+       /**
+        * Adds a filter to the pre filter chain
+        *
+        * @param       $filterInstance         An instance of a filter
+        * @return      void
+        */
+       public function addPreFilter (Filterable $filterInstance) {
+               // Add the pre filter
+               $this->preFilterChain->addFilter($filterInstance);
+       }
+
+       /**
+        * Adds a filter to the post filter chain
+        *
+        * @param       $filterInstance         An instance of a filter
+        * @return      void
+        */
+       public function addPostFilter (Filterable $filterInstance) {
+               // Add the post filter
+               $this->postFilterChain->addFilter($filterInstance);
+       }
+
+       /**
+        * Executes all pre filters
+        *
+        * @param       $requestInstance        An instance of a request class
+        * @param       $responseInstance       An instance of a response class
+        * @return      void
+        */
+       protected function executePreFilters (Requestable $requestInstance, Responseable $responseInstance) {
+               // Execute all pre filters
+               $this->preFilterChain->processFilters($requestInstance, $responseInstance);
        }
 
        /**
-        * The public constructor
+        * Executes all post filters
         *
+        * @param       $requestInstance        An instance of a request class
+        * @param       $responseInstance       An instance of a response class
         * @return      void
         */
-       public function constructor ($class) {
-               // Calls just the private one
-               $this->__construct($class);
+       protected function executePostFilters (Requestable $requestInstance, Responseable $responseInstance) {
+               // Execute all post filters
+               $this->postFilterChain->processFilters($requestInstance, $responseInstance);
        }
 }