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