3 namespace Org\Mxchange\City\Daemon\Factory;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Request\Requestable;
7 use Org\Mxchange\CoreFramework\Response\Responseable;
10 * A factory class for cities
12 * @author Roland Haeder <webmaster@ship-simu.org>
14 * @copyright Copyright (c) 2015, 2016 City Developer Team
15 * @license GNU GPL 3.0 or any newer version
16 * @link http://www.ship-simu.org
18 * This program is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 class CityDaemonFactory extends ObjectFactory {
33 * Protected constructor
37 protected function __construct () {
38 // Call parent constructor
39 parent::__construct(__CLASS__);
43 * Returns a singleton (registry-based) CityHelper instance
45 * @param $requestInstance An instance of a class with an Requestable interface
46 * @param $responseInstance An instance of a class with an Responseable interface
47 * @return $cityInstance An instance of a CityHelper class
48 * @throws FactoryRequiredParameterException If not all parameters are set and no instance 'city' is set.
50 public static final function createCityDaemonInstance (Requestable $requestInstance = NULL, Responseable $responseInstance = NULL) {
51 // Get new factory instance
52 $factoryInstance = new CityDaemonFactory();
54 // If there is no handler?
55 if (Registry::getRegistry()->instanceExists('city')) {
56 // Get handler from registry
57 $cityInstance = Registry::getRegistry()->getInstance('city');
58 } elseif (($requestInstance instanceof Requestable) && ($responseInstance instanceof Responseable)) {
59 // The default city-mode is from our configuration
60 $cityMode = $factoryInstance->getConfigInstance()->getConfigEntry('city_default_mode');
62 // Is the city 'mode' parameter set?
63 if ($requestInstance->isRequestElementSet('mode')) {
64 // Then use this which overrides the config entry temporarily
65 $cityMode = $requestInstance->getRequestElement('mode');
67 // Set it for easier re-usage
68 $requestInstance->setRequestElement('mode', $cityMode);
71 // Now convert the city-mode in a class name
72 $className = 'Simulation' . $factoryInstance->convertToClassName($cityMode) . 'CityDaemon';
74 // Get the city instance
75 $cityInstance = ObjectFactory::createObjectByName($className, array($requestInstance));
78 $applicationInstance = Registry::getRegistry()->getInstance('app');
80 // Set the app instance
81 $cityInstance->setApplicationInstance($applicationInstance);
83 // Add city-specific filters
84 $cityInstance->addExtraFilters($applicationInstance->getControllerInstance(), $responseInstance);
86 // Throw an exception here
87 throw new FactoryRequiredParameterException($factoryInstance, self::EXCEPTION_FACTORY_REQUIRE_PARAMETER);
90 // Return the instance