RemotingServer.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2015 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
11 namespace wcmf\lib\service;
12 
16 
17 /**
18  * RemotingServer is used to communicate with other wCMF instances.
19  * The url and login credentials of a remote instance are configured
20  * using the configuration sections RemoteServer and RemoteUser.
21  * Each remote instance is identified by a unique server key. The following
22  * example configures ServerKeyA over http and ServerKeyB over command
23  * line:
24  *
25  * @code
26  * [remoteserver]
27  * ServerKeyA = http://localhost/wcmfA
28  * ServerKeyB = /path/to/wcmfB/main.php
29  *
30  * [remoteuser]
31  * ServerKeyA = {loginA, passwordA}
32  * ServerKeyB = {loginB, passwordB}
33  *
34  * [system]
35  * php = /path/to/php-cli
36  * @endcode
37  *
38  * @author ingo herwig <ingo@wemove.com>
39  */
41 
42  private $_clients = array();
43  private $_users = array();
44 
45  /**
46  * Send a request to the server identified by serverKey.
47  * @param $serverKey An entry in the configuration section 'remoteserver'
48  * @param $request A Request instance
49  * @return A Response instance
50  */
51  public function doCall($serverKey, $request) {
52  $client = $this->getClient($serverKey);
53  if ($client) {
54  $response = $client->call($request);
55  return $response;
56  }
57  return ObjectFactory::getInstance('response');
58  }
59 
60  /**
61  * Get a client instance for a given server key
62  * @param $serverKey An entry in the configuration section 'remoteserver'
63  * @return A client instance or null
64  */
65  private function getClient($serverKey) {
66  if (!isset($this->_clients[$serverKey])) {
67  $config = ObjectFactory::getInstance('configuration');
68  $serverDef = $config->getValue($serverKey, 'remoteserver');
69  // get remote the user
70  $user = $this->getRemoteUser($serverKey);
71 
72  $client = null;
73  if (strpos($serverDef, 'http://') === 0 || strpos($serverDef, 'https://') === 0) {
74  $client = new HTTPClient($serverDef, $user);
75  }
76  else {
77  $client = new RPCClient($serverDef, $user);
78  }
79  $this->_clients[$serverKey] = $client;
80  }
81  return $this->_clients[$serverKey];
82  }
83 
84  /**
85  * Get the remote user login and password for a given server key
86  * @param $serverKey An entry in the configuration section 'remoteuser'
87  * @return Array with keys 'login', 'password'
88  */
89  private function getRemoteUser($serverKey) {
90  if (!isset($this->_users[$serverKey])) {
91  $config = ObjectFactory::getInstance('configuration');
92  $remoteUser = $config->getValue($serverKey, 'remoteuser');
93  if (is_array($remoteUser) && sizeof($remoteUser) == 2) {
94  $this->_users[$serverKey] = array(
95  'login' => $remoteUser[0],
96  'password' => $remoteUser[1]
97  );
98  }
99  else {
100  throw new IllegialConfigurationException(
101  "Remote user definition of '".$serverKey.
102  "' must be an array of login and password."
103  );
104  }
105  }
106  return $this->_users[$serverKey];
107  }
108 }
109 ?>
static getInstance($name, $dynamicConfiguration=array())
RemotingServer is used to communicate with other wCMF instances.
Service related interfaces and classes.
Definition: namespaces.php:92
HTTPClient is used to do calls to other wCMF instances over HTTP.
Definition: HTTPClient.php:35
doCall($serverKey, $request)
Send a request to the server identified by serverKey.