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