PageRenderTime 37ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Zend/Service/Nirvanix/Namespace/Base.php

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