1 <?php
2
3 namespace rpf\api;
4 use rpf\system\module;
5
6 7 8 9 10 11 12 13 14
15 class apiModule extends module
16 {
17 18 19 20
21 protected $rpcMethod = 'UNDEFINED';
22
23 24 25
26 private $rpcParams = array();
27
28 29 30
31 private $rpcCache = array();
32
33
34 35 36 37 38
39 protected function addParam($name, $value)
40 {
41 $this->rpcParams[(string) $name] = $value;
42 return $this;
43 }
44
45 46 47 48 49 50
51 public function get($cache = true)
52 {
53 return $this->getRpcResponse($this->rpcMethod, $this->rpcParams, null, $cache);
54 }
55
56 57 58 59 60 61 62 63 64 65
66 private function getRpcResponse($sMethod, $hArgs=array(), $hPlaceholders=null, $cache = true)
67 {
68 $requestString = '';
69 foreach ($this->rpcParams as $name => $value)
70 {
71 72 73 74
75 $requestString .= empty($requestString) ? $sMethod.'(' : ',';
76 $requestString .= "'$name' => '$value'";
77 }
78 $requestString .= ')';
79
80
81
82 if ($cache === true && isset($this->rpcCache[$requestString]))
83 {
84 module\log::debug('Getting RPC-Response from runtime-cache', __METHOD__ . "($requestString)");
85 return $this->rpcCache[$requestString];
86 }
87 else if ($cache === 'memcache')
88 {
89 throw new module\exception('Sorry, memcach is not implemented yet');
90 }
91 else
92 {
93 $duration = microtime(1);
94 global $_BBRPC_Msgs;
95 $this->rpcCache[$requestString] = bbRpc::call($sMethod,$hArgs,$hPlaceholders);
96 $duration = round(microtime(1)-$duration, 3);
97 $resultCounter = count($this->rpcCache[$requestString]);
98 module\log::debug("Performing RPC-Request within $duration sec.", __METHOD__);
99 module\log::debug("Getting $resultCounter rows ", $requestString);
100 $this->fetchRpcLog();
101 }
102 return $this->rpcCache[$requestString];
103 }
104
105 106 107
108 protected function fetchRpcLog()
109 {
110
111
112 global $_BBRPC_Msgs;
113
114 if (is_array($_BBRPC_Msgs))
115 {
116 foreach ($_BBRPC_Msgs as $key => &$hMsg)
117 {
118 switch ($hMsg["typ"])
119 {
120 case 0:
121 module\log::error("RPC-Msg.: $hMsg", __METHOD__);
122 break;
123 case 1:
124 module\log::warning("RPC-Msg.: $hMsg", __METHOD__);
125 break;
126 case 2:
127 module\log::info("RPC-Msg.: $hMsg", __METHOD__);
128 break;
129 case 3:
130 module\log::debug("RPC-Msg.: OK! $hMsg", __METHOD__);
131 break;
132 case 4:
133 module\log::debug("RPC-Msg.: $hMsg", __METHOD__);
134 break;
135 }
136 unset($_BBRPC_Msgs[$key]);
137 }
138 }
139 }
140 }