From 1b71297cbba86eb3ad32406ecc81ba445b9aac38 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sun, 19 Jul 2009 17:44:45 +0000 Subject: [PATCH] Now protected for other filter chain classes --- .gitattributes | 14 +- application/hub/class_ApplicationHelper.php | 10 +- application/hub/config.php | 56 +++--- ...nd.php => class_HubConsoleMainCommand.php} | 13 +- application/hub/main/controller/.htaccess | 1 + .../hub/main/controller/console/.htaccess | 1 + .../class_HubConsoleDefaultNewsController.php | 109 +++++++++++ .../bootstrap/{class_Bootstrap => class_} | 8 +- application/hub/main/resolver/.htaccess | 1 + .../hub/main/resolver/command/.htaccess | 1 + .../main/resolver/command/console/.htaccess | 1 + .../class_HubConsoleCommandResolver.php | 176 ++++++++++++++++++ .../hub/main/resolver/controller/.htaccess | 1 + .../resolver/controller/console/.htaccess | 1 + .../class_HubConsoleControllerResolver.php | 114 ++++++++++++ 15 files changed, 464 insertions(+), 43 deletions(-) rename application/hub/main/commands/console/{class_ConsoleMainCommand.php => class_HubConsoleMainCommand.php} (78%) create mode 100644 application/hub/main/controller/.htaccess create mode 100644 application/hub/main/controller/console/.htaccess create mode 100644 application/hub/main/controller/console/class_HubConsoleDefaultNewsController.php rename application/hub/main/filter/bootstrap/{class_Bootstrap => class_} (87%) create mode 100644 application/hub/main/resolver/.htaccess create mode 100644 application/hub/main/resolver/command/.htaccess create mode 100644 application/hub/main/resolver/command/console/.htaccess create mode 100644 application/hub/main/resolver/command/console/class_HubConsoleCommandResolver.php create mode 100644 application/hub/main/resolver/controller/.htaccess create mode 100644 application/hub/main/resolver/controller/console/.htaccess create mode 100644 application/hub/main/resolver/controller/console/class_HubConsoleControllerResolver.php diff --git a/.gitattributes b/.gitattributes index 35f40e25b..390bf8e40 100644 --- a/.gitattributes +++ b/.gitattributes @@ -38,7 +38,7 @@ application/hub/main/class_Base -text application/hub/main/class_BaseHubSystem.php -text application/hub/main/commands/.htaccess -text application/hub/main/commands/console/.htaccess -text -application/hub/main/commands/console/class_ConsoleMainCommand.php -text +application/hub/main/commands/console/class_HubConsoleMainCommand.php -text application/hub/main/connectors/.htaccess -text application/hub/main/connectors/class_BaseConnector.php -text application/hub/main/connectors/query/.htaccess -text @@ -51,12 +51,15 @@ application/hub/main/connectors/queues/class_ -text application/hub/main/connectors/queues/class_BaseQueueConnector.php -text application/hub/main/connectors/queues/local/.htaccess -text application/hub/main/connectors/queues/local/class_LocalQueueConnector.php -text +application/hub/main/controller/.htaccess -text +application/hub/main/controller/console/.htaccess -text +application/hub/main/controller/console/class_HubConsoleDefaultNewsController.php -text application/hub/main/database/.htaccess -text application/hub/main/database/wrapper/.htaccess -text application/hub/main/database/wrapper/class_NodeInformationDatabaseWrapper.php -text application/hub/main/filter/.htaccess -text application/hub/main/filter/bootstrap/.htaccess -text -application/hub/main/filter/bootstrap/class_Bootstrap -text +application/hub/main/filter/bootstrap/class_ -text application/hub/main/filter/console/.htaccess -text application/hub/main/filter/console/class_Console -text application/hub/main/filter/console/class_ConsoleWelcomeTeaserFilter.php -text @@ -102,6 +105,13 @@ application/hub/main/queues/class_ -text application/hub/main/queues/class_BaseQueue.php -text application/hub/main/queues/client/.htaccess -text application/hub/main/queues/client/class_LocalClientQueue.php -text +application/hub/main/resolver/.htaccess -text +application/hub/main/resolver/command/.htaccess -text +application/hub/main/resolver/command/console/.htaccess -text +application/hub/main/resolver/command/console/class_HubConsoleCommandResolver.php -text +application/hub/main/resolver/controller/.htaccess -text +application/hub/main/resolver/controller/console/.htaccess -text +application/hub/main/resolver/controller/console/class_HubConsoleControllerResolver.php -text application/hub/main/states/.htaccess -text application/hub/main/states/class_ -text application/hub/main/states/class_BaseState.php -text diff --git a/application/hub/class_ApplicationHelper.php b/application/hub/class_ApplicationHelper.php index 0d75b689e..7a78ebb17 100644 --- a/application/hub/class_ApplicationHelper.php +++ b/application/hub/class_ApplicationHelper.php @@ -178,7 +178,7 @@ class ApplicationHelper extends BaseFrameworkSystem implements ManageableApplica $this->setResponseInstance($responseInstance); // Get the parameter from the request - $commandName = $requestInstance->getRequestElement('page'); + $commandName = $requestInstance->getRequestElement('command'); // If it is null then get default command if (is_null($commandName)) { @@ -190,7 +190,7 @@ class ApplicationHelper extends BaseFrameworkSystem implements ManageableApplica } // END - if // Get a resolver - $resolverClass = sprintf("%sControllerResolver", $this->convertToClassName($responseType)); + $resolverClass = sprintf("Hub%sControllerResolver", $this->convertToClassName($responseType)); $resolverInstance = ObjectFactory::createObjectByName($resolverClass, array($commandName, $this)); // Get a controller instance as well @@ -206,12 +206,6 @@ class ApplicationHelper extends BaseFrameworkSystem implements ManageableApplica $this->controllerInstance->handleRequest($requestInstance, $responseInstance); die("STOP\n"); - // ----------------------- Output teaser lines ------------------------ - // Output some introducing lines to the console. This should be later - // be switched off if this is a productive release because it would be - // only visible to the logfile. - $nodeInstance->outputConsoleTeaser(); - // ----------------------- Bootstrapping phase ------------------------ // Try to bootstrap the node and pass the request instance to it for // extra arguments which mostly override config entries or enable special diff --git a/application/hub/config.php b/application/hub/config.php index 30830bb41..48027b29b 100644 --- a/application/hub/config.php +++ b/application/hub/config.php @@ -25,7 +25,7 @@ $cfg = FrameworkConfiguration::getInstance(); // CFG: NODE-LISTEN-ADDR -$cfg->setConfigEntry('node_listen_addr', "0.0.0.0"); +$cfg->setConfigEntry('node_listen_addr', '0.0.0.0'); // CFG: NODE-TCP-LISTEN-PORT $cfg->setConfigEntry('node_tcp_listen_port', 9060); @@ -34,73 +34,79 @@ $cfg->setConfigEntry('node_tcp_listen_port', 9060); $cfg->setConfigEntry('node_udp_listen_port', 9060); // CFG: NODE-MODE (can be 'regular', 'list', 'master' or 'boot', default is 'regular') -$cfg->setConfigEntry('node_mode', "regular"); +$cfg->setConfigEntry('node_mode', 'regular'); // CFG: TEMPLATE-ENGINE -$cfg->setConfigEntry('tpl_engine', "ConsoleOutput"); +$cfg->setConfigEntry('tpl_engine', 'ConsoleOutput'); // CFG: OUTPUT-CLASS -$cfg->setConfigEntry('output_class', "DebugConsoleOutput"); +$cfg->setConfigEntry('output_class', 'DebugConsoleOutput'); // CFG: DEBUG-CLASS -$cfg->setConfigEntry('debug_class', "DebugConsoleOutput"); +$cfg->setConfigEntry('debug_class', 'DebugConsoleOutput'); // CFG: NODE-INFO-DB-WRAPPER-CLASS -$cfg->setConfigEntry('node_info_db_wrapper_class', "NodeInformationDatabaseWrapper"); +$cfg->setConfigEntry('node_info_db_wrapper_class', 'NodeInformationDatabaseWrapper'); // CFG: WEB-CONTENT-TYPE -$cfg->setConfigEntry('web_content_type', ""); +$cfg->setConfigEntry('web_content_type', ''); // CFG: QUERY-CONNECTOR-CLASS -$cfg->setConfigEntry('query_connector_class', "LocalQueryConnector"); +$cfg->setConfigEntry('query_connector_class', 'LocalQueryConnector'); // CFG: QUEUE-CONNECTOR-CLASS -$cfg->setConfigEntry('queue_connector_class', "LocalQueueConnector"); +$cfg->setConfigEntry('queue_connector_class', 'LocalQueueConnector'); // CFG: LISTENER-POOL-CLAS -$cfg->setConfigEntry('listener_pool_class', "DefaultListenerPool"); +$cfg->setConfigEntry('listener_pool_class', 'DefaultListenerPool'); // CFG: TCP-LISTENER-CLASS -$cfg->setConfigEntry('tcp_listener_class', "TcpListener"); +$cfg->setConfigEntry('tcp_listener_class', 'TcpListener'); // CFG: UDP-LISTENER-CLASS -$cfg->setConfigEntry('udp_listener_class', "UdpListener"); +$cfg->setConfigEntry('udp_listener_class', 'UdpListener'); // CFG: HUB-TCP-LISTENER-CLASS -$cfg->setConfigEntry('hub_tcp_listener_class', "HubTcpListenerDecorator"); +$cfg->setConfigEntry('hub_tcp_listener_class', 'HubTcpListenerDecorator'); // CFG: HUB-UDP-LISTENER-CLASS -$cfg->setConfigEntry('hub_udp_listener_class', "HubUdpListenerDecorator"); +$cfg->setConfigEntry('hub_udp_listener_class', 'HubUdpListenerDecorator'); // CFG: CLIENT-TCP-LISTENER-CLASS -$cfg->setConfigEntry('client_tcp_listener_class', "ClientTcpListenerDecorator"); +$cfg->setConfigEntry('client_tcp_listener_class', 'ClientTcpListenerDecorator'); // CFG: CLIENT-UDP-LISTENER-CLASS -$cfg->setConfigEntry('client_udp_listener_class', "ClientUdpListenerDecorator"); +$cfg->setConfigEntry('client_udp_listener_class', 'ClientUdpListenerDecorator'); // CFG: CLIENT-POOL-CLASS -$cfg->setConfigEntry('client_pool_class', "DefaultClientPool"); +$cfg->setConfigEntry('client_pool_class', 'DefaultClientPool'); // CFG: DEFAULT-CONSOLE-COMMAND -$cfg->setConfigEntry('default_console_command', "main"); +$cfg->setConfigEntry('default_console_command', 'main'); -// CFG: CONSOLE-CMD-RESOLVER-CLASS -$cfg->setConfigEntry('console_cmd_resolver_class', "ConsoleCommandResolver"); +// CFG: DEFAULT-HUBCONSOLE-COMMAND +$cfg->setConfigEntry('default_hubconsole_command', 'main'); + +// CFG: HUBCONSOLE-CMD-RESOLVER-CLASS +$cfg->setConfigEntry('hubconsole_cmd_resolver_class', 'HubConsoleCommandResolver'); // CFG: NEWS-DOWNLOAD-FILTER -$cfg->setConfigEntry('news_download_filter', "NewsDownloadFilter"); +$cfg->setConfigEntry('news_download_filter', 'NewsDownloadFilter'); // CFG: NEWS-PROCESS-FILTER -$cfg->setConfigEntry('news_process_filter', "NewsProcessFilter"); +$cfg->setConfigEntry('news_process_filter', 'NewsProcessFilter'); // CFG: NODE-INITIALIZATION-FILTER -$cfg->setConfigEntry('node_initializer_filter', "NodeInitializationFilter"); +$cfg->setConfigEntry('node_initializer_filter', 'NodeInitializationFilter'); // CFG: CONSOLE-WELCOME-TEASER-FILTER -$cfg->setConfigEntry('console_welcome_teaser_filter', "ConsoleWelcomeTeaserFilter"); +$cfg->setConfigEntry('console_welcome_teaser_filter', 'ConsoleWelcomeTeaserFilter'); + +// CFG: HUB-BOOTSTRAP-AQUIRE-HUBID-FILTER +$cfg->setConfigEntry('hub_bootstrap_aquire_hubid_filter', 'HubBootstrapAquireHubIdFilter'); // CFG: NEWS-READER-CLASS -$cfg->setConfigEntry('news_reader_class', "ConsoleNewsReader"); +$cfg->setConfigEntry('news_reader_class', 'ConsoleNewsReader'); // CFG: NEWS-MAIN-LIMIT $cfg->setConfigEntry('news_main_limit', 5); diff --git a/application/hub/main/commands/console/class_ConsoleMainCommand.php b/application/hub/main/commands/console/class_HubConsoleMainCommand.php similarity index 78% rename from application/hub/main/commands/console/class_ConsoleMainCommand.php rename to application/hub/main/commands/console/class_HubConsoleMainCommand.php index be0cbd8f3..d89548d8e 100644 --- a/application/hub/main/commands/console/class_ConsoleMainCommand.php +++ b/application/hub/main/commands/console/class_HubConsoleMainCommand.php @@ -21,7 +21,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -class ConsoleMainCommand extends BaseCommand implements Commandable { +class HubConsoleMainCommand extends BaseCommand implements Commandable { /** * Protected constructor * @@ -38,9 +38,9 @@ class ConsoleMainCommand extends BaseCommand implements Commandable { * @param $resolverInstance An instance of a command resolver class * @return $commandInstance An instance a prepared command class */ - public final static function createConsoleMainCommand (CommandResolver $resolverInstance) { + public final static function createHubConsoleMainCommand (CommandResolver $resolverInstance) { // Get new instance - $commandInstance = new ConsoleMainCommand(); + $commandInstance = new HubConsoleMainCommand(); // Set the application instance $commandInstance->setResolverInstance($resolverInstance); @@ -69,9 +69,14 @@ class ConsoleMainCommand extends BaseCommand implements Commandable { * @return void */ public function addExtraFilters (Controller $controllerInstance, Requestable $requestInstance) { - // Add filters + // Add pre filters $controllerInstance->addPreFilter(ObjectFactory::createObjectByConfiguredName('node_initializer_filter')); $controllerInstance->addPreFilter(ObjectFactory::createObjectByConfiguredName('console_welcome_teaser_filter')); + + // Add bootstrap filters + $controllerInstance->addBootstrapFilter(ObjectFactory::createObjectByConfiguredName('hub_bootstrap_aquire_hubid_filter')); + $controllerInstance->addBootstrapFilter(ObjectFactory::createObjectByConfiguredName('hub_bootstrap_gen_sessionid_filter')); + $controllerInstance->addBootstrapFilter(ObjectFactory::createObjectByConfiguredName('hub_bootstrap_restore_nodelist_filter')); } } diff --git a/application/hub/main/controller/.htaccess b/application/hub/main/controller/.htaccess new file mode 100644 index 000000000..3a4288278 --- /dev/null +++ b/application/hub/main/controller/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/application/hub/main/controller/console/.htaccess b/application/hub/main/controller/console/.htaccess new file mode 100644 index 000000000..3a4288278 --- /dev/null +++ b/application/hub/main/controller/console/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/application/hub/main/controller/console/class_HubConsoleDefaultNewsController.php b/application/hub/main/controller/console/class_HubConsoleDefaultNewsController.php new file mode 100644 index 000000000..8da9f3929 --- /dev/null +++ b/application/hub/main/controller/console/class_HubConsoleDefaultNewsController.php @@ -0,0 +1,109 @@ + + * @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 HubConsoleDefaultNewsController extends BaseController implements Controller { + /** + * Protected constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + } + + /** + * Creates an instance of this class + * + * @param $resolverInstance An instance of a command resolver class + * @return $controllerInstance A prepared instance of this class + */ + public final static function createHubConsoleDefaultNewsController (CommandResolver $resolverInstance) { + // Create the instance + $controllerInstance = new HubConsoleDefaultNewsController(); + + // Set the command resolver + $controllerInstance->setResolverInstance($resolverInstance); + + // Add news filters to this controller + $controllerInstance->addPreFilter(ObjectFactory::createObjectByConfiguredName('news_download_filter')); + $controllerInstance->addPreFilter(ObjectFactory::createObjectByConfiguredName('news_process_filter')); + + // Return the prepared instance + return $controllerInstance; + } + + /** + * Handles the given request and response + * + * @param $requestInstance An instance of a request class + * @param $responseInstance An instance of a response class + * @return void + */ + public function handleRequest (Requestable $requestInstance, Responseable $responseInstance) { + // Get the command instance from the resolver by sending a request instance to the resolver + $commandInstance = $this->getResolverInstance()->resolveCommandByRequest($requestInstance); + + // Add more filters by the command + $commandInstance->addExtraFilters($this, $requestInstance); + + // Run the pre filters + $this->executePreFilters($requestInstance, $responseInstance); + + // This request was valid! :-D + $requestInstance->requestIsValid(); + + // Execute the command + $commandInstance->execute($requestInstance, $responseInstance); + + // Run the pre filters + $this->executePostFilters($requestInstance, $responseInstance); + + // Flush the response out + $responseInstance->flushBuffer(); + } + + /** + * Add a bootstrap filter + * + * @param $filterInstance A Filterable class + * @return void + */ + public function addBootstrapFilter (Filterable $filterInstance) { + $this->addFilter('bootstrap', $filterInstance); + } + + /** + * Executes all bootstrap filters + * + * @param $requestInstance A Requestable class + * @param $responseInstance A Responseable class + * @return void + */ + public function executeBootstrapFilters (Requestable $requestInstance, Responseable $responseInstance) { + $this->executeFilters('bootstrap', $requestInstance, $responseInstance); + } +} + +// [EOF] +?> diff --git a/application/hub/main/filter/bootstrap/class_Bootstrap b/application/hub/main/filter/bootstrap/class_ similarity index 87% rename from application/hub/main/filter/bootstrap/class_Bootstrap rename to application/hub/main/filter/bootstrap/class_ index 9e509f68d..58af89dea 100644 --- a/application/hub/main/filter/bootstrap/class_Bootstrap +++ b/application/hub/main/filter/bootstrap/class_ @@ -21,7 +21,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -class ???BootstrapFilter extends BaseFilter implements Filterable { +class HubBootstrap???Filter extends BaseFilter implements Filterable { /** * Protected constructor * @@ -37,9 +37,9 @@ class ???BootstrapFilter extends BaseFilter implements Filterable { * * @return $filterInstance An instance of this filter class */ - public final static function create???BootstrapFilter () { + public final static function createHubBootstrap???Filter () { // Get a new instance - $filterInstance = new ???BootstrapFilter(); + $filterInstance = new HubBootstrap???Filter(); // Return the instance return $filterInstance; @@ -55,7 +55,7 @@ class ???BootstrapFilter extends BaseFilter implements Filterable { */ public function execute (Requestable $requestInstance, Responseable $responseInstance) { // Implement this! - $this->partialStub("Please implement this method."); + $this->partialStub('Please implement this method.'); } } diff --git a/application/hub/main/resolver/.htaccess b/application/hub/main/resolver/.htaccess new file mode 100644 index 000000000..3a4288278 --- /dev/null +++ b/application/hub/main/resolver/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/application/hub/main/resolver/command/.htaccess b/application/hub/main/resolver/command/.htaccess new file mode 100644 index 000000000..3a4288278 --- /dev/null +++ b/application/hub/main/resolver/command/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/application/hub/main/resolver/command/console/.htaccess b/application/hub/main/resolver/command/console/.htaccess new file mode 100644 index 000000000..3a4288278 --- /dev/null +++ b/application/hub/main/resolver/command/console/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/application/hub/main/resolver/command/console/class_HubConsoleCommandResolver.php b/application/hub/main/resolver/command/console/class_HubConsoleCommandResolver.php new file mode 100644 index 000000000..342d1f76c --- /dev/null +++ b/application/hub/main/resolver/command/console/class_HubConsoleCommandResolver.php @@ -0,0 +1,176 @@ + + * @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 HubConsoleCommandResolver extends BaseCommandResolver implements CommandResolver { + /** + * Last successfull resolved command + */ + private $lastCommandInstance = null; + + /** + * Protected constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + + // Set prefix to "HubConsole" + $this->setCommandPrefix("HubConsole"); + } + + /** + * Creates an instance of a HubConsole command resolver with a given default command + * + * @param $commandName The default command we shall execute + * @param $appInstance An instance of a manageable application helper class + * @return $resolverInstance The prepared command resolver instance + * @throws EmptyVariableException Thrown if default command is not set + * @throws InvalidCommandException Thrown if default command is invalid + */ + public final static function createHubConsoleCommandResolver ($commandName, ManageableApplication $appInstance) { + // Create the new instance + $resolverInstance = new HubConsoleCommandResolver(); + + // Is the variable $commandName set and the command is valid? + if (empty($commandName)) { + // Then thrown an exception here + throw new EmptyVariableException(array($resolverInstance, 'commandName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING); + } elseif ($resolverInstance->isCommandValid($commandName) === false) { + // Invalid command found + throw new InvalidCommandException(array($resolverInstance, $commandName), self::EXCEPTION_INVALID_COMMAND); + } + + // Set the application instance + $resolverInstance->setApplicationInstance($appInstance); + + // Return the prepared instance + return $resolverInstance; + } + + /** + * Returns an command instance for a given request class or null if + * it was not found + * + * @param $requestInstance An instance of a request class + * @return $commandInstance An instance of the resolved command + * @throws InvalidCommandException Thrown if $commandName is + * invalid + * @throws InvalidCommandInstanceException Thrown if $commandInstance + * is an invalid instance + */ + public function resolveCommandByRequest (Requestable $requestInstance) { + // Init variables + $commandName = ''; + $commandInstance = null; + + // This goes fine so let's resolve the command + $commandName = $requestInstance->getRequestElement('command'); + + // Is the command empty? Then fall back to default command + if (empty($commandName)) $commandName = $this->getConfigInstance()->readConfig('default_console_command'); + + // Check if command is valid + if ($this->isCommandValid($commandName) === false) { + // This command is invalid! + throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND); + } // END - if + + // Get the command + $commandInstance = $this->loadCommand($commandName); + + // And validate it + if ((!is_object($commandInstance)) || (!$commandInstance instanceof Commandable)) { + // This command has an invalid instance! + throw new InvalidCommandInstanceException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND); + } // END - if + + // Set last command + $this->lastCommandInstance = $commandInstance; + + // Return the resolved command instance + return $commandInstance; + } + + /** + * Resolves the command by its direct name and returns an instance of its class + * + * @param $commandName The direct command name we shall resolve + * @return $commandInstance An instance of the command class + * @throws InvalidCommandException Thrown if $commandName is invalid + */ + public function resolveCommand ($commandName) { + // Initiate the instance variable + $commandInstance = null; + + // Is the command empty? Then fall back to default command + if (empty($commandName)) $commandName = $this->getConfigInstance()->readConfig('default_console_command'); + + // Check if command is valid + if ($this->isCommandValid($commandName) === false) { + // This command is invalid! + throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND); + } + + // Get the command + $commandInstance = $this->loadCommand($commandName); + + // Return the instance + return $commandInstance; + } + + /** + * "Loads" a given command and instances it if not yet cached + * + * @param $commandName A command name we shall look for + * @return $commandInstance A loaded command instance + * @throws InvalidCommandException Thrown if even the default + * command class is missing (bad!) + */ + private function loadCommand ($commandName) { + // Init command instance + $commandInstance = null; + + // Create class name + $className = 'HubConsole' . $this->convertToClassName($commandName) . 'Command'; + + // Create command class name + $this->setClassName($className); + + // Is this class loaded? + if (!class_exists($this->getClassName())) { + // Class not found, so throw an exception + throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND); + } // END - if + + // Initiate the command + $commandInstance = ObjectFactory::createObjectByName($this->getClassName(), array($this)); + + // Return the result + return $commandInstance; + } +} + +// [EOF] +?> diff --git a/application/hub/main/resolver/controller/.htaccess b/application/hub/main/resolver/controller/.htaccess new file mode 100644 index 000000000..3a4288278 --- /dev/null +++ b/application/hub/main/resolver/controller/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/application/hub/main/resolver/controller/console/.htaccess b/application/hub/main/resolver/controller/console/.htaccess new file mode 100644 index 000000000..3a4288278 --- /dev/null +++ b/application/hub/main/resolver/controller/console/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/application/hub/main/resolver/controller/console/class_HubConsoleControllerResolver.php b/application/hub/main/resolver/controller/console/class_HubConsoleControllerResolver.php new file mode 100644 index 000000000..7bb04a909 --- /dev/null +++ b/application/hub/main/resolver/controller/console/class_HubConsoleControllerResolver.php @@ -0,0 +1,114 @@ + + * @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 HubConsoleControllerResolver extends BaseControllerResolver implements ControllerResolver { + /** + * Last successfull resolved controller (name) + */ + private $lastControllerName = ''; + + /** + * Last successfull resolved controller (instance) + */ + private $lastControllerInstance = null; + + /** + * Protected constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + + // Set prefix to "HubConsole" + $this->setControllerPrefix('HubConsole'); + } + + /** + * Creates an instance of a resolver class with a given command + * + * @param $controllerName The controller we shall resolve + * @param $appInstance An instance of a manageable application helper class + * @return $resolverInstance The prepared controller resolver instance + * @throws EmptyVariableException Thrown if default command is not set + * @throws InvalidControllerException Thrown if default controller is invalid + */ + public final static function createHubConsoleControllerResolver ($controllerName, ManageableApplication $appInstance) { + // Create the new instance + $resolverInstance = new HubConsoleControllerResolver(); + + // Is the variable $controllerName set and the command is valid? + if (empty($controllerName)) { + // Then thrown an exception here + throw new EmptyVariableException(array($resolverInstance, 'commandName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING); + } elseif ($resolverInstance->isControllerValid($controllerName) === false) { + // Invalid command found + throw new InvalidControllerException(array($resolverInstance, $controllerName), self::EXCEPTION_INVALID_CONTROLLER); + } + + // Set the application instance + $resolverInstance->setApplicationInstance($appInstance); + + // Set command name + $resolverInstance->setControllerName($controllerName); + + // Return the prepared instance + return $resolverInstance; + } + + /** + * Resolves the default controller of the given command + * + * @return $controllerInstance A controller instance for the default + * command + * @throws InvalidControllerInstanceException Thrown if $controllerInstance + * is invalid + */ + public function resolveController () { + // Init variables + $controllerName = ''; + $controllerInstance = null; + + // Get the command name + $controllerName = $this->getControllerName(); + + // Get the command + $controllerInstance = $this->loadController($controllerName); + + // And validate it + if ((!is_object($controllerInstance)) || (!$controllerInstance instanceof Controller)) { + // This command has an invalid instance! + throw new InvalidControllerInstanceException(array($this, $controllerName), self::EXCEPTION_INVALID_CONTROLLER); + } // END - if + + // Set last controller + $this->lastControllerInstance = $controllerInstance; + + // Return the maybe resolved instance + return $controllerInstance; + } +} + +// [EOF] +?> -- 2.39.5