3 namespace Org\Mxchange\CoreFramework\Factory\Client;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
9 * An object factory for clients
11 * @author Roland Haeder <webmaster@shipsimu.org>
13 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
14 * @license GNU GPL 3.0 or any newer version
15 * @link http://www.shipsimu.org
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 class ClientFactory extends ObjectFactory {
32 * Protected constructor
36 protected function __construct () {
37 // Call parent constructor
38 parent::__construct(__CLASS__);
42 * Creates a client object for given protocol. This method uses the
43 * registry pattern to cache those instances.
45 * @param $protocolInstance An instance of a HandleableProtocol class to create a client object for (e.g. 'http' for a HTTP/1.1 client)
46 * @param $socketResource A valid socket resource (optional)
47 * @return $clientInstance An instance of the requested client
49 public static final function createClientByProtocolInstance (HandleableProtocol $protocolInstance, $socketResource = FALSE) {
50 // Default is NULL (to initialize variable)
51 $clientInstance = NULL;
53 // Generate registry key
54 $registryKey = strtolower($protocolInstance->getProtocolName()) . '_client';
56 // Is the key already in registry?
57 if (GenericRegistry::getRegistry()->instanceExists($registryKey)) {
58 // Then use that instance
59 $clientInstance = GenericRegistry::getRegistry()->getInstance($registryKey);
61 // Set socket resource
62 $clientInstance->setSocketResource($socketResource);
64 // Generate object instance
65 $clientInstance = self::createObjectByConfiguredName($registryKey, array($socketResource));
67 // Set it in registry for later re-use
68 GenericRegistry::getRegistry()->addInstance($registryKey, $clientInstance);
71 // Return the prepared instance
72 return $clientInstance;