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