/trunk/lib/AkActionWebService/Servers/AkXmlRpcServer.php

https://github.com/akelos/v1 · PHP · 174 lines · 128 code · 32 blank · 14 comment · 5 complexity · 48ca739128d98ab726b0d0b2012dd88a MD5 · raw file

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. // +----------------------------------------------------------------------+
  4. // | Akelos Framework - http://www.akelos.org |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 2002-2006, Akelos Media, S.L. & Bermi Ferrer Martinez |
  7. // | Released under the GNU Lesser General Public License, see LICENSE.txt|
  8. // +----------------------------------------------------------------------+
  9. /**
  10. * @package ActionWebservice
  11. * @subpackage Server
  12. * @author Bermi Ferrer <bermi a.t akelos c.om>
  13. * @copyright Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org
  14. * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
  15. */
  16. class AkXmlRpcServer extends AkObject
  17. {
  18. var $_ActionWebServiceServer;
  19. var $options = array();
  20. function AkXmlRpcServer(&$ActionWebServiceServer)
  21. {
  22. $this->_ActionWebServiceServer =& $ActionWebServiceServer;
  23. }
  24. function init($options = array())
  25. {
  26. $default_options = array(
  27. 'dynamic_server_class_name' => 'AkDynamicXmlRpcServer'
  28. );
  29. $this->options = array_merge($default_options, $options);
  30. if(!empty($this->_ActionWebServiceServer->_services)){
  31. foreach (array_keys($this->_ActionWebServiceServer->_services) as $name_space){
  32. $this->_addWebService($name_space, $this->_ActionWebServiceServer->_services[$name_space]);
  33. }
  34. }
  35. }
  36. function _addWebService($service_name, &$WebService)
  37. {
  38. $Apis =& $WebService->getApis();
  39. foreach (array_keys($Apis) as $k){
  40. $api_methods =& $Apis[$k]->getApiMethods();
  41. foreach (array_keys($api_methods) as $k){
  42. $api_method =& $api_methods[$k];
  43. $public_name = AkInflector::variablize($api_method->public_name);
  44. $signatures = var_export(array_merge($api_method->returns, $api_method->expects), true);
  45. $documentation = var_export($this->_getDocumentationForMethod($api_method), true);
  46. $this->_callbacks[] = "
  47. \$this->addCallback(
  48. '$service_name.$public_name',
  49. 'this:_{$service_name}_{$api_method->name}_call',
  50. $signatures,
  51. $documentation
  52. );
  53. ";
  54. $this->_methods[] = "
  55. function _{$service_name}_{$api_method->name}_call()
  56. {
  57. \$args = func_get_args();
  58. return call_user_func_array(array(&\$this->_{$service_name}, '".$api_method->name."'), (array)\$args[0]);
  59. }
  60. ";
  61. }
  62. }
  63. }
  64. function _getDocumentationForMethod($ApiMethod)
  65. {
  66. $doc = !empty($ApiMethod->documentation)? $ApiMethod->documentation."\n" : '';
  67. foreach (array('expects', 'returns') as $expects_or_returns){
  68. if(!empty($ApiMethod->{$expects_or_returns})){
  69. foreach ($ApiMethod->{$expects_or_returns} as $k=>$type){
  70. $doc .= "\n".(
  71. $expects_or_returns == 'expects' ?
  72. Ak::t(AkInflector::ordinalize($k+1)).' parameter as' : 'Returns'
  73. )." $type:";
  74. if(!empty($ApiMethod->{$expects_or_returns.'_documentation'}[$k])){
  75. $doc .= ' '.$ApiMethod->{$expects_or_returns.'_documentation'}[$k];
  76. }
  77. }
  78. }
  79. }
  80. return $doc;
  81. }
  82. function _generateServerClassCode()
  83. {
  84. $this->_serverClassCode = "<?php
  85. class {$this->options['dynamic_server_class_name']} extends AkIxrInstrospectionServer
  86. {
  87. function {$this->options['dynamic_server_class_name']}()
  88. {
  89. \$this->IXR_IntrospectionServer();
  90. }
  91. ";
  92. $this->_serverClassCode .= join("\n", $this->_methods);
  93. $this->_serverClassCode .= '
  94. function init()
  95. {
  96. '. join("\n", $this->_callbacks).'
  97. $this->serve();
  98. }
  99. }
  100. ?>';
  101. }
  102. function serve()
  103. {
  104. $this->_generateServerClassCode();
  105. eval('?>'.$this->_serverClassCode.'<?php ');
  106. $Server =& new $this->options['dynamic_server_class_name'];
  107. $this->_linkWebServicesToServer($Server);
  108. $Server->init();
  109. }
  110. function _linkWebServicesToServer(&$Server)
  111. {
  112. if(!empty($this->_ActionWebServiceServer->_services)){
  113. foreach (array_keys($this->_ActionWebServiceServer->_services) as $name_space){
  114. $Server->{'_'.$name_space} =& $this->_ActionWebServiceServer->_services[$name_space];
  115. }
  116. }
  117. }
  118. }
  119. require_once(AK_VENDOR_DIR.DS.'incutio'.DS.'IXR_Library.inc.php');
  120. class AkIxrInstrospectionServer extends IXR_IntrospectionServer
  121. {
  122. var $_services = array();
  123. function output($xml)
  124. {
  125. $xml = '<?xml version="1.0"?>'."\n".$xml;
  126. $length = strlen($xml);
  127. header('Connection: close');
  128. header('Content-Length: '.$length);
  129. header('Content-Type: text/xml');
  130. header('Date: '.date('r'));
  131. echo $xml;
  132. exit;
  133. }
  134. function _addService($service_name, &$ServiceInstance)
  135. {
  136. $this->_services[$service_name] =& $ServiceInstance;
  137. }
  138. }
  139. ?>