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