Moved some class fields and their setter/getter to BaseFrameworkSystem.
[core.git] / inc / classes / main / resolver / controller / class_BaseControllerResolver.php
1 <?php
2 /**
3  * A generic controller resolver class
4  *
5  * @author              Roland Haeder <webmaster@shipsimu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Core Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.shipsimu.org
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <http://www.gnu.org/licenses/>.
23  */
24 class BaseControllerResolver extends BaseResolver {
25         /**
26          * Protected constructor
27          *
28          * @param       $className      Name of the real class
29          * @return      void
30          */
31         protected function __construct ($className) {
32                 // Call parent constructor
33                 parent::__construct($className);
34         }
35
36         /**
37          * "Loads" a given controller and instances it if not yet cached. If the
38          * controller was not found one of the default controllers will be used
39          * depending on whether news shall be displayed.
40          *
41          * @param       $controllerName                 A controller name we shall look for
42          * @return      $controllerInstance             A loaded controller instance
43          * @throws      InvalidControllerException      Thrown if even the requested
44          *                                                                              controller class is missing (bad!)
45          */
46         protected function loadController ($controllerName) {
47                 // Cache default command
48                 $defaultController = $this->getConfigInstance()->getConfigEntry('default_' . strtolower($this->getClassPrefix()) . '_command');
49
50                 // Init controller instance
51                 $controllerInstance = NULL;
52
53                 // Default controller
54                 $this->setClassName($this->getCapitalizedClassPrefix() . 'DefaultNewsController');
55
56                 // Generate the class name
57                 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('BEFORE: controller=' . $controllerName);
58                 if ($controllerName != $defaultController) {
59                         // Create controller class name
60                         $className = $this->getCapitalizedClassPrefix() . $this->convertToClassName($controllerName) . 'Controller';
61
62                         // ... and set it
63                         $this->setClassName($className);
64                 } else {
65                         // No news at main command or non-news command
66                         $this->setClassName($this->getCapitalizedClassPrefix() . 'DefaultNewsController');
67                 }
68                 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('AFTER: controller=' . $this->getClassName());
69
70                 // Is this class loaded?
71                 if (!class_exists($this->getClassName())) {
72                         // Throw an exception here
73                         throw new InvalidControllerException(array($this, $controllerName), self::EXCEPTION_INVALID_CONTROLLER);
74                 } // END - if
75
76                 // Set default resolver config name
77                 $resolverConfigEntry = '';
78
79                 // Try to read a config entry for our resolver including controller name... ;-)
80                 $resolverConfigEntry = sprintf('%s_cmd_%s_resolver_class', strtolower($this->getClassPrefix()), strtolower($controllerName));
81
82                 // Get the config, this will throw an exception if there is no special command resolver
83                 $resolverClass = $this->getConfigInstance()->getConfigEntry($resolverConfigEntry);
84
85                 // Initiate the resolver and controller
86                 $resolverInstance = ObjectFactory::createObjectByConfiguredName(
87                         $resolverConfigEntry,
88                         array(
89                                 $controllerName,
90                                 $this->getApplicationInstance()
91                         )
92                 );
93                 $controllerInstance = ObjectFactory::createObjectByName(
94                         $this->getClassName(),
95                         array($resolverInstance)
96                 );
97
98                 // Return the result
99                 return $controllerInstance;
100         }
101
102         /**
103          * Checks whether the given controller is valid
104          *
105          * @param       $controllerName         The default controller we shall execute
106          * @return      $isValid                        Whether the given controller is valid
107          * @throws      EmptyVariableException          Thrown if given controller is not set
108          * @throws      DefaultControllerException      Thrown if default controller was not found
109          */
110         public function isControllerValid ($controllerName) {
111                 // By default nothing shall be valid
112                 $isValid = FALSE;
113
114                 // Is a controller set?
115                 if (empty($controllerName)) {
116                         // Then thrown an exception here
117                         throw new EmptyVariableException(array($this, 'controllerName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
118                 } // END - if
119
120                 // Create class name
121                 $className = $this->getCapitalizedClassPrefix() . $this->convertToClassName($controllerName) . 'Controller';
122
123                 // Now, let us create the full name of the controller class
124                 $this->setClassName($className);
125
126                 // Try it hard to get an controller
127                 while ($isValid === FALSE) {
128                         // Is this class already loaded?
129                         if (class_exists($this->getClassName())) {
130                                 // This class does exist. :-)
131                                 $isValid = TRUE;
132                         } elseif ($this->getClassName() != $this->getCapitalizedClassPrefix() . 'DefaultNewsController') {
133                                 // Set default controller
134                                 $this->setClassName($this->getCapitalizedClassPrefix() . 'DefaultNewsController');
135                         } else {
136                                 // All is tried, give it up here
137                                 throw new DefaultControllerException($this, self::EXCEPTION_DEFAULT_CONTROLLER_GONE);
138                         }
139                 } // END - while
140
141                 // Return the result
142                 return $isValid;
143         }
144 }
145
146 // [EOF]
147 ?>