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