]> 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\Object\ObjectFactory;
11 use Org\Mxchange\CoreFramework\Helper\Application\ApplicationHelper;
12 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
13 use Org\Mxchange\CoreFramework\Resolver\BaseResolver;
14 use Org\Mxchange\CoreFramework\Resolver\Controller\ControllerResolver;
15 use Org\Mxchange\CoreFramework\Utils\String\StringUtils;
16
17 // Import SPL stuff
18 use \InvalidArgumentException;
19
20 /**
21  * A generic controller resolver class
22  *
23  * @author              Roland Haeder <webmaster@shipsimu.org>
24  * @version             0.0.0
25  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2021 Core Developer Team
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          * Controller name
45          */
46         private $controllerName = '';
47
48         /**
49          * Protected constructor
50          *
51          * @param       $className      Name of the real class
52          * @return      void
53          */
54         protected function __construct (string $className) {
55                 // Call parent constructor
56                 parent::__construct($className);
57         }
58
59         /**
60          * Setter for controller name
61          *
62          * @param       $controllerName Last validated controller name
63          * @return      void
64          */
65         protected final function setControllerName (string $controllerName) {
66                 $this->controllerName = $controllerName;
67         }
68
69         /**
70          * Getter for controller name
71          *
72          * @return      $controllerName Last validated controller name
73          */
74         protected final function getControllerName () {
75                 return $this->controllerName;
76         }
77
78         /**
79          * "Loads" a given controller and instances it if not yet cached. If the
80          * controller was not found one of the default controllers will be used
81          * depending on whether news shall be displayed.
82          *
83          * @param       $controllerName                 A controller name we shall look for
84          * @return      $controllerInstance             A loaded controller instance
85          * @throws      InvalidControllerException      Thrown if even the requested
86          *                                                                              controller class is missing (bad!)
87          */
88         protected function loadController (string $controllerName) {
89                 // Cache default controller
90                 $defaultController = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('default_' . strtolower($this->getClassPrefix()) . '_controller');
91
92                 // Create full class name
93                 $className = sprintf(
94                         '%s\%sDefaultNewsController',
95                         $this->getNamespace(),
96                         $this->getCapitalizedClassPrefix()
97                 );
98
99                 // Default controller
100                 $this->setClassName($className);
101
102                 // Generate the class name
103                 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BEFORE: controller=' . $controllerName);
104                 if ($controllerName != $defaultController) {
105                         // Create controller class name
106                         $className = sprintf(
107                                 '%s\%s%sController',
108                                 $this->getNamespace(),
109                                 $this->getCapitalizedClassPrefix(),
110                                 StringUtils::convertToClassName($controllerName)
111                         );
112
113                         // ... and set it
114                         $this->setClassName($className);
115                 } else {
116                         // No news at main controller or non-news controller
117                         $this->setClassName($className);
118                 }
119
120                 // Is this class loaded?
121                 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('AFTER: controller=' . $this->getClassName());
122                 if (!class_exists($this->getClassName())) {
123                         // Throw an exception here
124                         throw new InvalidControllerException(array($this, $controllerName), self::EXCEPTION_INVALID_CONTROLLER);
125                 }
126
127                 // Try to read a config entry for our resolver including controller name... ;-)
128                 $resolverConfigEntry = sprintf('%s_cmd_%s_resolver_class', strtolower($this->getClassPrefix()), strtolower($controllerName));
129
130                 // Get the config, this will throw an exception if there is no special controller resolver
131                 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-CONTROLLER-RESOLVER: resolverConfigEntry=%s', $resolverConfigEntry));
132                 $resolverClass = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($resolverConfigEntry);
133
134                 // Initiate the resolver and controller
135                 $resolverInstance = ObjectFactory::createObjectByConfiguredName(
136                         $resolverConfigEntry,
137                         array(
138                                 $controllerName,
139                                 ApplicationHelper::getSelfInstance()
140                         )
141                 );
142
143                 // Get controller instance
144                 $controllerInstance = ObjectFactory::createObjectByName(
145                         $this->getClassName(),
146                         array($resolverInstance)
147                 );
148
149                 // Return the result
150                 return $controllerInstance;
151         }
152
153         /**
154          * Checks whether the given controller is valid
155          *
156          * @param       $namespace                      Namespace to look in, no trailing backslash
157          * @param       $controllerName         The default controller we shall execute
158          * @return      $isValid                        Whether the given controller is valid
159          * @throws      InvalidArgumentException                Thrown if given controller is not set
160          * @throws      DefaultControllerException      Thrown if default controller was not found
161          */
162         protected function isControllerValid (string $namespace, string $controllerName) {
163                 // Is a action set?
164                 if (empty($namespace)) {
165                         // Then thrown an exception here
166                         throw new InvalidArgumentException('Parameter "namespace" is empty');
167                 } elseif (empty($controllerName)) {
168                         // Then thrown an exception here
169                         throw new InvalidArgumentException('Parameter "controllerName" is empty');
170                 }
171
172                 // By default nothing shall be valid
173                 $isValid = false;
174
175                 // Create class name
176                 $className = sprintf(
177                         '%s\%s%sController',
178                         $namespace,
179                         $this->getCapitalizedClassPrefix(),
180                         StringUtils::convertToClassName($controllerName)
181                 );
182
183                 // Application's default news controller
184                 $appDefaultControllerName = sprintf(
185                         '%s\%sDefaultNewsController',
186                         $namespace,
187                         $this->getCapitalizedClassPrefix()
188                 );
189
190                 // Framework's default news controller
191                 $defaultControllerName = sprintf(
192                         'Org\Mxchange\CoreFramework\Controller\News\%sDefaultNewsController',
193                         $this->getCapitalizedClassPrefix()
194                 );
195
196                 // Now, let us create the full name of the controller class
197                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('className=%s', $className));
198                 $this->setClassName($className);
199
200                 // Try it hard to get an controller
201                 while ($isValid === false) {
202                         // Is this class already loaded?
203                         if (class_exists($this->getClassName())) {
204                                 // This class does exist. :-)
205                                 $isValid = true;
206                         } elseif ($this->getClassName() != $appDefaultControllerName) {
207                                 // Set application's default controller
208                                 $this->setClassName($appDefaultControllerName);
209                         } elseif ($this->getClassName() != $defaultControllerName) {
210                                 // Set framework's default controller
211                                 $this->setClassName($defaultControllerName);
212                         } else {
213                                 // All is tried, give it up here
214                                 throw new DefaultControllerException($this, self::EXCEPTION_DEFAULT_CONTROLLER_GONE);
215                         }
216                 }
217
218                 // Return the result
219                 return $isValid;
220         }
221
222         /**
223          * Resolves the default controller of the given controller
224          *
225          * @return      $controllerInstance             A controller instance for the default
226          *                                                                      controller
227          * @throws      InvalidControllerInstanceException      Thrown if $controllerInstance
228          *                                                                                              is invalid
229          */
230         public function resolveController () {
231                 // Init variables
232                 $controllerName = $this->getControllerName();
233
234                 // Get the controller
235                 $controllerInstance = $this->loadController($controllerName);
236
237                 // And validate it
238                 if ((!is_object($controllerInstance)) || (!$controllerInstance instanceof Controller)) {
239                         // This controller has an invalid instance!
240                         throw new InvalidControllerInstanceException(array($this, $controllerName), self::EXCEPTION_INVALID_CONTROLLER);
241                 }
242
243                 // Set last controller
244                 $this->setResolvedInstance($controllerInstance);
245
246                 // Return the maybe resolved instance
247                 return $controllerInstance;
248         }
249
250 }