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