1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13:
14: 15: 16: 17: 18: 19: 20: 21: 22:
23: abstract class Jyxo_Rpc_Server
24: {
25: 26: 27: 28: 29:
30: private $aliases = array();
31:
32: 33: 34: 35: 36:
37: private $logFile;
38:
39: 40: 41: 42: 43: 44:
45: private $logCallback;
46:
47: 48: 49:
50: protected function __construct()
51: {}
52:
53: 54: 55:
56: public function __destruct()
57: {}
58:
59: 60: 61: 62: 63:
64: public final function __clone()
65: {
66: throw new LogicException(sprintf('Class %s can have only one instance.', get_class($this)));
67: }
68:
69: 70: 71: 72: 73:
74: public static function getInstance()
75: {
76: static $instance;
77: if (null === $instance) {
78: $instance = new static();
79: }
80:
81: return $instance;
82: }
83:
84: 85: 86: 87: 88: 89: 90: 91:
92: public function enableLogging($filename, $callback = null)
93: {
94: $filename = (string) $filename;
95: $filename = trim($filename);
96:
97:
98: if (empty($filename)) {
99: throw new InvalidArgumentException('No log file was provided.');
100: }
101:
102: $this->logFile = $filename;
103:
104:
105: if ((!empty($callback)) && (!is_callable($callback))) {
106: throw new InvalidArgumentException('Invalid callback was provided.');
107: }
108:
109: $this->logCallback = $callback;
110:
111: return $this;
112: }
113:
114: 115: 116: 117: 118: 119: 120: 121:
122: public function registerClass($class, $useFullName = true)
123: {
124: if (!class_exists($class)) {
125: throw new InvalidArgumentException(sprintf('Class %s does not exist.', $class));
126: }
127:
128: $reflection = new ReflectionClass($class);
129: foreach ($reflection->getMethods() as $method) {
130:
131: if ($method->isPublic()) {
132: $func = $class . '::' . $method->getName();
133:
134:
135: if (!$useFullName) {
136: $this->aliases[$method->getName()] = $func;
137: $func = $method->getName();
138: }
139:
140: $this->register($func);
141: }
142: }
143:
144: return $this;
145: }
146:
147: 148: 149: 150: 151: 152: 153: 154: 155: 156:
157: public function registerMethod($class, $method, $useFullName = true)
158: {
159: if (!class_exists($class)) {
160: throw new InvalidArgumentException(sprintf('Třída %s neexistuje.', $class));
161: }
162:
163:
164: if ((!method_exists($class, '__call')) && (!method_exists($class, '__callStatic'))) {
165: try {
166: $reflection = new ReflectionMethod($class, $method);
167: } catch (ReflectionException $e) {
168: throw new InvalidArgumentException(sprintf('Method %s::%s does not exist.', $class, $method));
169: }
170:
171:
172: if (!$reflection->isPublic()) {
173: throw new InvalidArgumentException(sprintf('Method %s::%s is not public.', $class, $method));
174: }
175: }
176:
177: $func = $class . '::' . $method;
178:
179:
180: if (!$useFullName) {
181: $this->aliases[$method] = $func;
182: $func = $method;
183: }
184:
185: $this->register($func);
186:
187: return $this;
188: }
189:
190: 191: 192: 193: 194: 195: 196:
197: public function registerFunc($func)
198: {
199: if (!function_exists($func)) {
200: throw new InvalidArgumentException(sprintf('Function %s does not exist.', $func));
201: }
202:
203: $this->register($func);
204:
205: return $this;
206: }
207:
208: 209: 210: 211: 212:
213: abstract protected function register($func);
214:
215: 216: 217:
218: abstract public function process();
219:
220: 221: 222: 223: 224: 225: 226:
227: protected function call($method, $params)
228: {
229: $func = $method;
230:
231: if (isset($this->aliases[$method])) {
232: $func = $this->aliases[$method];
233: }
234:
235:
236: if (false !== strpos($func, '::')) {
237: list($className, $methodName) = explode('::', $func);
238:
239: try {
240:
241: $reflection = new ReflectionMethod($className, $methodName);
242: if ($reflection->isStatic()) {
243:
244: $callback = array($className, $methodName);
245: } else {
246:
247: $callback = array(new $className(), $methodName);
248: }
249: } catch (ReflectionException $e) {
250:
251: if (method_exists($className, '__call')) {
252:
253: $callback = array(new $className(), $methodName);
254: } else {
255:
256: $callback = array($className, $methodName);
257: }
258: }
259: } else {
260:
261: $callback = $func;
262: }
263:
264: $result = call_user_func_array($callback, $params);
265:
266:
267: $this->log($method, $params, $result);
268:
269: return $result;
270: }
271:
272: 273: 274: 275: 276: 277: 278:
279: private function log($method, $params, $result)
280: {
281:
282: if (!empty($this->logFile)) {
283:
284: if (!empty($this->logCallback)) {
285: list($method, $params, $result) = call_user_func($this->logCallback, $method, $params, $result);
286: }
287:
288:
289: $text = sprintf("Method: %s\n", $method);
290:
291: foreach ($params as $paramName => $param) {
292: $text .= sprintf("Param %s: %s\n", $paramName, trim(print_r($param, true)));
293: }
294:
295: $text .= sprintf("Result: %s\n", trim(print_r($result, true)));
296:
297:
298: $text = strtr(trim($text), array("\n" => "\n\t"));
299:
300:
301: $text = sprintf("[%s] %s %s %s\n\t%s\n", date('Y-m-d H:i:s'), $_SERVER['REMOTE_ADDR'], $_SERVER['HTTP_HOST'], $_SERVER['REQUEST_URI'], $text);
302:
303:
304: file_put_contents($this->logFile, $text, FILE_APPEND);
305: }
306: }
307: }
308: