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