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