class_WebControllerResolver.php

Go to the documentation of this file.
00001 <?php
00024 class WebControllerResolver extends BaseControllerResolver implements ControllerResolver {
00028         private $lastControllerName = "";
00029 
00033         private $lastControllerInstance = null;
00034 
00040         protected function __construct () {
00041                 // Call parent constructor
00042                 parent::__construct(__CLASS__);
00043 
00044                 // Set prefix to "Web"
00045                 $this->setControllerPrefix("Web");
00046         }
00047 
00057         public final static function createWebControllerResolver ($controllerName, ManageableApplication $appInstance) {
00058                 // Create the new instance
00059                 $resolverInstance = new WebControllerResolver();
00060 
00061                 // Is the variable $controllerName set and the command is valid?
00062                 if (empty($controllerName)) {
00063                         // Then thrown an exception here
00064                         throw new EmptyVariableException(array($resolverInstance, 'commandName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
00065                 } elseif ($resolverInstance->isControllerValid($controllerName) === false) {
00066                         // Invalid command found
00067                         throw new InvalidControllerException(array($resolverInstance, $controllerName), self::EXCEPTION_INVALID_CONTROLLER);
00068                 }
00069 
00070                 // Set the application instance
00071                 $resolverInstance->setApplicationInstance($appInstance);
00072 
00073                 // Set command name
00074                 $resolverInstance->setControllerName($controllerName);
00075 
00076                 // Return the prepared instance
00077                 return $resolverInstance;
00078         }
00079 
00088         public function resolveController () {
00089                 // Init variables
00090                 $controllerName = "";
00091                 $controllerInstance = null;
00092 
00093                 // Get the command name 
00094                 $controllerName = $this->getControllerName();
00095 
00096                 // Get the command
00097                 $controllerInstance = $this->loadController($controllerName);
00098 
00099                 // And validate it
00100                 if ((!is_object($controllerInstance)) || (!$controllerInstance instanceof Controller)) {
00101                         // This command has an invalid instance!
00102                         throw new InvalidControllerInstanceException(array($this, $controllerName), self::EXCEPTION_INVALID_CONTROLLER);
00103                 } // END - if
00104 
00105                 // Set last controller
00106                 $this->lastControllerInstance = $controllerInstance;
00107 
00108                 // Return the maybe resolved instance
00109                 return $controllerInstance;
00110         }
00111 
00122         private function loadController ($controllerName) {
00123                 // Cache default command
00124                 $defaultController = $this->getConfigInstance()->readConfig('default_web_command');
00125 
00126                 // Init controller instance
00127                 $controllerInstance = null;
00128 
00129                 // Default controller
00130                 $this->setClassName('WebDefaultController');
00131 
00132                 // Generate the class name
00133                 //* DEBUG: */ echo __METHOD__.": Controller=".$controllerName;
00134                 if ($controllerName != $defaultController) {
00135                         // Create controller class name
00136                         $this->setClassName(sprintf("Web%sController",
00137                                 $this->convertToClassName($controllerName)
00138                         ));
00139                 } elseif ($this->getConfigInstance()->readConfig('page_with_news') == $this->getApplicationInstance()->getRequestInstance()->getRequestElement('page')) {
00140                         // Yes, display news in home then set default controller with news
00141                         $this->setClassName('WebDefaultNewsController');
00142                 } else {
00143                         // No news at home page or non-news page
00144                         $this->setClassName('WebDefaultController');
00145                 }
00146                 //* DEBUG: */ echo ", controller=".$this->getClassName()."<br />\n";
00147 
00148                 // Is this class loaded?
00149                 if (!class_exists($this->getClassName())) {
00150                         // Throw an exception here
00151                         throw new InvalidControllerException(array($this, $controllerName), self::EXCEPTION_INVALID_CONTROLLER);
00152                 } // END - if
00153 
00154                 // Set default resolver config name
00155                 $resolverConfigEntry = "";
00156 
00157                 // Try to read a config entry for our resolver including controller name... ;-)
00158                 try {
00159                         // Create the resolver name
00160                         $resolverConfigEntry = sprintf("web_cmd_%s_resolver_class", strtolower($controllerName));
00161 
00162                         // Get the config, this will throw an exception if there is no special command resolver
00163                         $resolverClass = $this->getConfigInstance()->readConfig($resolverConfigEntry);
00164                 } catch (ConfigEntryNotFoundException $e) {
00165                         // Use default resolver entry
00166                         $resolverConfigEntry = "web_cmd_resolver_class";
00167                 }
00168 
00169                 // Initiate the resolver and controller
00170                 $resolverInstance = ObjectFactory::createObjectByConfiguredName($resolverConfigEntry, array($controllerName, $this->getApplicationInstance()));
00171                 $controllerInstance = ObjectFactory::createObjectByName($this->getClassName(), array($resolverInstance));
00172 
00173                 // Remove resolver (we don't need it anymore)
00174                 unset($resolverInstance);
00175 
00176                 // Return the result
00177                 return $controllerInstance;
00178         }
00179 }
00180 
00181 // [EOF]
00182 ?>

Generated on Mon Dec 8 01:06:46 2008 for Ship-Simulator by  doxygen 1.5.6