1<?php 2/** 3 * Tivoka - JSON-RPC done right! 4 * Copyright (c) 2011-2012 by Marcel Klehr <mklehr@gmx.net> 5 * 6 * MIT LICENSE 7 * 8 * Permission is hereby granted, free of charge, to any person obtaining a copy 9 * of this software and associated documentation files (the "Software"), to deal 10 * in the Software without restriction, including without limitation the rights 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 * copies of the Software, and to permit persons to whom the Software is 13 * furnished to do so, subject to the following conditions: 14 * 15 * The above copyright notice and this permission notice shall be included in 16 * all copies or substantial portions of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 * THE SOFTWARE. 25 * 26 * @package Tivoka 27 * @author Marcel Klehr <mklehr@gmx.net> 28 * @copyright (c) 2011-2012, Marcel Klehr 29 */ 30 31namespace Tivoka\Server; 32use Tivoka\Exception; 33 34/** 35 * MethodWrapper for implementing anonymous objects on the fly 36 * @package Tivoka 37 */ 38class MethodWrapper 39{ 40 /** 41 * @var array The list of callbacks 42 */ 43 private $___methods; 44 45 /** 46 * Registers a server method 47 * 48 * @param string $name The name of the method to provide (already existing methods with the same name will be overridden) 49 * @param callback $method The callback 50 * @returns bool FALSE if no valid callback has been given 51 */ 52 public function ___register($name, $method) 53 { 54 if(!is_callable($method)) return FALSE; 55 56 $this->___methods[$name] = $method; 57 return TRUE; 58 } 59 60 /** 61 * Returns TRUE if the method with the given name is registered and a valid callback 62 * 63 * @param callback $method The name of the method to check 64 * @returns bool 65 */ 66 public function ___exist($method) 67 { 68 if(!is_array($this->___methods))return FALSE; 69 if(is_callable($this->___methods[$method]))return TRUE; 70 } 71 72 /** 73 * Invokes the requested method 74 * 75 * @param string $method 76 * @param array $args 77 * @return mixed|void 78 */ 79 public function __call($method,$args) 80 { 81 if(!$this->___exist($method)){ 82 $args[0]->error(-32601); return; 83 } 84 $prc = $args[0]; 85 return call_user_func_array($this->___methods[$method],array($prc)); 86 } 87} 88?> 89