PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/fuel/core/classes/request/soap.php

https://bitbucket.org/codeyash/bootstrap
PHP | 265 lines | 150 code | 30 blank | 85 comment | 12 complexity | 3b92558bf39e1ae9a25b251dcdc0bc05 MD5 | raw file
Possible License(s): MIT, Apache-2.0
  1. <?php
  2. namespace Fuel\Core;
  3. class Request_Soap extends \Request_Driver
  4. {
  5. protected static $wsdl_settings = array('wsdl', 'classmap', 'cache_wsdl');
  6. protected static $non_wsdl_settings = array('location', 'uri', 'style', 'use');
  7. protected static $generic_settings = array(
  8. 'soap_version', 'compression', 'encoding', 'trace', 'connection_timeout',
  9. 'typemap', 'user_agent', 'stream_context', 'features',
  10. );
  11. /**
  12. * @var \SoapClient holds the SoapClient object used for the connection
  13. */
  14. protected $connection;
  15. /**
  16. * @var string function to call
  17. */
  18. protected $function = '';
  19. /**
  20. * Extends parent constructor to detect availability of cURL
  21. *
  22. * @param string $resource
  23. * @param array $options
  24. * @throws \RuntimeException
  25. */
  26. public function __construct($resource, array $options)
  27. {
  28. // check if we have libcurl available
  29. if ( ! class_exists('SoapClient'))
  30. {
  31. throw new \RuntimeException('Your PHP installation doesn\'t have Soap enabled. Rebuild PHP with --enable-soap');
  32. }
  33. logger(\Fuel::L_INFO, 'Creating a new SOAP Request with URI = "'.$resource.'"', __METHOD__);
  34. // If authentication is enabled use it
  35. if ( ! empty($options['user']) and ! empty($options['pass']))
  36. {
  37. $this->set_option('login', $options['user']);
  38. $this->set_option('password', $options['pass']);
  39. }
  40. // WSDL-mode only options
  41. if ( ! empty($resource))
  42. {
  43. foreach (static::$wsdl_settings as $setting)
  44. {
  45. isset($options[$setting]) and $this->set_option($setting, $options[$setting]);
  46. }
  47. }
  48. // non-WSDL-mode only options
  49. else
  50. {
  51. $resource = null;
  52. if ( ! isset($options['location']) or ! isset($options['uri']))
  53. {
  54. throw new \RequestException('The keys "location" and "uri" are required in non-WSDL mode.');
  55. }
  56. foreach (static::$non_wsdl_settings as $setting)
  57. {
  58. isset($options[$setting]) and $this->set_option($setting, $options[$setting]);
  59. }
  60. }
  61. foreach (static::$generic_settings as $setting)
  62. {
  63. isset($options[$setting]) and $this->set_option($setting, $options[$setting]);
  64. }
  65. // make it always throw exceptions
  66. $this->set_option('exceptions', true);
  67. parent::__construct($resource, $options);
  68. }
  69. /**
  70. * Set the function to execute on the SoapClient
  71. *
  72. * @param string $function
  73. * @return Request_Soap
  74. */
  75. public function set_function($function)
  76. {
  77. $this->function = $function;
  78. return $this;
  79. }
  80. /**
  81. * Fetch the connection, create if necessary
  82. *
  83. * @return \SoapClient
  84. */
  85. protected function connection()
  86. {
  87. if (empty($this->connection))
  88. {
  89. $this->connection = new \SoapClient($this->resource, $this->options);
  90. }
  91. return $this->connection;
  92. }
  93. public function execute(array $additional_params = array())
  94. {
  95. if (empty($this->function))
  96. {
  97. throw new \RequestException('No function set to execute on the Soap request.');
  98. }
  99. $additional_params and $this->params = \Arr::merge($this->params, $additional_params);
  100. // Execute the request & and hide all output
  101. try
  102. {
  103. $body = $this->connection()->__soapCall($this->function, $this->params, array(), $this->get_headers(), $headers);
  104. $this->response_info = $headers;
  105. $mime = isset($this->headers['Accept']) ? $this->headers['Accept'] : null;
  106. $this->set_response($body, $this->response_info('http_code', 200), $mime, $headers);
  107. $this->set_defaults();
  108. return $this;
  109. }
  110. catch (\SoapFault $e)
  111. {
  112. $this->set_defaults();
  113. throw new \RequestException($e->getMessage(), $e->getCode(), $e);
  114. }
  115. }
  116. /**
  117. * Extends parent to reset headers as well
  118. *
  119. * @return Request_Soap
  120. */
  121. protected function set_defaults()
  122. {
  123. parent::set_defaults();
  124. $this->function = '';
  125. return $this;
  126. }
  127. /**
  128. * Get functions defined in WSDL
  129. *
  130. * @return array
  131. * @throws \RequestException
  132. */
  133. public function get_functions()
  134. {
  135. if ( ! $this->resource)
  136. {
  137. throw new \RequestException('SOAP get functions not available in non-WSDL mode.');
  138. }
  139. return $this->connection()->__getFunctions();
  140. }
  141. /**
  142. * Get last request XML
  143. *
  144. * @return string
  145. * @throws \RequestException
  146. */
  147. public function get_request_xml()
  148. {
  149. if (empty($this->options['trace']))
  150. {
  151. throw new \RequestException('The "trace" option must be true to be able to get the last request.');
  152. }
  153. return $this->connection()->__getLastRequest();
  154. }
  155. /**
  156. * Get last request headers
  157. *
  158. * @return string
  159. * @throws \RequestException
  160. */
  161. public function get_request_headers()
  162. {
  163. if (empty($this->options['trace']))
  164. {
  165. throw new \RequestException('The "trace" option must be true to be able to get the last request headers.');
  166. }
  167. return $this->connection()->__getLastRequestHeaders();
  168. }
  169. /**
  170. * Get last response XML
  171. *
  172. * @return string
  173. * @throws \RequestException
  174. */
  175. public function get_response_xml()
  176. {
  177. if (empty($this->options['trace']))
  178. {
  179. throw new \RequestException('The "trace" option must be true to be able to get the last response.');
  180. }
  181. return $this->connection()->__getLastResponse();
  182. }
  183. /**
  184. * Get last response headers
  185. *
  186. * @return string
  187. * @throws \RequestException
  188. */
  189. public function get_response_headers()
  190. {
  191. if (empty($this->options['trace']))
  192. {
  193. throw new \RequestException('The "trace" option must be true to be able to get the last response headers.');
  194. }
  195. return $this->connection()->__getLastResponseHeaders();
  196. }
  197. /**
  198. * Get last response headers
  199. *
  200. * @return array
  201. * @throws \RequestException
  202. */
  203. public function get_types()
  204. {
  205. if ( ! $this->resource)
  206. {
  207. throw new \RequestException('SOAP get types not available in non-WSDL mode.');
  208. }
  209. return $this->connection()->__getTypes();
  210. }
  211. /**
  212. * Set cookie for subsequent requests
  213. *
  214. * @param string $name
  215. * @param string $value
  216. * @return void
  217. * @throws \RequestException
  218. */
  219. public function set_cookie($name, $value = null)
  220. {
  221. is_null($value)
  222. ? $this->connection()->__setCookie($name)
  223. : $this->connection()->__setCookie($name, $value);
  224. }
  225. /**
  226. * Change the endpoint location
  227. *
  228. * @param string $location
  229. * @return string the old endpoint
  230. */
  231. public function set_location($location)
  232. {
  233. $this->connection()->__setLocation($location);
  234. }
  235. }