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

/fuel/category_tool/fuel/core/classes/request/driver.php

https://github.com/connvoi/dev
PHP | 335 lines | 153 code | 40 blank | 142 comment | 7 complexity | ee486514a2c2e92c6c998a634e9d36de MD5 | raw file
Possible License(s): MIT, BSD-3-Clause
  1. <?php
  2. namespace Fuel\Core;
  3. class RequestException extends \HttpNotFoundException {}
  4. class RequestStatusException extends \RequestException {}
  5. abstract class Request_Driver
  6. {
  7. /**
  8. * Forge
  9. *
  10. * @param string $resource
  11. * @param array $options
  12. * @return Request_Driver
  13. */
  14. public static function forge($resource, array $options = array(), $method = null)
  15. {
  16. return new static($resource, $options, $method);
  17. }
  18. /**
  19. * @var string URL resource to perform requests upon
  20. */
  21. protected $resource = '';
  22. /**
  23. * @var array parameters to pass
  24. */
  25. protected $params = array();
  26. /**
  27. * @var array params set during object creation are handled as the defaults
  28. */
  29. protected $default_params = array();
  30. /**
  31. * @var array driver specific options
  32. */
  33. protected $options = array();
  34. /**
  35. * @var array options set during object creation are handled as the defaults
  36. */
  37. protected $default_options = array();
  38. /**
  39. * @var array http headers set for the request
  40. */
  41. protected $headers = array();
  42. /**
  43. * @var Response the response object after execute
  44. */
  45. protected $response;
  46. /**
  47. * @var array info about the response
  48. */
  49. protected $response_info = array();
  50. /**
  51. * @var bool whether to attempt auto-formatting the response
  52. */
  53. protected $auto_format = true;
  54. /**
  55. * @var string $method request method
  56. */
  57. protected $method = null;
  58. /**
  59. * @var array supported response formats
  60. */
  61. protected static $supported_formats = array(
  62. 'xml' => 'application/xml',
  63. 'json' => 'application/json',
  64. 'serialize' => 'application/vnd.php.serialized',
  65. 'php' => 'text/plain',
  66. 'csv' => 'text/csv',
  67. );
  68. /**
  69. * @var array mimetype format autodetection
  70. */
  71. protected static $auto_detect_formats = array(
  72. 'application/xml' => 'xml',
  73. 'text/xml' => 'xml',
  74. 'application/json' => 'json',
  75. 'text/json' => 'json',
  76. 'text/csv' => 'csv',
  77. 'application/csv' => 'csv',
  78. 'application/vnd.php.serialized' => 'serialize',
  79. );
  80. public function __construct($resource, array $options, $method = null)
  81. {
  82. $this->resource = $resource;
  83. $this->method = $method;
  84. foreach ($options as $key => $value)
  85. {
  86. if (method_exists($this, 'set_'.$key))
  87. {
  88. $this->{'set_'.$key}($value);
  89. }
  90. }
  91. $this->default_options = $this->options;
  92. $this->default_params = $this->params;
  93. }
  94. /**
  95. * Sets the request method.
  96. *
  97. * @param string $method request method
  98. * @return object current instance
  99. */
  100. public function set_method($method)
  101. {
  102. $this->method = strtoupper($method);
  103. return $this;
  104. }
  105. /**
  106. * Returns the request method.
  107. *
  108. * @return string request method
  109. */
  110. public function get_method()
  111. {
  112. return $this->method;
  113. }
  114. /**
  115. * Set the parameters to pass with the request
  116. *
  117. * @param array $params
  118. * @return Request_Driver
  119. */
  120. public function set_params($params)
  121. {
  122. $this->params = $params;
  123. return $this;
  124. }
  125. /**
  126. * Sets options on the driver
  127. *
  128. * @param array $options
  129. * @return Request_Curl
  130. */
  131. public function set_options(array $options)
  132. {
  133. $this->options = $options;
  134. return $this;
  135. }
  136. /**
  137. * Sets a single option/value
  138. *
  139. * @param int|string $option
  140. * @param mixed $value
  141. * @return Request_Curl
  142. */
  143. public function set_option($option, $value)
  144. {
  145. return $this->set_options(array($option => $value));
  146. }
  147. /**
  148. * Add a single parameter/value or an array of parameters
  149. *
  150. * @param string|array $param
  151. * @param mixed $value
  152. * @return Request_Driver
  153. */
  154. public function add_param($param, $value = null)
  155. {
  156. if ( ! is_array($param))
  157. {
  158. $param = array($param, $value);
  159. }
  160. foreach ($param as $key => $val)
  161. {
  162. \Arr::set($this->params, $key, $val);
  163. }
  164. return $this;
  165. }
  166. /**
  167. * set a request http header
  168. *
  169. * @param string $header
  170. * @param string $header
  171. * @return Request_Driver
  172. */
  173. public function set_header($header, $content = null)
  174. {
  175. if (is_null($content))
  176. {
  177. $this->headers[] = $header;
  178. }
  179. else
  180. {
  181. $this->headers[$header] = $content;
  182. }
  183. return $this;
  184. }
  185. /**
  186. * Collect all headers and parse into consistent string
  187. *
  188. * @return array
  189. */
  190. public function get_headers()
  191. {
  192. $headers = array();
  193. foreach ($this->headers as $key => $value)
  194. {
  195. $headers[] = is_int($key) ? $value : $key.': '.$value;
  196. }
  197. return $headers;
  198. }
  199. /**
  200. * Set mime-type accept header
  201. *
  202. * @param string $mime
  203. * @return string Request_Driver
  204. */
  205. public function set_mime_type($mime)
  206. {
  207. if (array_key_exists($mime, static::$supported_formats))
  208. {
  209. $mime = static::$supported_formats[$mime];
  210. }
  211. $this->set_header('Accept', $mime);
  212. return $this;
  213. }
  214. /**
  215. * Switch auto formatting on or off
  216. *
  217. * @param bool $auto_format
  218. * @return Request_Driver
  219. */
  220. public function set_auto_format($auto_format)
  221. {
  222. $this->auto_format = (bool) $auto_format;
  223. return $this;
  224. }
  225. /**
  226. * Executes the request upon the URL
  227. *
  228. * @param array $additional_params
  229. * @param array $query_string
  230. * @return Response
  231. */
  232. abstract public function execute(array $additional_params = array());
  233. /**
  234. * Reset before doing another request
  235. *
  236. * @return Request_Curl
  237. */
  238. protected function set_defaults()
  239. {
  240. $this->options = $this->default_options;
  241. $this->params = $this->default_params;
  242. return $this;
  243. }
  244. /**
  245. * Creates the Response and optionally attempts to auto-format the output
  246. *
  247. * @param string $body
  248. * @param int $status
  249. * @param string $mime
  250. * @param array $headers
  251. * @return Response
  252. */
  253. public function set_response($body, $status, $mime = null, $headers = array())
  254. {
  255. if ($this->auto_format and array_key_exists($mime, static::$auto_detect_formats))
  256. {
  257. $body = \Format::forge($body, static::$auto_detect_formats[$mime])->to_array();
  258. }
  259. $this->response = \Response::forge($body, $status, $headers);
  260. return $this->response;
  261. }
  262. /**
  263. * Fetch the response
  264. *
  265. * @return Response
  266. */
  267. public function response()
  268. {
  269. return $this->response;
  270. }
  271. /**
  272. * Fetch the response info or a key from it
  273. *
  274. * @param string $key
  275. * @param string $default
  276. * @return mixed
  277. */
  278. public function response_info($key = null, $default = null)
  279. {
  280. if (func_num_args() == 0)
  281. {
  282. return $this->response_info;
  283. }
  284. return \Arr::get($this->response_info, $key, $default);
  285. }
  286. /**
  287. * Returns the body as a string.
  288. *
  289. * @return string
  290. */
  291. public function __toString()
  292. {
  293. return (string) $this->response();
  294. }
  295. }