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