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