3 namespace Org\Mxchange\City\Command;
5 // Import application-specific stuff
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\Helper\Application\ApplicationHelper;
14 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
15 use Org\Mxchange\CoreFramework\Request\Requestable;
16 use Org\Mxchange\CoreFramework\Resolver\Command\CommandResolver;
17 use Org\Mxchange\CoreFramework\Response\Responseable;
20 * A command for the 'daemon' routine
22 * @author Roland Haeder <webmaster@shipsimu.org>
24 * @copyright Copyright (c) 2015, 2016 City Developer Team
25 * @license GNU GPL 3.0 or any newer version
26 * @link http://www.shipsimu.org
28 * This program is free software: you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation, either version 3 of the License, or
31 * (at your option) any later version.
33 * This program is distributed in the hope that it will be useful,
34 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36 * GNU General Public License for more details.
38 * You should have received a copy of the GNU General Public License
39 * along with this program. If not, see <http://www.gnu.org/licenses/>.
41 class CityConsoleDaemonCommand extends BaseCommand implements Commandable {
43 * Protected constructor
47 protected function __construct () {
48 // Call parent constructor
49 parent::__construct(__CLASS__);
53 * Creates an instance of this class
55 * @param $resolverInstance An instance of a command resolver class
56 * @return $commandInstance An instance a prepared command class
58 public static final function createCityConsoleDaemonCommand (CommandResolver $resolverInstance) {
60 $commandInstance = new CityConsoleDaemonCommand();
62 // Set the application instance
63 $commandInstance->setResolverInstance($resolverInstance);
65 // Return the prepared instance
66 return $commandInstance;
70 * Executes the given command with given request and response objects
72 * @param $requestInstance An instance of a class with an Requestable interface
73 * @param $responseInstance An instance of a class with an Responseable interface
77 public function execute (Requestable $requestInstance, Responseable $responseInstance) {
78 // Get a registry and the application instance from it
79 $applicationInstance = ApplicationHelper::getSelfInstance();
82 * ----------------------- Bootstrapping phase ------------------------
83 * Try to bootstrap the city and pass the request instance to it for
84 * extra arguments which mostly override config entries or enable special
85 * features within the hub (none is ready at this development stage)
87 self::createDebugInstance(__CLASS__)->debugOutput('BOOTSTRAP: Beginning with bootstrap...');
88 $applicationInstance->getControllerInstance()->executeBootstrapFilters($requestInstance, $responseInstance);
89 self::createDebugInstance(__CLASS__)->debugOutput('BOOTSTRAP: Bootstrap finished.');
92 $cityInstance = CityDaemonFactory::createCityDaemonInstance();
94 // Add some city-specific filters, e.g. announcement
95 $cityInstance->addExtraCityFilters();
98 * -------------------------- City activation --------------------------
99 * Activates the city daemon by doing some final preparation steps and
100 * setting the attribute $cityIsActive to TRUE.
102 $cityInstance->activateCityDaemon($requestInstance, $responseInstance);
104 // Get task handler instance
105 $handlerInstance = GenericRegistry::getRegistry()->getInstance('task_handler');
108 self::createDebugInstance(__CLASS__)->debugOutput('MAIN: --- Entering main loop. ---');
111 * ----------------------------- Main loop ----------------------------
112 * This is the main loop. Queried calls should come back here very fast
113 * so the whole application runs on nice speed. This while-loop goes
114 * until the application is no longer active or all tasks are killed.
116 while (($cityInstance->isCityActive()) && ($handlerInstance->hasTasksLeft())) {
117 // Handle all tasks here
118 $handlerInstance->handleTasks();
122 self::createDebugInstance(__CLASS__)->debugOutput('MAIN: --- Leaving main loop. ---');
126 * Adds extra filters to the given controller instance
128 * @param $controllerInstance A controller instance
129 * @param $requestInstance An instance of a class with an Requestable interface
133 public function addExtraFilters (Controller $controllerInstance, Requestable $requestInstance) {
135 $controllerInstance->addPreFilter(ObjectFactory::createObjectByConfiguredName('city_daemon_php_requirements_filter_class'));
136 $controllerInstance->addPreFilter(ObjectFactory::createObjectByConfiguredName('city_daemon_initializer_filter_class'));
137 $controllerInstance->addPreFilter(ObjectFactory::createObjectByConfiguredName('city_daemon_welcome_teaser_filter_class'));
139 // Add bootstrap filters
140 $controllerInstance->addBootstrapFilter(ObjectFactory::createObjectByConfiguredName('city_daemon_bootstrap_init_daemon_filter_class'));
141 $controllerInstance->addBootstrapFilter(ObjectFactory::createObjectByConfiguredName('city_daemon_bootstrap_extra_bootstrapping_filter_class'));
143 // Add city activation filters
144 $controllerInstance->addActivationFilter(ObjectFactory::createObjectByConfiguredName('city_daemon_activation_task_handler_initializer_filter_class'));
146 // Add shutdown filters, you may want to shutdown the task handler as last one.
147 $controllerInstance->addShutdownFilter(ObjectFactory::createObjectByConfiguredName('city_daemon_shutdown_task_handler_filter_class'));
149 // This is the last generic shutdown filter
150 $controllerInstance->addShutdownFilter(ObjectFactory::createObjectByConfiguredName('city_daemon_shutdown_city_filter_class'));