6ec8fc4c6857c14f886988d16fa8eb2551202df9
[shipsimu.git] / inc / classes / main / resolver / controller / image / class_ImageControllerResolver.php
1 <?php
2 /**
3  * A resolver for resolving controllers locally
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, this is free software
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.ship-simu.org
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <http://www.gnu.org/licenses/>.
23  */
24 class ImageControllerResolver extends BaseControllerResolver implements ControllerResolver {
25         /**
26          * Last successfull resolved controller (name)
27          */
28         private $lastControllerName = "";
29
30         /**
31          * Last successfull resolved controller (instance)
32          */
33         private $lastControllerInstance = null;
34
35         /**
36          * Protected constructor
37          *
38          * @return      void
39          */
40         protected function __construct () {
41                 // Call parent constructor
42                 parent::__construct(__CLASS__);
43
44                 // Set part description
45                 $this->setObjectDescription("Resolver for local web controllers");
46
47                 // Create unique ID number
48                 $this->generateUniqueId();
49
50                 // Set prefix to "Image"
51                 $this->setControllerPrefix("Image");
52         }
53
54         /**
55          * Creates an instance of a resolver class with a given command
56          *
57          * @param       $controllerName                         The controller we shall resolve
58          * @param       $appInstance                            An instance of a manageable application helper class
59          * @return      $resolverInstance                       The prepared controller resolver instance
60          * @throws      EmptyVariableException          Thrown if the default command is not set
61          * @throws      InvalidControllerException      Thrown if the default controller is invalid
62          */
63         public final static function createImageControllerResolver ($controllerName, ManageableApplication $appInstance) {
64                 // Create the new instance
65                 $resolverInstance = new ImageControllerResolver();
66
67                 // Is the variable $controllerName set and the command is valid?
68                 if (empty($controllerName)) {
69                         // Then thrown an exception here
70                         throw new EmptyVariableException(array($resolverInstance, 'commandName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
71                 } elseif (!$resolverInstance->isControllerValid($controllerName)) {
72                         // Invalid command found
73                         throw new InvalidControllerException(array($resolverInstance, $controllerName), self::EXCEPTION_INVALID_CONTROLLER);
74                 }
75
76                 // Set the application instance
77                 $resolverInstance->setApplicationInstance($appInstance);
78
79                 // Set command name
80                 $resolverInstance->setControllerName($controllerName);
81
82                 // Return the prepared instance
83                 return $resolverInstance;
84         }
85
86         /**
87          * Resolves the default controller of the given command
88          *
89          * @return      $controllerInstance             A controller instance for the default
90          *                                                                      command
91          * @throws      InvalidControllerInstanceException      Thrown if $controllerInstance
92          *                                                                                              is invalid
93          */
94         public function resolveController () {
95                 // Init variables
96                 $controllerName = "";
97                 $controllerInstance = null;
98
99                 // Get the command name 
100                 $controllerName = $this->getControllerName();
101
102                 // Get the command
103                 $controllerInstance = $this->loadController($controllerName);
104
105                 // And validate it
106                 if ((!is_object($controllerInstance)) || (!$controllerInstance instanceof Controller)) {
107                         // This command has an invalid instance!
108                         throw new InvalidControllerInstanceException(array($this, $controllerName), self::EXCEPTION_INVALID_CONTROLLER);
109                 } // END - if
110
111                 // Set last controller
112                 $this->lastControllerInstance = $controllerInstance;
113
114                 // Return the maybe resolved instance
115                 return $controllerInstance;
116         }
117
118         /**
119          * "Loads" a given controller and instances it if not yet cached. If the
120          * controller was not found one of the default controllers will be used
121          * depending on wether news shall be displayed.
122          *
123          * @param       $controllerName                 A controller name we shall look for
124          * @return      $controllerInstance             A loaded controller instance
125          * @throws      InvalidControllerException      Thrown if even the requested
126          *                                                                              controller class is missing (bad!)
127          */
128         private function loadController ($controllerName) {
129                  // Debug message
130                  //* DEBUG: */ $this->debugBackTrace();
131
132                 // Cache default command
133                 $defaultController = $this->getConfigInstance()->readConfig('default_image_command');
134
135                 // Init controller instance
136                 $controllerInstance = null;
137
138                 // Default controller
139                 $this->setClassName('ImageDefaultController');
140
141                 // Generate the class name
142                 //* DEBUG: */ echo __METHOD__.": Controller=".$controllerName;
143                 if ($controllerName != $defaultController) {
144                         // Create controller class name
145                         $this->setClassName(sprintf("Image%sController",
146                                 $this->convertToClassName($controllerName)
147                         ));
148                 } else {
149                         // Default controller
150                         $this->setClassName('ImageDefaultController');
151                 }
152                 //* DEBUG: */ echo ", controller=".$this->getClassName()."<br />\n";
153
154                 // Is this class loaded?
155                 if (!class_exists($this->getClassName())) {
156                         // Class not found, so or throw an exception
157                         throw new InvalidControllerException(array($this, $controllerName), self::EXCEPTION_INVALID_CONTROLLER);
158                 } // END - if
159
160                 // Initiate the resolver and controller
161                 $resolverInstance = ObjectFactory::createObjectByConfiguredName('image_cmd_resolver_class', array($controllerName, $this->getApplicationInstance()));
162                 $controllerInstance = ObjectFactory::createObjectByName($this->getClassName(), array($resolverInstance));
163
164                 // Remove resolver
165                 unset($resolverInstance);
166
167                 // Return the result
168                 return $controllerInstance;
169         }
170 }
171
172 // [EOF]
173 ?>