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