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