From 7e46f248cc9eb9a3cd540b2e864e053f40a46bcd Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sat, 18 Jul 2009 07:49:53 +0000 Subject: [PATCH] loadController() is now more generic (TODO: check that all other apps works) --- .gitattributes | 1 + .../factories/web/class_WebNewsFactory.php | 2 +- .../main/reader/class_ConsoleNewsReader.php | 87 +++++++++++++++++++ .../main/reader/class_DefaultNewsReader.php | 8 +- .../console/class_ConsoleCommandResolver.php | 2 +- .../class_BaseControllerResolver.php | 73 ++++++++++++++++ .../class_ConsoleControllerResolver.php | 73 ---------------- .../image/class_ImageControllerResolver.php | 54 ------------ .../web/class_WebControllerResolver.php | 73 ---------------- 9 files changed, 167 insertions(+), 206 deletions(-) create mode 100644 inc/classes/main/reader/class_ConsoleNewsReader.php diff --git a/.gitattributes b/.gitattributes index bef744e4..dfdc488d 100644 --- a/.gitattributes +++ b/.gitattributes @@ -401,6 +401,7 @@ inc/classes/main/output/class_WebOutput.php -text inc/classes/main/points/.htaccess -text inc/classes/main/points/class_UserPoints.php -text inc/classes/main/reader/.htaccess -text +inc/classes/main/reader/class_ConsoleNewsReader.php -text inc/classes/main/reader/class_DefaultNewsReader.php -text inc/classes/main/registration/.htaccess -text inc/classes/main/registration/class_BaseRegistration.php -text diff --git a/inc/classes/main/factories/web/class_WebNewsFactory.php b/inc/classes/main/factories/web/class_WebNewsFactory.php index 10386e23..304d5a15 100644 --- a/inc/classes/main/factories/web/class_WebNewsFactory.php +++ b/inc/classes/main/factories/web/class_WebNewsFactory.php @@ -46,7 +46,7 @@ class WebNewsFactory extends BaseFactory { */ public final static function createFactoryByRequest (Requestable $requestInstance) { // Set default news reader class - $configEntry = "news_reader_class"; + $configEntry = 'news_reader_class'; // Get "page" $page = $requestInstance->getRequestElement('page'); diff --git a/inc/classes/main/reader/class_ConsoleNewsReader.php b/inc/classes/main/reader/class_ConsoleNewsReader.php new file mode 100644 index 00000000..0240b74d --- /dev/null +++ b/inc/classes/main/reader/class_ConsoleNewsReader.php @@ -0,0 +1,87 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Core Developer Team + * @license GNU GPL 3.0 or any newer version + * @link http://www.ship-simu.org + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +class DefaultNewsReader extends BaseFrameworkSystem implements ReadableNews, Registerable { + /** + * Protected constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + + // Clean up a little + $this->removeNumberFormaters(); + $this->removeSystemArray(); + } + + /** + * Creates an instance of this reader by a provided request instance + * + * @param $requestInstance An instance of a Requestable class + * @return $readerInstance An instance of this reader class + */ + public final static function createDefaultNewsReader (Requestable $requestInstance) { + // Get a new instance + $readerInstance = new DefaultNewsReader(); + + // Set request instance + $readerInstance->setRequestInstance($requestInstance); + + // Return prepared instance + return $readerInstance; + } + + /** + * Initializes this reader class by pre-fetching news depending on 'page' + * (outside or login area), which amount of news and how much to skip + * + * @return void + */ + public function initializeReader () { + // Get 'page' for saving some calls + $page = $this->getRequestInstance()->getRequestElement('page'); + + // First get a wrapper instance + $wrapperInstance = ObjectFactory::createObjectByConfiguredName('news_db_wrapper_class'); + + // Next create a searchable criteria instance + $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class'); + + // Add the page as criteria to it at lease + $criteriaInstance->addCriteria('page', $page); + + // Add limitation from config + $criteriaInstance->setLimit($this->getConfigInstance()->readConfig('news_' . $page . '_limit')); + + // Get a resultInstance back from the database + $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance); + + // Save that resultInstance in this class + $this->setResultInstance($resultInstance); + } +} + +// [EOF] +?> diff --git a/inc/classes/main/reader/class_DefaultNewsReader.php b/inc/classes/main/reader/class_DefaultNewsReader.php index a0c4fb61..0240b74d 100644 --- a/inc/classes/main/reader/class_DefaultNewsReader.php +++ b/inc/classes/main/reader/class_DefaultNewsReader.php @@ -54,13 +54,13 @@ class DefaultNewsReader extends BaseFrameworkSystem implements ReadableNews, Reg } /** - * Initializes this reader class by pre-fetching news depending on "page" + * Initializes this reader class by pre-fetching news depending on 'page' * (outside or login area), which amount of news and how much to skip * * @return void */ public function initializeReader () { - // Get "page" for saving some calls + // Get 'page' for saving some calls $page = $this->getRequestInstance()->getRequestElement('page'); // First get a wrapper instance @@ -70,10 +70,10 @@ class DefaultNewsReader extends BaseFrameworkSystem implements ReadableNews, Reg $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class'); // Add the page as criteria to it at lease - $criteriaInstance->addCriteria("page", $page); + $criteriaInstance->addCriteria('page', $page); // Add limitation from config - $criteriaInstance->setLimit($this->getConfigInstance()->readConfig("news_{$page}_limit")); + $criteriaInstance->setLimit($this->getConfigInstance()->readConfig('news_' . $page . '_limit')); // Get a resultInstance back from the database $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance); diff --git a/inc/classes/main/resolver/command/console/class_ConsoleCommandResolver.php b/inc/classes/main/resolver/command/console/class_ConsoleCommandResolver.php index 9a212ab7..5dffd576 100644 --- a/inc/classes/main/resolver/command/console/class_ConsoleCommandResolver.php +++ b/inc/classes/main/resolver/command/console/class_ConsoleCommandResolver.php @@ -86,7 +86,7 @@ class ConsoleCommandResolver extends BaseCommandResolver implements CommandResol $commandInstance = null; // This goes fine so let's resolve the command - $commandName = $requestInstance->getRequestElement('page'); + $commandName = $requestInstance->getRequestElement('command'); // Is the command empty? Then fall back to default command if (empty($commandName)) $commandName = $this->getConfigInstance()->readConfig('default_console_command'); diff --git a/inc/classes/main/resolver/controller/class_BaseControllerResolver.php b/inc/classes/main/resolver/controller/class_BaseControllerResolver.php index 23e449ac..ece1f26d 100644 --- a/inc/classes/main/resolver/controller/class_BaseControllerResolver.php +++ b/inc/classes/main/resolver/controller/class_BaseControllerResolver.php @@ -72,6 +72,79 @@ class BaseControllerResolver extends BaseResolver { return $this->controllerName; } + /** + * "Loads" a given controller and instances it if not yet cached. If the + * controller was not found one of the default controllers will be used + * depending on wether news shall be displayed. + * + * @param $controllerName A controller name we shall look for + * @return $controllerInstance A loaded controller instance + * @throws InvalidControllerException Thrown if even the requested + * controller class is missing (bad!) + */ + protected function loadController ($controllerName) { + // Cache default command + $defaultController = $this->getConfigInstance()->readConfig('default_' . strtolower($this->getControllerPrefix()) . '_command'); + + // Init controller instance + $controllerInstance = null; + + // Default controller + $this->setClassName($this->getControllerPrefix() . 'DefaultNewsController'); + + // Generate the class name + //* DEBUG: */ echo __METHOD__.": Controller=".$controllerName; + if ($controllerName != $defaultController) { + // Create controller class name + $className = $this->getControllerPrefix() . '' . $this->convertToClassName($controllerName) . 'Controller'; + + // ... and set it + $this->setClassName($className); + } else { + // No news at main command or non-news command + $this->setClassName($this->getControllerPrefix() . 'DefaultNewsController'); + } + //* DEBUG: */ echo ", controller=".$this->getClassName()."
\n"; + + // Is this class loaded? + if (!class_exists($this->getClassName())) { + // Throw an exception here + throw new InvalidControllerException(array($this, $controllerName), self::EXCEPTION_INVALID_CONTROLLER); + } // END - if + + // Set default resolver config name + $resolverConfigEntry = ''; + + // Try to read a config entry for our resolver including controller name... ;-) + try { + // Create the resolver name + $resolverConfigEntry = sprintf("%s_cmd_%s_resolver_class", strtolower($this->getControllerPrefix(), strtolower($controllerName)); + + // Get the config, this will throw an exception if there is no special command resolver + $resolverClass = $this->getConfigInstance()->readConfig($resolverConfigEntry); + } catch (ConfigEntryNotFoundException $e) { + // Use default resolver entry + // @TODO Maybe we need to log this? + $resolverConfigEntry = $this->getControllerPrefix() . '_cmd_resolver_class'; + } + + // Initiate the resolver and controller + $resolverInstance = ObjectFactory::createObjectByConfiguredName( + $resolverConfigEntry, + array( + $controllerName, + $this->getApplicationInstance() + ) + ); + $controllerInstance = ObjectFactory::createObjectByName( + $this->getClassName(), + array($resolverInstance) + ); + + // Return the result + return $controllerInstance; + } + /** * Checks wether the given controller is valid * diff --git a/inc/classes/main/resolver/controller/console/class_ConsoleControllerResolver.php b/inc/classes/main/resolver/controller/console/class_ConsoleControllerResolver.php index f5cbe43f..35430d43 100644 --- a/inc/classes/main/resolver/controller/console/class_ConsoleControllerResolver.php +++ b/inc/classes/main/resolver/controller/console/class_ConsoleControllerResolver.php @@ -108,79 +108,6 @@ class ConsoleControllerResolver extends BaseControllerResolver implements Contro // Return the maybe resolved instance return $controllerInstance; } - - /** - * "Loads" a given controller and instances it if not yet cached. If the - * controller was not found one of the default controllers will be used - * depending on wether news shall be displayed. - * - * @param $controllerName A controller name we shall look for - * @return $controllerInstance A loaded controller instance - * @throws InvalidControllerException Thrown if even the requested - * controller class is missing (bad!) - */ - private function loadController ($controllerName) { - // Cache default command - $defaultController = $this->getConfigInstance()->readConfig('default_console_command'); - - // Init controller instance - $controllerInstance = null; - - // Default controller - $this->setClassName('ConsoleDefaultNewsController'); - - // Generate the class name - //* DEBUG: */ echo __METHOD__.": Controller=".$controllerName; - if ($controllerName != $defaultController) { - // Create controller class name - $className = 'Console' . $this->convertToClassName($controllerName) . 'Controller'; - - // ... and set it - $this->setClassName($className); - } else { - // No news at main command or non-news command - $this->setClassName('ConsoleDefaultNewsController'); - } - //* DEBUG: */ echo ", controller=".$this->getClassName()."
\n"; - - // Is this class loaded? - if (!class_exists($this->getClassName())) { - // Throw an exception here - throw new InvalidControllerException(array($this, $controllerName), self::EXCEPTION_INVALID_CONTROLLER); - } // END - if - - // Set default resolver config name - $resolverConfigEntry = ''; - - // Try to read a config entry for our resolver including controller name... ;-) - try { - // Create the resolver name - $resolverConfigEntry = sprintf("console_cmd_%s_resolver_class", strtolower($controllerName)); - - // Get the config, this will throw an exception if there is no special command resolver - $resolverClass = $this->getConfigInstance()->readConfig($resolverConfigEntry); - } catch (ConfigEntryNotFoundException $e) { - // Use default resolver entry - // @TODO Maybe we need to log this? - $resolverConfigEntry = 'console_cmd_resolver_class'; - } - - // Initiate the resolver and controller - $resolverInstance = ObjectFactory::createObjectByConfiguredName( - $resolverConfigEntry, - array( - $controllerName, - $this->getApplicationInstance() - ) - ); - $controllerInstance = ObjectFactory::createObjectByName( - $this->getClassName(), - array($resolverInstance) - ); - - // Return the result - return $controllerInstance; - } } // [EOF] diff --git a/inc/classes/main/resolver/controller/image/class_ImageControllerResolver.php b/inc/classes/main/resolver/controller/image/class_ImageControllerResolver.php index a8b136aa..fea3a826 100644 --- a/inc/classes/main/resolver/controller/image/class_ImageControllerResolver.php +++ b/inc/classes/main/resolver/controller/image/class_ImageControllerResolver.php @@ -108,60 +108,6 @@ class ImageControllerResolver extends BaseControllerResolver implements Controll // Return the maybe resolved instance return $controllerInstance; } - - /** - * "Loads" a given controller and instances it if not yet cached. If the - * controller was not found one of the default controllers will be used - * depending on wether news shall be displayed. - * - * @param $controllerName A controller name we shall look for - * @return $controllerInstance A loaded controller instance - * @throws InvalidControllerException Thrown if even the requested - * controller class is missing (bad!) - */ - private function loadController ($controllerName) { - // Cache default command - $defaultController = $this->getConfigInstance()->readConfig('default_image_command'); - - // Init controller instance - $controllerInstance = null; - - // Default controller - $this->setClassName($defaultController); - - // Generate the class name - //* DEBUG: */ echo __METHOD__.": Controller=".$controllerName; - if ($controllerName != $defaultController) { - // Create controller class name - $className = 'Image' . $this->convertToClassName($controllerName) . 'Controller'; - - // ... and set it - $this->setClassName($className); - } // END - if - //* DEBUG: */ echo ", controller=".$this->getClassName()."
\n"; - - // Is this class loaded? - if (!class_exists($this->getClassName())) { - // Class not found, so or throw an exception - throw new InvalidControllerException(array($this, $controllerName), self::EXCEPTION_INVALID_CONTROLLER); - } // END - if - - // Initiate the resolver and controller - $resolverInstance = ObjectFactory::createObjectByConfiguredName( - 'image_cmd_resolver_class', - array( - $controllerName, - $this->getApplicationInstance() - ) - ); - $controllerInstance = ObjectFactory::createObjectByName( - $this->getClassName(), - array($resolverInstance) - ); - - // Return the result - return $controllerInstance; - } } // [EOF] diff --git a/inc/classes/main/resolver/controller/web/class_WebControllerResolver.php b/inc/classes/main/resolver/controller/web/class_WebControllerResolver.php index a941f53c..b43274d3 100644 --- a/inc/classes/main/resolver/controller/web/class_WebControllerResolver.php +++ b/inc/classes/main/resolver/controller/web/class_WebControllerResolver.php @@ -108,79 +108,6 @@ class WebControllerResolver extends BaseControllerResolver implements Controller // Return the maybe resolved instance return $controllerInstance; } - - /** - * "Loads" a given controller and instances it if not yet cached. If the - * controller was not found one of the default controllers will be used - * depending on wether news shall be displayed. - * - * @param $controllerName A controller name we shall look for - * @return $controllerInstance A loaded controller instance - * @throws InvalidControllerException Thrown if even the requested - * controller class is missing (bad!) - */ - private function loadController ($controllerName) { - // Cache default command - $defaultController = $this->getConfigInstance()->readConfig('default_web_command'); - - // Init controller instance - $controllerInstance = null; - - // Default controller - $this->setClassName('WebDefaultNewsController'); - - // Generate the class name - //* DEBUG: */ echo __METHOD__.": Controller=".$controllerName; - if ($controllerName != $defaultController) { - // Create controller class name - $className = 'Web' . $this->convertToClassName($controllerName) . 'Controller'; - - // ... and set it - $this->setClassName($className); - } else { - // No news at home page or non-news page - $this->setClassName('WebDefaultNewsController'); - } - //* DEBUG: */ echo ", controller=".$this->getClassName()."
\n"; - - // Is this class loaded? - if (!class_exists($this->getClassName())) { - // Throw an exception here - throw new InvalidControllerException(array($this, $controllerName), self::EXCEPTION_INVALID_CONTROLLER); - } // END - if - - // Set default resolver config name - $resolverConfigEntry = ''; - - // Try to read a config entry for our resolver including controller name... ;-) - try { - // Create the resolver name - $resolverConfigEntry = sprintf("web_cmd_%s_resolver_class", strtolower($controllerName)); - - // Get the config, this will throw an exception if there is no special command resolver - $resolverClass = $this->getConfigInstance()->readConfig($resolverConfigEntry); - } catch (ConfigEntryNotFoundException $e) { - // Use default resolver entry - // @TODO Maybe we need to log this? - $resolverConfigEntry = 'web_cmd_resolver_class'; - } - - // Initiate the resolver and controller - $resolverInstance = ObjectFactory::createObjectByConfiguredName( - $resolverConfigEntry, - array( - $controllerName, - $this->getApplicationInstance() - ) - ); - $controllerInstance = ObjectFactory::createObjectByName( - $this->getClassName(), - array($resolverInstance) - ); - - // Return the result - return $controllerInstance; - } } // [EOF] -- 2.39.5