2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4 // +----------------------------------------------------------------------+
5 // | Akelos Framework - http://www.akelos.org |
6 // +----------------------------------------------------------------------+
7 // | Copyright (c) 2002-2006, Akelos Media, S.L. & Bermi Ferrer Martinez |
8 // | Released under the GNU Lesser General Public License, see LICENSE.txt|
9 // +----------------------------------------------------------------------+
12 * @package ActionWebservice
14 * @author Bermi Ferrer <bermi a.t akelos c.om>
15 * @copyright Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org
16 * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
20 * A web service API class specifies the methods that will be available for
21 * invocation for an API. It also contains metadata such as the method type
24 * It is not intended to be instantiated.
26 * It is attached to web service implementation classes like
27 * AkActionWebService and AkActionController derivatives by using
28 * <tt>container::web_service_api</tt>, where <tt>container</tt> is an
29 * AkActionController or an AkActionWebService.
31 * See AkActionWebService/AkDirectContainer.php class methods for an example
34 class AkActionWebserviceApi
extends AkObject
37 * Whether to transform the public API method names into camel-cased names
39 var $inflect_names = true;
42 * Whether to localize the API documentation automatically.
44 var $localize_documentation = true;
47 * If present, the name of a method to call when the remote caller
48 * tried to call a nonexistent method.
50 var $default_api_method;
52 var $_api_methods = array();
53 var $_api_public_method_names = array();
56 * API methods have a +name+, which must be the PHP method name to use when
57 * performing the invocation on the web service object.
59 * The signatures for the method input parameters and return value can
60 * by specified in +options+.
62 * A signature is an array of one or more parameter specifiers.
63 * A parameter specifier can be one of the following:
65 * * A string representing one of the Action Web Service base types.
66 * See AkActionWebService/AkSignatureTypes.php for a canonical list of the base types.
67 * * The Class object of the parameter type
68 * * A single-element Array containing one of the two preceding items. This
69 * will cause Action Web Service to treat the parameter at that position
70 * as an array containing only values of the given type.
71 * * An Array containing as key the name of the parameter, and as value
72 * one of the three preceding items
74 * If no method input parameter or method return value signatures are given,
75 * the method is assumed to take no parameters and/or return no values of
76 * interest, and any values that are received by the server will be
77 * discarded and ignored.
80 * <tt>expects</tt> Signature for the method input parameters
81 * <tt>returns</tt> Signature for the method return value
82 * <tt>expects_and_returns</tt> Signature for both input parameters and return value
84 function addApiMethod($name, $options = array())
86 $this->_validateOptions(array('expects', 'returns', 'expects_and_returns', 'documentation'), array_keys($options));
87 if (!empty($options['expects_and_returns'])){
88 $expects = $returns = $options['expects_and_returns'];
90 $expects = @$options['expects'];
91 $returns = @$options['returns'];
94 $public_name = $this->getPublicApiMethodName($name);
95 $method =& new AkActionWebServiceMethod($name, $public_name, $expects, $returns, @$options['documentation'], $this->localize_documentation
);
96 $this->_api_methods
[$name] =& $method;
97 $this->_api_public_method_names
[$public_name] = $name;
101 * Whether the given method name is a service method on this API
103 function hasApiMethod($name)
105 return !empty($this->_api_methods
[$name]);
109 * Whether the given public method name has a corresponding service method
112 function hasPublicApiMethod($public_name)
114 return !empty($this->_api_public_method_names
[$public_name]);
118 * The corresponding public method name for the given service method name
120 function getPublicApiMethodName($name)
122 return $this->inflect_names ? AkInflector
::camelize($name) : $name;
126 * The corresponding service method name for the given public method name
128 function getApiMethodName($public_name)
130 return $this->_api_public_method_names
[$public_name];
134 * An array containing all service methods on this API, and their
135 * associated metadata.
137 function &getApiMethods()
139 return $this->_api_methods
;
143 * The Method instance for the given public API method name, if any
145 function &getPublicApiMethodInstance($public_method_name)
147 return $this->getApiMethodInstance($this->getApiMethodName($public_method_name));
151 * The Method instance for the given API method name, if any
153 function &getApiMethodInstance($method_name)
155 return $this->_api_methods
[$method_name];
159 * The Method instance for the default API method, if any
161 function &getDefaultApiMethodInstance()
163 if(empty($this->default_api_method
)){
168 $name = $this->default_api_method
;
169 if(!empty($this->default_api_method_instance
->name
) && $this->default_api_method_instance
->name
== $name){
170 return $this->default_api_method_instance
;
173 $this->default_api_method_instance
=& new AkActionWebServiceMethod($name, $this->getPublicApiMethodName($name), null, null, null);
174 return $this->default_api_method_instance
;
177 function _getApiPublicMethodNames()
179 return array_keys($this->_api_public_method_names
);
182 function _validateOptions($valid_option_keys, $supplied_option_keys)
184 $unknown_option_keys = array_diff($supplied_option_keys, $valid_option_keys);
185 if(!empty($unknown_option_keys)){
186 trigger_error(Ak
::t('Unknown options: %options', array('%options'=> var_export($unknown_option_keys,true))), E_USER_ERROR
);
193 * Represents an API method and its associated metadata, and provides functionality
194 * to assist in commonly performed API method tasks.
196 class AkActionWebServiceMethod
203 var $expects_documentation = array();
204 var $returns_documentation = array();
206 function AkActionWebServiceMethod($name, $public_name, $expects, $returns, $documentation, $localize_documentation = true)
209 $this->public_name
= $public_name;
210 $this->expects
= $expects;
211 $this->returns
= $returns;
212 $this->documentation
= $documentation;
213 $this->localize_documentation
= $localize_documentation;
215 $this->_extractDocumentationFromExpects();
216 $this->_extractDocumentationFromReturns();
219 function _extractDocumentationFromExpects()
221 return $this->_extractDocumentationFromMethod('expects');
224 function _extractDocumentationFromReturns()
226 return $this->_extractDocumentationFromMethod('returns');
229 function _extractDocumentationFromMethod($expects_or_returns)
231 if(!in_array($expects_or_returns, array('expects', 'returns'))){
232 trigger_error(Ak
::t('Only expects and returns options are valid'), E_USER_ERROR
);
235 $parameters = array();
237 foreach ((array)$this->{$expects_or_returns} as $parameter=>$documentation){
239 if(is_numeric($parameter)){
240 $parameters[] = $documentation;
242 $parameters[] = $parameter;
243 $this->{$expects_or_returns.'_documentation'}[$i] = $documentation;
247 $this->{$expects_or_returns} = $parameters;