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