ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilRPCServerSettings.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 /*
5  +-----------------------------------------------------------------------------+
6  | ILIAS open source |
7  +-----------------------------------------------------------------------------+
8  | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
9  | |
10  | This program is free software; you can redistribute it and/or |
11  | modify it under the terms of the GNU General Public License |
12  | as published by the Free Software Foundation; either version 2 |
13  | of the License, or (at your option) any later version. |
14  | |
15  | This program is distributed in the hope that it will be useful, |
16  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
17  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18  | GNU General Public License for more details. |
19  | |
20  | You should have received a copy of the GNU General Public License |
21  | along with this program; if not, write to the Free Software |
22  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
23  +-----------------------------------------------------------------------------+
24 */
25 
26 
33 {
34  private const RPC_SERVER_PATH = "/RPC2";
35 
36  private static ?ilRPCServerSettings $instance = null;
37 
38  public string $rpc_host = '';
39  public string $rpc_port = '';
40 
41  private ilLogger $log;
43 
44  private function __construct()
45  {
46  global $DIC;
47 
48  $this->log = $DIC->logger()->wsrv();
49  $this->settings = $DIC->settings();
50  }
51 
52  public static function getInstance(): ilRPCServerSettings
53  {
54  if (self::$instance) {
55  return self::$instance;
56  }
57  return self::$instance = new ilRPCServerSettings();
58  }
59 
63  public function isEnabled(): bool
64  {
65  return $this->getHost() !== '' && $this->getPort() !== '';
66  }
67 
68  public function getServerUrl(): string
69  {
70  return 'http://' . $this->getHost() . ':' . $this->getPort() . '/' . self::RPC_SERVER_PATH;
71  }
72 
73  public function getHost(): string
74  {
75  if ($this->rpc_host !== '') {
76  return $this->rpc_host;
77  }
78  return $this->rpc_host = (string) $this->settings->get('rpc_server_host');
79  }
80 
81  public function setHost($a_host): void
82  {
83  $this->rpc_host = $a_host;
84  }
85 
86  public function getPort(): string
87  {
88  if ($this->rpc_port !== '') {
89  return $this->rpc_port;
90  }
91  return $this->rpc_port = (string) $this->settings->get('rpc_server_port');
92  }
93 
94  public function setPort(string $a_port): void
95  {
96  $this->rpc_port = $a_port;
97  }
98 
99  public function getPath(): string
100  {
101  return self::RPC_SERVER_PATH;
102  }
103 
104  public function update(): void
105  {
106  $this->settings->set('rpc_server_host', $this->getHost());
107  $this->settings->set('rpc_server_port', $this->getPort());
108  }
109 
110  public function pingServer(): bool
111  {
112  try {
113  ilRpcClientFactory::factory('RPCebug')->ping();
114  return true;
115  } catch (Exception $e) {
116  $this->log->warning('Calling RPC server failed with message: ' . $e->getMessage());
117  return false;
118  }
119  }
120 }
static ilRPCServerSettings $instance
static factory(string $a_package, int $a_timeout=0)
Creates an ilRpcClient instance to our ilServer.
global $DIC
Definition: feed.php:28
isEnabled()
Returns true if server ip and port are set.
Class for storing all rpc communication settings.