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