PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/codeyash/bootstrap
PHP | 339 lines | 156 code | 41 blank | 142 comment | 7 complexity | 2d2bf9562d1805461e78e65ebc819dde MD5 | raw file
Possible License(s): MIT, Apache-2.0
  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. $method and $this->set_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_Driver
  130. */
  131. public function set_options(array $options)
  132. {
  133. foreach ($options as $key => $val)
  134. {
  135. $this->options[$key] = $val;
  136. }
  137. return $this;
  138. }
  139. /**
  140. * Sets a single option/value
  141. *
  142. * @param int|string $option
  143. * @param mixed $value
  144. * @return Request_Driver
  145. */
  146. public function set_option($option, $value)
  147. {
  148. return $this->set_options(array($option => $value));
  149. }
  150. /**
  151. * Add a single parameter/value or an array of parameters
  152. *
  153. * @param string|array $param
  154. * @param mixed $value
  155. * @return Request_Driver
  156. */
  157. public function add_param($param, $value = null)
  158. {
  159. if ( ! is_array($param))
  160. {
  161. $param = array($param, $value);
  162. }
  163. foreach ($param as $key => $val)
  164. {
  165. \Arr::set($this->params, $key, $val);
  166. }
  167. return $this;
  168. }
  169. /**
  170. * set a request http header
  171. *
  172. * @param string $header
  173. * @param string $header
  174. * @return Request_Driver
  175. */
  176. public function set_header($header, $content = null)
  177. {
  178. if (is_null($content))
  179. {
  180. $this->headers[] = $header;
  181. }
  182. else
  183. {
  184. $this->headers[$header] = $content;
  185. }
  186. return $this;
  187. }
  188. /**
  189. * Collect all headers and parse into consistent string
  190. *
  191. * @return array
  192. */
  193. public function get_headers()
  194. {
  195. $headers = array();
  196. foreach ($this->headers as $key => $value)
  197. {
  198. $headers[] = is_int($key) ? $value : $key.': '.$value;
  199. }
  200. return $headers;
  201. }
  202. /**
  203. * Set mime-type accept header
  204. *
  205. * @param string $mime
  206. * @return string Request_Driver
  207. */
  208. public function set_mime_type($mime)
  209. {
  210. if (array_key_exists($mime, static::$supported_formats))
  211. {
  212. $mime = static::$supported_formats[$mime];
  213. }
  214. $this->set_header('Accept', $mime);
  215. return $this;
  216. }
  217. /**
  218. * Switch auto formatting on or off
  219. *
  220. * @param bool $auto_format
  221. * @return Request_Driver
  222. */
  223. public function set_auto_format($auto_format)
  224. {
  225. $this->auto_format = (bool) $auto_format;
  226. return $this;
  227. }
  228. /**
  229. * Executes the request upon the URL
  230. *
  231. * @param array $additional_params
  232. * @param array $query_string
  233. * @return Response
  234. */
  235. abstract public function execute(array $additional_params = array());
  236. /**
  237. * Reset before doing another request
  238. *
  239. * @return Request_Driver
  240. */
  241. protected function set_defaults()
  242. {
  243. $this->options = $this->default_options;
  244. $this->params = $this->default_params;
  245. return $this;
  246. }
  247. /**
  248. * Creates the Response and optionally attempts to auto-format the output
  249. *
  250. * @param string $body
  251. * @param int $status
  252. * @param string $mime
  253. * @param array $headers
  254. * @return Response
  255. */
  256. public function set_response($body, $status, $mime = null, $headers = array())
  257. {
  258. if ($this->auto_format and array_key_exists($mime, static::$auto_detect_formats))
  259. {
  260. $body = \Format::forge($body, static::$auto_detect_formats[$mime])->to_array();
  261. }
  262. $this->response = \Response::forge($body, $status, $headers);
  263. return $this->response;
  264. }
  265. /**
  266. * Fetch the response
  267. *
  268. * @return Response
  269. */
  270. public function response()
  271. {
  272. return $this->response;
  273. }
  274. /**
  275. * Fetch the response info or a key from it
  276. *
  277. * @param string $key
  278. * @param string $default
  279. * @return mixed
  280. */
  281. public function response_info($key = null, $default = null)
  282. {
  283. if (func_num_args() == 0)
  284. {
  285. return $this->response_info;
  286. }
  287. return \Arr::get($this->response_info, $key, $default);
  288. }
  289. /**
  290. * Returns the body as a string.
  291. *
  292. * @return string
  293. */
  294. public function __toString()
  295. {
  296. return (string) $this->response();
  297. }
  298. }