3 namespace Org\Mxchange\City\Command;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Command\BaseCommand;
7 use Org\Mxchange\CoreFramework\Command\Commandable;
8 use Org\Mxchange\CoreFramework\Controller\Controller;
9 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
10 use Org\Mxchange\CoreFramework\Request\Requestable;
11 use Org\Mxchange\CoreFramework\Resolver\Command\CommandResolver;
12 use Org\Mxchange\CoreFramework\Response\Responseable;
15 * A command for the 'daemon' routine
17 * @author Roland Haeder <webmaster@shipsimu.org>
19 * @copyright Copyright (c) 2015, 2016 City Developer Team
20 * @license GNU GPL 3.0 or any newer version
21 * @link http://www.shipsimu.org
23 * This program is free software: you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation, either version 3 of the License, or
26 * (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
33 * You should have received a copy of the GNU General Public License
34 * along with this program. If not, see <http://www.gnu.org/licenses/>.
36 class CityConsoleDaemonCommand extends BaseCommand implements Commandable {
38 * Protected constructor
42 protected function __construct () {
43 // Call parent constructor
44 parent::__construct(__CLASS__);
48 * Creates an instance of this class
50 * @param $resolverInstance An instance of a command resolver class
51 * @return $commandInstance An instance a prepared command class
53 public static final function createCityConsoleDaemonCommand (CommandResolver $resolverInstance) {
55 $commandInstance = new CityConsoleDaemonCommand();
57 // Set the application instance
58 $commandInstance->setResolverInstance($resolverInstance);
60 // Return the prepared instance
61 return $commandInstance;
65 * Executes the given command with given request and response objects
67 * @param $requestInstance An instance of a class with an Requestable interface
68 * @param $responseInstance An instance of a class with an Responseable interface
72 public function execute (Requestable $requestInstance, Responseable $responseInstance) {
73 // Get a registry and the application instance from it
74 $applicationInstance = Registry::getRegistry()->getInstance('app');
77 * ----------------------- Bootstrapping phase ------------------------
78 * Try to bootstrap the city and pass the request instance to it for
79 * extra arguments which mostly override config entries or enable special
80 * features within the hub (none is ready at this development stage)
82 self::createDebugInstance(__CLASS__)->debugOutput('BOOTSTRAP: Beginning with bootstrap...');
83 $applicationInstance->getControllerInstance()->executeBootstrapFilters($requestInstance, $responseInstance);
84 self::createDebugInstance(__CLASS__)->debugOutput('BOOTSTRAP: Bootstrap finished.');
87 $cityInstance = CityDaemonFactory::createCityDaemonInstance();
89 // Add some city-specific filters, e.g. announcement
90 $cityInstance->addExtraCityFilters();
93 * -------------------------- City activation --------------------------
94 * Activates the city daemon by doing some final preparation steps and
95 * setting the attribute $cityIsActive to TRUE.
97 $cityInstance->activateCityDaemon($requestInstance, $responseInstance);
99 // Get task handler instance
100 $handlerInstance = Registry::getRegistry()->getInstance('task_handler');
103 self::createDebugInstance(__CLASS__)->debugOutput('MAIN: --- Entering main loop. ---');
106 * ----------------------------- Main loop ----------------------------
107 * This is the main loop. Queried calls should come back here very fast
108 * so the whole application runs on nice speed. This while-loop goes
109 * until the application is no longer active or all tasks are killed.
111 while (($cityInstance->isCityActive()) && ($handlerInstance->hasTasksLeft())) {
112 // Handle all tasks here
113 $handlerInstance->handleTasks();
117 self::createDebugInstance(__CLASS__)->debugOutput('MAIN: --- Leaving main loop. ---');
121 * Adds extra filters to the given controller instance
123 * @param $controllerInstance A controller instance
124 * @param $requestInstance An instance of a class with an Requestable interface
128 public function addExtraFilters (Controller $controllerInstance, Requestable $requestInstance) {
130 $controllerInstance->addPreFilter(ObjectFactory::createObjectByConfiguredName('city_daemon_php_requirements_filter'));
131 $controllerInstance->addPreFilter(ObjectFactory::createObjectByConfiguredName('city_daemon_initializer_filter'));
132 $controllerInstance->addPreFilter(ObjectFactory::createObjectByConfiguredName('city_daemon_welcome_teaser_filter'));
134 // Add bootstrap filters
135 $controllerInstance->addBootstrapFilter(ObjectFactory::createObjectByConfiguredName('city_daemon_bootstrap_init_daemon_filter'));
136 $controllerInstance->addBootstrapFilter(ObjectFactory::createObjectByConfiguredName('city_daemon_bootstrap_extra_bootstrapping_filter'));
138 // Add city activation filters
139 $controllerInstance->addActivationFilter(ObjectFactory::createObjectByConfiguredName('city_daemon_activation_task_handler_initializer_filter'));
141 // Add shutdown filters, you may want to shutdown the task handler as last one.
142 $controllerInstance->addShutdownFilter(ObjectFactory::createObjectByConfiguredName('city_daemon_shutdown_task_handler_filter'));
144 // This is the last generic shutdown filter
145 $controllerInstance->addShutdownFilter(ObjectFactory::createObjectByConfiguredName('city_daemon_shutdown_city_filter'));