Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 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: Class.php 205680 2006-01-22 01:54:48Z fab $
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 a classe's static public methods.
50
 *
51
 * XML_RPC2_Server_Callhandler_Class is the preferred call handler to use when you are
52
 * designing your XML-RPC server from the ground up. Usage is quite simple:
53
 *  - Create a class holding all of the XML-RPC server's exported procedures as public static methods (the interface class).
54
 *  - PhpDoc the classes' methods, including at least method signature (params and return types) and short description.
55
 *  - Use the XML_RPC2 factory method to create a server based on the interface class.
56
 * A simple example:
57
 * <code>
58
 * /**
59
 *  * echoecho echoes the message received
60
 *  *
61
 *  * @param string  Message
62
 *  * @return string The echo
63
 *  {@*}
64
 * class EchoServer {
65
 *     public static function echoecho($string)
66
 *     {
67
 *         return $string;
68
 *     }
69
 * }
70
 *
71
 * require_once 'XML/RPC2/Server.php';
72
 * $server = XML_RPC2_Server::create('EchoServer');
73
 * $server->handleCall();
74
 * </code>
75
 *
76
 * Use this call handler if you have designed your xml-rpc external interface as a set of
77
 * public class methods on a given class. If, on the other hand, you intend to export an
78
 * already existing class, it may be that not all of the methods you want to export are static.
79
 * In that case, it is probably best to use XML_RPC2_Server_Callhandler_Instance instead.
80
 *
81
 * @category   XML
82
 * @package    XML_RPC2
83
 * @author     Sergio Carvalho <sergio.carvalho@portugalmail.com>
84
 * @copyright  2004-2006 Sergio Carvalho
85
 * @license    http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
86
 * @link       http://pear.php.net/package/XML_RPC2
87
 * @see XML_RPC2_Server::create
88
 * @see XML_RPC2_Server_Callhandler_Instance
89
 */
90
class XML_RPC2_Server_Callhandler_Class extends XML_RPC2_Server_CallHandler
91
{
92
 
93
    // {{{ properties
94
 
95
    /**
96
     * name of target class
97
     *
98
     * @var string
99
     */
100
    private $_className;
101
 
102
    // }}}
103
    // {{{ constructor
104
 
105
    /**
106
     * XML_RPC2_Server_Callhandler_Class Constructor. Creates a new call handler exporting the give static class' methods
107
     *
108
     * Before using this constructor, take a look at XML_RPC2_Server::create. The factory
109
     * method is usually a quicker way of instantiating the server and its call handler.
110
     *
111
     * @see XML_RPC2_Server::create()
112
     * @param string The Target class. Calls will be made on this class
113
     * @param string Default prefix to prepend to all exported methods (defaults to '')
114
     */
115
    public function __construct($className, $defaultPrefix)
116
    {
117
        $this->_className = $className;
118
        $reflection = new ReflectionClass($className);
119
        foreach ($reflection->getMethods() as $method) {
120
            if ($method->isStatic() && $method->isPublic() && !$method->isAbstract() && !$method->isConstructor())
121
            {
122
                $candidate = new XML_RPC2_Server_Method($method, $defaultPrefix);
123
                if (!$candidate->isHidden()) $this->addMethod($candidate);
124
            }
125
        }
126
    }
127
 
128
    // }}}
129
    // {{{ __call()
130
 
131
    /**
132
     * __call catchall. Delegate the method call to the target class, and return its result
133
     *
134
     * @param string Name of method to call
135
     * @param array  Array of parameters for call
136
     * @return mixed Whatever the target method returned
137
     */
138
    public function __call($methodName, $parameters)
139
    {
140
        if (!array_key_exists($methodName, $this->getMethods())) {
141
            throw new XML_RPC2_UnknownMethodException("Method $methodName is not exported by this server");
142
        }
143
        return call_user_func_array(array($this->_className, $this->getMethod($methodName)->getInternalMethod()), $parameters);
144
    }
145
 
146
    // }}}
147
 
148
}
149
 
150
?>