ServerIntrospection.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_XmlRpc
  17. * @subpackage Client
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * Wraps the XML-RPC system.* introspection methods
  23. *
  24. * @category Zend
  25. * @package Zend_XmlRpc
  26. * @subpackage Client
  27. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  28. * @license http://framework.zend.com/license/new-bsd New BSD License
  29. */
  30. class Zend_XmlRpc_Client_ServerIntrospection
  31. {
  32. /**
  33. * @var Zend_XmlRpc_Client_ServerProxy
  34. */
  35. private $_system = null;
  36. /**
  37. * @param Zend_XmlRpc_Client $client
  38. */
  39. public function __construct(Zend_XmlRpc_Client $client)
  40. {
  41. $this->_system = $client->getProxy('system');
  42. }
  43. /**
  44. * Returns the signature for each method on the server,
  45. * autodetecting whether system.multicall() is supported and
  46. * using it if so.
  47. *
  48. * @return array
  49. */
  50. public function getSignatureForEachMethod()
  51. {
  52. $methods = $this->listMethods();
  53. require_once 'Zend/XmlRpc/Client/FaultException.php';
  54. try {
  55. $signatures = $this->getSignatureForEachMethodByMulticall($methods);
  56. } catch (Zend_XmlRpc_Client_FaultException $e) {
  57. // degrade to looping
  58. }
  59. if (empty($signatures)) {
  60. $signatures = $this->getSignatureForEachMethodByLooping($methods);
  61. }
  62. return $signatures;
  63. }
  64. /**
  65. * Attempt to get the method signatures in one request via system.multicall().
  66. * This is a boxcar feature of XML-RPC and is found on fewer servers. However,
  67. * can significantly improve performance if present.
  68. *
  69. * @param array $methods
  70. * @return array array(array(return, param, param, param...))
  71. */
  72. public function getSignatureForEachMethodByMulticall($methods = null)
  73. {
  74. if ($methods === null) {
  75. $methods = $this->listMethods();
  76. }
  77. $multicallParams = array();
  78. foreach ($methods as $method) {
  79. $multicallParams[] = array('methodName' => 'system.methodSignature',
  80. 'params' => array($method));
  81. }
  82. $serverSignatures = $this->_system->multicall($multicallParams);
  83. if (! is_array($serverSignatures)) {
  84. $type = gettype($serverSignatures);
  85. $error = "Multicall return is malformed. Expected array, got $type";
  86. require_once 'Zend/XmlRpc/Client/IntrospectException.php';
  87. throw new Zend_XmlRpc_Client_IntrospectException($error);
  88. }
  89. if (count($serverSignatures) != count($methods)) {
  90. $error = 'Bad number of signatures received from multicall';
  91. require_once 'Zend/XmlRpc/Client/IntrospectException.php';
  92. throw new Zend_XmlRpc_Client_IntrospectException($error);
  93. }
  94. // Create a new signatures array with the methods name as keys and the signature as value
  95. $signatures = array();
  96. foreach ($serverSignatures as $i => $signature) {
  97. $signatures[$methods[$i]] = $signature;
  98. }
  99. return $signatures;
  100. }
  101. /**
  102. * Get the method signatures for every method by
  103. * successively calling system.methodSignature
  104. *
  105. * @param array $methods
  106. * @return array
  107. */
  108. public function getSignatureForEachMethodByLooping($methods = null)
  109. {
  110. if ($methods === null) {
  111. $methods = $this->listMethods();
  112. }
  113. $signatures = array();
  114. foreach ($methods as $method) {
  115. $signatures[$method] = $this->getMethodSignature($method);
  116. }
  117. return $signatures;
  118. }
  119. /**
  120. * Call system.methodSignature() for the given method
  121. *
  122. * @param array $method
  123. * @return array array(array(return, param, param, param...))
  124. */
  125. public function getMethodSignature($method)
  126. {
  127. $signature = $this->_system->methodSignature($method);
  128. return $signature;
  129. }
  130. /**
  131. * Call system.listMethods()
  132. *
  133. * @param array $method
  134. * @return array array(method, method, method...)
  135. */
  136. public function listMethods()
  137. {
  138. return $this->_system->listMethods();
  139. }
  140. }