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