FRED™  3.0
FRED™: Framework for Rapid and Easy Development
Services.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Rsi\Fred;
4 
5 class Services extends Component{
6 
7  protected $_endpointKey = 'services-endpoint'; //!< Key for the last successful endpoint URL in the cache.
8  protected $_endpointTtl = 86400; //!< Default TTL for the endpoint URL to be stored in the cache.
9  protected $_endpoints = ['' => []]; //!< Available endpoints (key = URL, value = params: username, password, timeout, ttl =
10  // endpoint specific cache TTL, weight = relative chance to be choosen from the endpoints; main endpoint: high TTL, high
11  // weight, backup endpoint: low TTL, low weight; all optional).
12  protected $_endpointUrl = null;
13  protected $_endpoint = null;
14 
15  protected function endpointParam($key,$default = null){
16  return \Rsi\Record::get($this->_endpoints[$this->_endpointUrl],$key,$default);
17  }
18 
19  protected function getEndpoint(){
20  if($this->_endpoint === null){
21  $cache = $this->component('cache');
22  $this->_endpointUrl = $cache->_fetch($this->_endpointKey);
23  if(($this->_endpointUrl === false) || !array_key_exists($this->_endpointUrl,$this->_endpoints)){
24  $endpoints = [];
25  foreach($this->_endpoints as $endpoint => $params)
26  $endpoints = array_merge($endpoints,array_fill(0,\Rsi\Record::get($params,'weight',1),$endpoint));
27  $this->_endpointUrl = $endpoints[rand(0,count($endpoints) - 1)];
28  $cache->_save($this->_endpointKey,$this->_endpointUrl,$this->endpointParam('ttl',$this->_endpointTtl));
29  }
30  $this->component('log')->debug(__CLASS__ . "->endpointUrl = '{$this->_endpointUrl}'",__FILE__,__LINE__);
31  $this->_endpoint = new \Rsi\Services($this->endpointParam('username'),$this->endpointParam('password'),$this->_endpointUrl,$this->endpointParam('timeout'));
32  }
33  return $this->_endpoint;
34  }
35 
36  protected function getResponse(){
37  return $this->_endpoint ? $this->_endpoint->response : false;
38  }
39 
40  public function __call($func_name,$params){
41  $log = $this->component('log');
42  $log->debug(__CLASS__ . "::$func_name(...)",__FILE__,__LINE__);
43  while($this->_endpoints) try{
44  return call_user_func_array([$this->endpoint,$func_name],$params);
45  }
46  catch(\Rsi\Services\Exception $e){
47  $log->error('Endpoint error: ' . $e->getMessage(),__FILE__,__LINE__);
48  unset($this->_endpoints[$this->_endpointUrl]);
49  $this->_endpoint = null;
50  }
51  throw new Exception('No (more) endpoints');
52  }
53 
54 }
endpointParam($key, $default=null)
Definition: Services.php:15
$_endpointKey
Key for the last successful endpoint URL in the cache.
Definition: Services.php:7
Basic component class.
Definition: Component.php:8
$_endpointTtl
Default TTL for the endpoint URL to be stored in the cache.
Definition: Services.php:8
$_endpoints
Available endpoints (key = URL, value = params: username, password, timeout, ttl =.
Definition: Services.php:9
component($name)
Get a component (local or default).
Definition: Component.php:80
__call($func_name, $params)
Definition: Services.php:40