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