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 - 2017 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                 // Set default resolver config name
104                 $resolverConfigEntry = '';
105
106                 // Try to read a config entry for our resolver including controller name... ;-)
107                 $resolverConfigEntry = sprintf('%s_cmd_%s_resolver_class', strtolower($this->getClassPrefix()), strtolower($controllerName));
108
109                 // Get the config, this will throw an exception if there is no special controller resolver
110                 $resolverClass = $this->getConfigInstance()->getConfigEntry($resolverConfigEntry);
111
112                 // Initiate the resolver and controller
113                 $resolverInstance = ObjectFactory::createObjectByConfiguredName(
114                         $resolverConfigEntry,
115                         array(
116                                 $controllerName,
117                                 GenericRegistry::getRegistry()->getInstance('application')
118                         )
119                 );
120                 $controllerInstance = ObjectFactory::createObjectByName(
121                         $this->getClassName(),
122                         array($resolverInstance)
123                 );
124
125                 // Return the result
126                 return $controllerInstance;
127         }
128
129         /**
130          * Checks whether the given controller is valid
131          *
132          * @param       $namespace                      Namespace to look in, no trailing backslash
133          * @param       $controllerName         The default controller we shall execute
134          * @return      $isValid                        Whether the given controller is valid
135          * @throws      InvalidArgumentException                Thrown if given controller is not set
136          * @throws      DefaultControllerException      Thrown if default controller was not found
137          */
138         protected function isControllerValid ($namespace, $controllerName) {
139                 // By default nothing shall be valid
140                 $isValid = false;
141
142                 // Is namespace and controller name set?
143                 if (empty($namespace)) {
144                         // Then thrown an exception here
145                         throw new InvalidArgumentException('Parameter "namespace" is empty');
146                 } elseif (empty($controllerName)) {
147                         // Then thrown an exception here
148                         throw new InvalidArgumentException('Parameter "controllerName" is empty');
149                 }
150
151                 // Create class name
152                 $className = sprintf(
153                         '%s\%sController',
154                         $namespace,
155                         $this->getCapitalizedClassPrefix() . self::convertToClassName($controllerName)
156                 );
157                 $newsControllerName = sprintf(
158                         '%s\%sDefaultNewsController',
159                         $namespace,
160                         $this->getCapitalizedClassPrefix()
161                 );
162
163                 // Debug message
164                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('className=%s', $className));
165
166                 // Now, let us create the full name of the controller class
167                 $this->setClassName($className);
168
169                 // Try it hard to get an controller
170                 while ($isValid === false) {
171                         // Is this class already loaded?
172                         if (class_exists($this->getClassName())) {
173                                 // This class does exist. :-)
174                                 $isValid = true;
175                         } elseif ($this->getClassName() != $newsControllerName) {
176                                 // Set default controller
177                                 $this->setClassName($newsControllerName);
178                         } else {
179                                 // All is tried, give it up here
180                                 throw new DefaultControllerException($this, self::EXCEPTION_DEFAULT_CONTROLLER_GONE);
181                         }
182                 } // END - while
183
184                 // Return the result
185                 return $isValid;
186         }
187
188         /**
189          * Resolves the default controller of the given controller
190          *
191          * @return      $controllerInstance             A controller instance for the default
192          *                                                                      controller
193          * @throws      InvalidControllerInstanceException      Thrown if $controllerInstance
194          *                                                                                              is invalid
195          */
196         public function resolveController () {
197                 // Init variables
198                 $controllerName = '';
199                 $controllerInstance = NULL;
200
201                 // Get namespace and controller name
202                 $controllerName = $this->getControllerName();
203
204                 // Get the controller
205                 $controllerInstance = $this->loadController($controllerName);
206
207                 // And validate it
208                 if ((!is_object($controllerInstance)) || (!$controllerInstance instanceof Controller)) {
209                         // This controller has an invalid instance!
210                         throw new InvalidControllerInstanceException(array($this, $controllerName), self::EXCEPTION_INVALID_CONTROLLER);
211                 } // END - if
212
213                 // Set last controller
214                 $this->setResolvedInstance($controllerInstance);
215
216                 // Return the maybe resolved instance
217                 return $controllerInstance;
218         }
219
220 }