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