]> git.mxchange.org Git - core.git/blob - framework/main/classes/factories/client/class_ClientFactory.php
Continued:
[core.git] / framework / main / classes / factories / client / class_ClientFactory.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Factory\Client;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
7
8 /**
9  * An object factory for clients
10  *
11  * @author              Roland Haeder <webmaster@shipsimu.org>
12  * @version             0.0.0
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
16  *
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.
21  *
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.
26  *
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/>.
29  */
30 class ClientFactory extends ObjectFactory {
31         /**
32          * Protected constructor
33          *
34          * @return      void
35          */
36         protected function __construct () {
37                 // Call parent constructor
38                 parent::__construct(__CLASS__);
39         }
40
41         /**
42          * Creates a client object for given protocol. This method uses the
43          * registry pattern to cache those instances.
44          *
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
48          */
49         public static final function createClientByProtocolInstance (HandleableProtocol $protocolInstance, $socketResource = FALSE) {
50                 // Default is NULL (to initialize variable)
51                 $clientInstance = NULL;
52
53                 // Generate registry key
54                 $registryKey = strtolower($protocolInstance->getProtocolName()) . '_client';
55
56                 // Is the key already in registry?
57                 if (GenericRegistry::getRegistry()->instanceExists($registryKey)) {
58                         // Then use that instance
59                         $clientInstance = GenericRegistry::getRegistry()->getInstance($registryKey);
60
61                         // Set socket resource
62                         $clientInstance->setSocketResource($socketResource);
63                 } else {
64                         // Generate object instance
65                         $clientInstance = self::createObjectByConfiguredName($registryKey, array($socketResource));
66
67                         // Set it in registry for later re-use
68                         GenericRegistry::getRegistry()->addInstance($registryKey, $clientInstance);
69                 }
70
71                 // Return the prepared instance
72                 return $clientInstance;
73         }
74
75 }