Base.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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_Service
  17. * @subpackage Nirvanix
  18. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @see Zend_Http_Client
  23. */
  24. require_once 'Zend/Http/Client.php';
  25. /**
  26. * @see Zend_Service_Nirvanix_Response
  27. */
  28. require_once 'Zend/Service/Nirvanix/Response.php';
  29. /**
  30. * The Nirvanix web services are split into namespaces. This is a proxy class
  31. * representing one namespace. It allows calls to the namespace to be made by
  32. * PHP object calls rather than by having to construct HTTP client requests.
  33. *
  34. * @category Zend
  35. * @package Zend_Service
  36. * @subpackage Nirvanix
  37. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  38. * @license http://framework.zend.com/license/new-bsd New BSD License
  39. */
  40. class Zend_Service_Nirvanix_Namespace_Base
  41. {
  42. /**
  43. * HTTP client instance that will be used to make calls to
  44. * the Nirvanix web services.
  45. * @var Zend_Http_Client
  46. */
  47. protected $_httpClient;
  48. /**
  49. * Host to use for calls to this Nirvanix namespace. It is possible
  50. * that the user will wish to use different hosts for different namespaces.
  51. * @var string
  52. */
  53. protected $_host = 'http://services.nirvanix.com';
  54. /**
  55. * Name of this namespace as used in the URL.
  56. * @var string
  57. */
  58. protected $_namespace = '';
  59. /**
  60. * Defaults for POST parameters. When a request to the service is to be
  61. * made, the POST parameters are merged into these. This is a convenience
  62. * feature so parameters that are repeatedly required like sessionToken
  63. * do not need to be supplied again and again by the user.
  64. *
  65. * @param array
  66. */
  67. protected $_defaults = array();
  68. /**
  69. * Class constructor.
  70. *
  71. * @param $options array Options and dependency injection
  72. */
  73. public function __construct($options = array())
  74. {
  75. if (isset($options['baseUrl'])) {
  76. $this->_host = $options['baseUrl'];
  77. }
  78. if (isset($options['namespace'])) {
  79. $this->_namespace = $options['namespace'];
  80. }
  81. if (isset($options['defaults'])) {
  82. $this->_defaults = $options['defaults'];
  83. }
  84. if (! isset($options['httpClient'])) {
  85. $options['httpClient'] = new Zend_Http_Client();
  86. }
  87. $this->_httpClient = $options['httpClient'];
  88. }
  89. /**
  90. * When a method call is made against this proxy, convert it to
  91. * an HTTP request to make against the Nirvanix REST service.
  92. *
  93. * $imfs->DeleteFiles(array('filePath' => 'foo'));
  94. *
  95. * Assuming this object was proxying the IMFS namespace, the
  96. * method call above would call the DeleteFiles command. The
  97. * POST parameters would be filePath, merged with the
  98. * $this->_defaults (containing the sessionToken).
  99. *
  100. * @param string $methodName Name of the command to call
  101. * on this namespace.
  102. * @param array $args Only the first is used and it must be
  103. * an array. It contains the POST params.
  104. *
  105. * @return Zend_Service_Nirvanix_Response
  106. */
  107. public function __call($methodName, $args)
  108. {
  109. $uri = $this->_makeUri($methodName);
  110. $this->_httpClient->setUri($uri);
  111. if (!isset($args[0]) || !is_array($args[0])) {
  112. $args[0] = array();
  113. }
  114. $params = array_merge($this->_defaults, $args[0]);
  115. $this->_httpClient->resetParameters();
  116. $this->_httpClient->setParameterPost($params);
  117. $httpResponse = $this->_httpClient->request(Zend_Http_Client::POST);
  118. return $this->_wrapResponse($httpResponse);
  119. }
  120. /**
  121. * Return the HTTP client used for this namespace. This is useful
  122. * for inspecting the last request or directly interacting with the
  123. * HTTP client.
  124. *
  125. * @return Zend_Http_Client
  126. */
  127. public function getHttpClient()
  128. {
  129. return $this->_httpClient;
  130. }
  131. /**
  132. * Make a complete URI from an RPC method name. All Nirvanix REST
  133. * service URIs use the same format.
  134. *
  135. * @param string $methodName RPC method name
  136. * @return string
  137. */
  138. protected function _makeUri($methodName)
  139. {
  140. $methodName = ucfirst($methodName);
  141. return "{$this->_host}/ws/{$this->_namespace}/{$methodName}.ashx";
  142. }
  143. /**
  144. * All Nirvanix REST service calls return an XML payload. This method
  145. * makes a Zend_Service_Nirvanix_Response from that XML payload.
  146. *
  147. * @param Zend_Http_Response $httpResponse Raw response from Nirvanix
  148. * @return Zend_Service_Nirvanix_Response Wrapped response
  149. */
  150. protected function _wrapResponse($httpResponse)
  151. {
  152. return new Zend_Service_Nirvanix_Response($httpResponse->getBody());
  153. }
  154. }