]> git.mxchange.org Git - city.git/blob - application/city/classes/commands/console/class_CityConsoleDaemonCommand.php
Continued:
[city.git] / application / city / classes / commands / console / class_CityConsoleDaemonCommand.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\City\Command;
4
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;
13
14 /**
15  * A command for the 'daemon' routine
16  *
17  * @author              Roland Haeder <webmaster@shipsimu.org>
18  * @version             0.0.0
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
22  *
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.
27  *
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.
32  *
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/>.
35  */
36 class CityConsoleDaemonCommand extends BaseCommand implements Commandable {
37         /**
38          * Protected constructor
39          *
40          * @return      void
41          */
42         protected function __construct () {
43                 // Call parent constructor
44                 parent::__construct(__CLASS__);
45         }
46
47         /**
48          * Creates an instance of this class
49          *
50          * @param       $resolverInstance       An instance of a command resolver class
51          * @return      $commandInstance        An instance a prepared command class
52          */
53         public static final function createCityConsoleDaemonCommand (CommandResolver $resolverInstance) {
54                 // Get new instance
55                 $commandInstance = new CityConsoleDaemonCommand();
56
57                 // Set the application instance
58                 $commandInstance->setResolverInstance($resolverInstance);
59
60                 // Return the prepared instance
61                 return $commandInstance;
62         }
63
64         /**
65          * Executes the given command with given request and response objects
66          *
67          * @param       $requestInstance        An instance of a class with an Requestable interface
68          * @param       $responseInstance       An instance of a class with an Responseable interface
69          * @return      void
70          * @todo        ~10% done?
71          */
72         public function execute (Requestable $requestInstance, Responseable $responseInstance) {
73                 // Get a registry and the application instance from it
74                 $applicationInstance = Registry::getRegistry()->getInstance('app');
75
76                 /*
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)
81                  */
82                 self::createDebugInstance(__CLASS__)->debugOutput('BOOTSTRAP: Beginning with bootstrap...');
83                 $applicationInstance->getControllerInstance()->executeBootstrapFilters($requestInstance, $responseInstance);
84                 self::createDebugInstance(__CLASS__)->debugOutput('BOOTSTRAP: Bootstrap finished.');
85
86                 // Get city instance
87                 $cityInstance = CityDaemonFactory::createCityDaemonInstance();
88
89                 // Add some city-specific filters, e.g. announcement
90                 $cityInstance->addExtraCityFilters();
91
92                 /*
93                  * -------------------------- City activation --------------------------
94                  * Activates the city daemon by doing some final preparation steps and
95                  * setting the attribute $cityIsActive to TRUE.
96                  */
97                 $cityInstance->activateCityDaemon($requestInstance, $responseInstance);
98
99                 // Get task handler instance
100                 $handlerInstance = Registry::getRegistry()->getInstance('task_handler');
101
102                 // Debug message
103                 self::createDebugInstance(__CLASS__)->debugOutput('MAIN: --- Entering main loop. ---');
104
105                 /*
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.
110                  */
111                 while (($cityInstance->isCityActive()) && ($handlerInstance->hasTasksLeft())) {
112                         // Handle all tasks here
113                         $handlerInstance->handleTasks();
114                 } // END - while
115
116                 // Debug message
117                 self::createDebugInstance(__CLASS__)->debugOutput('MAIN: --- Leaving main loop. ---');
118         }
119
120         /**
121          * Adds extra filters to the given controller instance
122          *
123          * @param       $controllerInstance             A controller instance
124          * @param       $requestInstance                An instance of a class with an Requestable interface
125          * @return      void
126          * @todo        ~10% done
127          */
128         public function addExtraFilters (Controller $controllerInstance, Requestable $requestInstance) {
129                 // Add pre filters
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'));
133
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'));
137
138                 // Add city activation filters
139                 $controllerInstance->addActivationFilter(ObjectFactory::createObjectByConfiguredName('city_daemon_activation_task_handler_initializer_filter'));
140
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'));
143
144                 // This is the last generic shutdown filter
145                 $controllerInstance->addShutdownFilter(ObjectFactory::createObjectByConfiguredName('city_daemon_shutdown_city_filter'));
146         }
147 }