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

Language PHP Lines 266
MD5 Hash 3b92558bf39e1ae9a25b251dcdc0bc05 Estimated Cost $3,660 (why?)
Repository https://bitbucket.org/codeyash/bootstrap.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?php

namespace Fuel\Core;

class Request_Soap extends \Request_Driver
{
	protected static $wsdl_settings = array('wsdl', 'classmap', 'cache_wsdl');
	protected static $non_wsdl_settings = array('location', 'uri', 'style', 'use');
	protected static $generic_settings = array(
		'soap_version', 'compression', 'encoding', 'trace', 'connection_timeout',
		'typemap', 'user_agent', 'stream_context', 'features',
	);

	/**
	 * @var  \SoapClient	holds the SoapClient object used for the connection
	 */
	protected $connection;

	/**
	 * @var  string  function to call
	 */
	protected $function = '';

	/**
	 * Extends parent constructor to detect availability of cURL
	 *
	 * @param   string  $resource
	 * @param   array   $options
	 * @throws  \RuntimeException
	 */
	public function __construct($resource, array $options)
	{
		// check if we have libcurl available
		if ( ! class_exists('SoapClient'))
		{
			throw new \RuntimeException('Your PHP installation doesn\'t have Soap enabled. Rebuild PHP with --enable-soap');
		}

		logger(\Fuel::L_INFO, 'Creating a new SOAP Request with URI = "'.$resource.'"', __METHOD__);

		// If authentication is enabled use it
		if ( ! empty($options['user']) and ! empty($options['pass']))
		{
			$this->set_option('login', $options['user']);
			$this->set_option('password', $options['pass']);
		}

		// WSDL-mode only options
		if ( ! empty($resource))
		{
			foreach (static::$wsdl_settings as $setting)
			{
				isset($options[$setting]) and $this->set_option($setting, $options[$setting]);
			}
		}
		// non-WSDL-mode only options
		else
		{
			$resource = null;

			if ( ! isset($options['location']) or ! isset($options['uri']))
			{
				throw new \RequestException('The keys "location" and "uri" are required in non-WSDL mode.');
			}

			foreach (static::$non_wsdl_settings as $setting)
			{
				isset($options[$setting]) and $this->set_option($setting, $options[$setting]);
			}
		}

		foreach (static::$generic_settings as $setting)
		{
			isset($options[$setting]) and $this->set_option($setting, $options[$setting]);
		}

		// make it always throw exceptions
		$this->set_option('exceptions', true);

		parent::__construct($resource, $options);
	}

	/**
	 * Set the function to execute on the SoapClient
	 *
	 * @param   string  $function
	 * @return  Request_Soap
	 */
	public function set_function($function)
	{
		$this->function = $function;
		return $this;
	}

	/**
	 * Fetch the connection, create if necessary
	 *
	 * @return  \SoapClient
	 */
	protected function connection()
	{
		if (empty($this->connection))
		{
			$this->connection = new \SoapClient($this->resource, $this->options);
		}

		return $this->connection;
	}

	public function execute(array $additional_params = array())
	{
		if (empty($this->function))
		{
			throw new \RequestException('No function set to execute on the Soap request.');
		}

		$additional_params and $this->params = \Arr::merge($this->params, $additional_params);

		// Execute the request & and hide all output
		try
		{
			$body = $this->connection()->__soapCall($this->function, $this->params, array(), $this->get_headers(), $headers);
			$this->response_info = $headers;
			$mime = isset($this->headers['Accept']) ? $this->headers['Accept'] : null;
			$this->set_response($body, $this->response_info('http_code', 200), $mime, $headers);

			$this->set_defaults();
			return $this;
		}
		catch (\SoapFault $e)
		{
			$this->set_defaults();
			throw new \RequestException($e->getMessage(), $e->getCode(), $e);
		}
	}

	/**
	 * Extends parent to reset headers as well
	 *
	 * @return  Request_Soap
	 */
	protected function set_defaults()
	{
		parent::set_defaults();
		$this->function = '';

		return $this;
	}

	/**
	 * Get functions defined in WSDL
	 *
	 * @return  array
	 * @throws  \RequestException
	 */
	public function get_functions()
	{
		if ( ! $this->resource)
		{
			throw new \RequestException('SOAP get functions not available in non-WSDL mode.');
		}
		return $this->connection()->__getFunctions();
	}

	/**
	 * Get last request XML
	 *
	 * @return  string
	 * @throws  \RequestException
	 */
	public function get_request_xml()
	{
		if (empty($this->options['trace']))
		{
			throw new \RequestException('The "trace" option must be true to be able to get the last request.');
		}
		return $this->connection()->__getLastRequest();
	}

	/**
	 * Get last request headers
	 *
	 * @return  string
	 * @throws  \RequestException
	 */
	public function get_request_headers()
	{
		if (empty($this->options['trace']))
		{
			throw new \RequestException('The "trace" option must be true to be able to get the last request headers.');
		}
		return $this->connection()->__getLastRequestHeaders();
	}

	/**
	 * Get last response XML
	 *
	 * @return  string
	 * @throws  \RequestException
	 */
	public function get_response_xml()
	{
		if (empty($this->options['trace']))
		{
			throw new \RequestException('The "trace" option must be true to be able to get the last response.');
		}
		return $this->connection()->__getLastResponse();
	}

	/**
	 * Get last response headers
	 *
	 * @return  string
	 * @throws  \RequestException
	 */
	public function get_response_headers()
	{
		if (empty($this->options['trace']))
		{
			throw new \RequestException('The "trace" option must be true to be able to get the last response headers.');
		}
		return $this->connection()->__getLastResponseHeaders();
	}

	/**
	 * Get last response headers
	 *
	 * @return  array
	 * @throws  \RequestException
	 */
	public function get_types()
	{
		if ( ! $this->resource)
		{
			throw new \RequestException('SOAP get types not available in non-WSDL mode.');
		}
		return $this->connection()->__getTypes();
	}

	/**
	 * Set cookie for subsequent requests
	 *
	 * @param   string  $name
	 * @param   string  $value
	 * @return  void
	 * @throws  \RequestException
	 */
	public function set_cookie($name, $value = null)
	{
		is_null($value)
			? $this->connection()->__setCookie($name)
			: $this->connection()->__setCookie($name, $value);
	}

	/**
	 * Change the endpoint location
	 *
	 * @param   string  $location
	 * @return  string  the old endpoint
	 */
	public function set_location($location)
	{
		$this->connection()->__setLocation($location);
	}
}
Back to Top