home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / XML / RPC2 / Server / CallHandler / Instance.php < prev   
Encoding:
PHP Script  |  2008-07-02  |  5.8 KB  |  149 lines

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
  4.  
  5. // LICENSE AGREEMENT. If folded, press za here to unfold and read license {{{ 
  6.  
  7. /**
  8. * +-----------------------------------------------------------------------------+
  9. * | Copyright (c) 2004-2006 Sergio Goncalves Carvalho                                |
  10. * +-----------------------------------------------------------------------------+
  11. * | This file is part of XML_RPC2.                                              |
  12. * |                                                                             |
  13. * | XML_RPC2 is free software; you can redistribute it and/or modify            |
  14. * | it under the terms of the GNU Lesser General Public License as published by |
  15. * | the Free Software Foundation; either version 2.1 of the License, or         |
  16. * | (at your option) any later version.                                         |
  17. * |                                                                             |
  18. * | XML_RPC2 is distributed in the hope that it will be useful,                 |
  19. * | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
  20. * | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
  21. * | GNU Lesser General Public License for more details.                         |
  22. * |                                                                             |
  23. * | You should have received a copy of the GNU Lesser General Public License    |
  24. * | along with XML_RPC2; if not, write to the Free Software                     |
  25. * | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA                    |
  26. * | 02111-1307 USA                                                              |
  27. * +-----------------------------------------------------------------------------+
  28. * | Author: Sergio Carvalho <sergio.carvalho@portugalmail.com>                  |
  29. * +-----------------------------------------------------------------------------+
  30. *
  31. * @category   XML
  32. * @package    XML_RPC2
  33. * @author     Sergio Carvalho <sergio.carvalho@portugalmail.com>  
  34. * @copyright  2004-2006 Sergio Carvalho
  35. * @license    http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
  36. * @version    CVS: $Id: Instance.php,v 1.5 2006/01/22 01:54:48 fab Exp $
  37. * @link       http://pear.php.net/package/XML_RPC2
  38. */
  39.  
  40. // }}}
  41.  
  42. // dependencies {{{
  43. require_once 'XML/RPC2/Exception.php';
  44. require_once 'XML/RPC2/Server/Method.php';
  45. require_once 'XML/RPC2/Server/CallHandler.php';
  46. // }}}
  47.  
  48. /**
  49.  * This class is a server call handler which exposes an instance's public methods.
  50.  *
  51.  * XML_RPC2_Server_Callhandler_Instance is the preferred call handler to use when
  52.  * you just need to quickly expose an already existing object. If designing a remote 
  53.  * API from the ground up, it's best to use XML_RPC2_Server_Callhandler_Class instead.
  54.  *
  55.  * Usage is simple:
  56.  *  - PhpDoc the methods, including at least method signature (params and return types) and short description.
  57.  *  - Use the XML_RPC2 factory method to create a server based on the interface class.
  58.  * A simple example:
  59.  * <code>
  60.  * class EchoServer {
  61.  *     /**
  62.  *      * Echo the message
  63.  *      *
  64.  *      * @param string The string to echo
  65.  *      * @return string The echo
  66.  *     {@*}
  67.  *     public function echoecho($string) 
  68.  *     {
  69.  *         return $string;
  70.  *     }
  71.  * }
  72.  * 
  73.  * require_once 'XML/RPC2/Server.php';
  74.  * $someInstance = new EchoServer();
  75.  * $server = XML_RPC2_Server::create($someInstance);
  76.  * $server->handleCall();
  77.  * </code>
  78.  *
  79.  * @category   XML
  80.  * @package    XML_RPC2
  81.  * @author     Sergio Carvalho <sergio.carvalho@portugalmail.com>  
  82.  * @copyright  2004-2006 Sergio Carvalho
  83.  * @license    http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
  84.  * @link       http://pear.php.net/package/XML_RPC2
  85.  * @see XML_RPC2_Server::create
  86.  * @see XML_RPC2_Server_Callhandler_Class
  87.  */
  88. class XML_RPC2_Server_Callhandler_Instance extends XML_RPC2_Server_CallHandler
  89. {
  90.     
  91.     // {{{ properties
  92.     
  93.     /**
  94.      * instance of target object
  95.      * 
  96.      * @var mixed
  97.      */
  98.     private $_instance;
  99.        
  100.     // }}}
  101.     // {{{ constructor 
  102.     
  103.     /**
  104.      * XML_RPC2_Server_Callhandler_Class Constructor. Creates a new call handler exporting the given object methods
  105.      *
  106.      * Before using this constructor, take a look at XML_RPC2_Server::create. The factory
  107.      * method is usually a quicker way of instantiating the server and its call handler.
  108.      *
  109.      * @see XML_RPC2_Server::create()
  110.      * @param object The Target object. Calls will be made on this instance
  111.      * @param string Default prefix to prepend to all exported methods (defaults to '')
  112.      */
  113.     public function __construct($instance, $defaultPrefix) 
  114.     {
  115.         $this->_instance = $instance;
  116.         $reflection = new ReflectionClass(get_class($instance));
  117.         foreach ($reflection->getMethods() as $method) {
  118.             if (!$method->isStatic() && $method->isPublic() && !$method->isConstructor())
  119.             {
  120.                 $candidate = new XML_RPC2_Server_Method($method, $defaultPrefix);
  121.                 if (!$candidate->isHidden()) $this->addMethod($candidate);
  122.             }
  123.         }
  124.     }
  125.     
  126.     // }}}
  127.     // {{{ __call()
  128.     
  129.     /**
  130.      * __call catchall. Delegate the method call to the target object, and return its result
  131.      *
  132.      * @param string Name of method to call
  133.      * @param array  Array of parameters for call
  134.      * @return mixed Whatever the target method returned
  135.      */
  136.     public function __call($methodName, $parameters)
  137.     {
  138.         if (!array_key_exists($methodName, $this->getMethods())) {
  139.             throw new XML_RPC2_UnknownMethodException("Method $methodName is not exported by this server");
  140.         }
  141.         return call_user_func_array(array($this->_instance, $this->getMethod($methodName)->getInternalMethod()), $parameters);
  142.     }
  143.     
  144.     // }}}
  145.     
  146. }
  147.  
  148. ?>
  149.