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