Fixed a typo
[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@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Core Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.ship-simu.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          * Validated controller name
27          */
28         private $controllerName = '';
29
30         /**
31          * Protected constructor
32          *
33          * @param       $className      Name of the real class
34          * @return      void
35          */
36         protected function __construct ($className) {
37                 // Call parent constructor
38                 parent::__construct($className);
39         }
40
41         /**
42          * Setter for controller name
43          *
44          * @param       $controllerName         Last validated controller name
45          * @return      void
46          */
47         protected final function setControllerName ($controllerName) {
48                 $this->controllerName = $controllerName;
49         }
50
51         /**
52          * Getter for controller name
53          *
54          * @return      $controllerName Last validated controller name
55          */
56         public final function getControllerName () {
57                 return $this->controllerName;
58         }
59
60         /**
61          * "Loads" a given controller and instances it if not yet cached. If the
62          * controller was not found one of the default controllers will be used
63          * depending on whether news shall be displayed.
64          *
65          * @param       $controllerName                 A controller name we shall look for
66          * @return      $controllerInstance             A loaded controller instance
67          * @throws      InvalidControllerException      Thrown if even the requested
68          *                                                                              controller class is missing (bad!)
69          */
70         protected function loadController ($controllerName) {
71                 // Cache default command
72                 $defaultController = $this->getConfigInstance()->getConfigEntry('default_' . strtolower($this->getClassPrefix()) . '_command');
73
74                 // Init controller instance
75                 $controllerInstance = NULL;
76
77                 // Default controller
78                 $this->setClassName($this->getClassPrefix() . 'DefaultNewsController');
79
80                 // Generate the class name
81                 //* DEBUG: */ $this->debugOutput('BEFORE: controller=' . $controllerName);
82                 if ($controllerName != $defaultController) {
83                         // Create controller class name
84                         $className = $this->getClassPrefix() . $this->convertToClassName($controllerName) . 'Controller';
85
86                         // ... and set it
87                         $this->setClassName($className);
88                 } else {
89                         // No news at main command or non-news command
90                         $this->setClassName($this->getClassPrefix() . 'DefaultNewsController');
91                 }
92                 //* DEBUG: */ $this->debugOutput('AFTER: controller=' . $this->getClassName());
93
94                 // Is this class loaded?
95                 if (!class_exists($this->getClassName())) {
96                         // Throw an exception here
97                         throw new InvalidControllerException(array($this, $controllerName), self::EXCEPTION_INVALID_CONTROLLER);
98                 } // END - if
99
100                 // Set default resolver config name
101                 $resolverConfigEntry = '';
102
103                 // Try to read a config entry for our resolver including controller name... ;-)
104                 $resolverConfigEntry = sprintf("%s_cmd_%s_resolver_class", strtolower($this->getClassPrefix()), strtolower($controllerName));
105
106                 // Get the config, this will throw an exception if there is no special command resolver
107                 $resolverClass = $this->getConfigInstance()->getConfigEntry($resolverConfigEntry);
108
109                 // Initiate the resolver and controller
110                 $resolverInstance = ObjectFactory::createObjectByConfiguredName(
111                         $resolverConfigEntry,
112                         array(
113                                 $controllerName,
114                                 $this->getApplicationInstance()
115                         )
116                 );
117                 $controllerInstance = ObjectFactory::createObjectByName(
118                         $this->getClassName(),
119                         array($resolverInstance)
120                 );
121
122                 // Return the result
123                 return $controllerInstance;
124         }
125
126         /**
127          * Checks whether the given controller is valid
128          *
129          * @param       $controllerName         The default controller we shall execute
130          * @return      $isValid                        Whether the given controller is valid
131          * @throws      EmptyVariableException          Thrown if given controller is not set
132          * @throws      DefaultControllerException      Thrown if default controller was not found
133          */
134         public function isControllerValid ($controllerName) {
135                 // By default nothing shall be valid
136                 $isValid = false;
137
138                 // Is a controller set?
139                 if (empty($controllerName)) {
140                         // Then thrown an exception here
141                         throw new EmptyVariableException(array($this, 'controllerName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
142                 } // END - if
143
144                 // Create class name
145                 $className = $this->getClassPrefix() . $this->convertToClassName($controllerName) . 'Controller';
146
147                 // Now, let us create the full name of the controller class
148                 $this->setClassName($className);
149
150                 // Try it hard to get an controller
151                 while ($isValid === false) {
152                         // Is this class already loaded?
153                         if (class_exists($this->getClassName())) {
154                                 // This class does exist. :-)
155                                 $isValid = true;
156                         } elseif ($this->getClassName() != $this->getClassPrefix() . 'DefaultNewsController') {
157                                 // Set default controller
158                                 $this->setClassName($this->getClassPrefix() . 'DefaultNewsController');
159                         } else {
160                                 // All is tried, give it up here
161                                 throw new DefaultControllerException($this, self::EXCEPTION_DEFAULT_CONTROLLER_GONE);
162                         }
163                 } // END - while
164
165                 // Return the result
166                 return $isValid;
167         }
168 }
169
170 // [EOF]
171 ?>