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