| 1 |
|
<?php |
| 2 |
|
/** |
| 3 |
|
* HTTPCached.php |
| 4 |
|
* 08-Nov-2011 |
| 5 |
|
* |
| 6 |
|
* PHP Version 5 |
| 7 |
|
* |
| 8 |
|
* @category Services |
| 9 |
|
* @package Services_OpenStreetMap |
| 10 |
|
* @author Ken Guest <kguest@php.net> |
| 11 |
|
* @license BSD http://www.opensource.org/licenses/bsd-license.php |
| 12 |
|
* @version Release: @package_version@ |
| 13 |
|
* @link http://pear.php.net/package/Services_OpenStreetMap |
| 14 |
|
*/ |
| 15 |
|
require_once 'Cache.php'; |
| 16 |
|
require_once 'Services/OpenStreetMap/Transport/HTTP.php'; |
| 17 |
|
|
| 18 |
|
/** |
| 19 |
|
* Services_OpenStreetMap_Transport_HTTPCached |
| 20 |
|
* |
| 21 |
|
* @category Services |
| 22 |
|
* @package Services_OpenStreetMap |
| 23 |
|
* @author Ken Guest <kguest@php.net> |
| 24 |
|
* @license BSD http://www.opensource.org/licenses/bsd-license.php |
| 25 |
|
* @link HTTPCached.php |
| 26 |
|
*/ |
| 27 |
|
class Services_OpenStreetMap_Transport_HTTPCached |
| 28 |
|
extends Services_OpenStreetMap_Transport_HTTP |
| 29 |
|
{ |
| 30 |
|
|
| 31 |
|
protected $cache; |
| 32 |
|
|
| 33 |
|
/** |
| 34 |
|
* __construct |
| 35 |
|
* |
| 36 |
|
* @return Services_OpenStreetMap_Transport_HTTPCached |
| 37 |
|
*/ |
| 38 |
|
public function __construct() |
| 39 |
|
{ |
| 40 |
|
parent::__construct(); |
| 41 |
|
|
| 42 |
|
$this->setCache(new Cache('file')); |
| 43 |
|
} |
| 44 |
|
|
| 45 |
|
/** |
| 46 |
|
* setCache |
| 47 |
|
* |
| 48 |
|
* @param Cache $cache Cache object |
| 49 |
|
* |
| 50 |
|
* @return Services_OpenStreetMap_Transport_HTTPCached |
| 51 |
|
*/ |
| 52 |
|
public function setCache($cache) |
| 53 |
|
{ |
| 54 |
|
$this->cache = $cache; |
| 55 |
|
return $this; |
| 56 |
|
} |
| 57 |
|
|
| 58 |
|
/** |
| 59 |
|
* Send request to OSM server and return the response. |
| 60 |
|
* |
| 61 |
|
* @param string $url URL |
| 62 |
|
* @param string $method GET (default)/POST/PUT |
| 63 |
|
* @param string $user user (optional for read-only actions) |
| 64 |
|
* @param string $password password (optional for read-only actions) |
| 65 |
|
* @param string $body body (optional) |
| 66 |
|
* @param array $post_data (optional) |
| 67 |
|
* @param array $headers (optional) |
| 68 |
|
* |
| 69 |
|
* @access public |
| 70 |
|
* @return HTTP_Request2_Response |
| 71 |
|
* @todo Consider just returning the content? |
| 72 |
|
* @throws Services_OpenStreetMap_Exception If something unexpected has |
| 73 |
|
* happened while conversing with |
| 74 |
|
* the server. |
| 75 |
|
*/ |
| 76 |
|
public function getResponse( |
| 77 |
|
$url, |
| 78 |
|
$method = HTTP_Request2::METHOD_GET, |
| 79 |
|
$user = null, |
| 80 |
|
$password = null, |
| 81 |
|
$body = null, |
| 82 |
|
array $post_data = null, |
| 83 |
|
array $headers = null |
| 84 |
|
) { |
| 85 |
|
$arguments = array( |
| 86 |
|
$url, |
| 87 |
|
$method, |
| 88 |
|
$user, |
| 89 |
|
$password, |
| 90 |
|
$body, |
| 91 |
|
implode(":", (array) $post_data), |
| 92 |
|
implode(":", (array) $headers) |
| 93 |
|
); |
| 94 |
|
$id = md5(implode(":", $arguments)); |
| 95 |
|
|
| 96 |
|
$data = $this->cache->get($id); |
| 97 |
|
if ($data) { |
| 98 |
|
$response = new HTTP_Request2_Response(); |
| 99 |
|
$response->setStatus(200); |
| 100 |
|
$response->setBody($data); |
| 101 |
|
|
| 102 |
|
return $response; |
| 103 |
|
} |
| 104 |
|
|
| 105 |
|
$response = parent::getResponse( |
| 106 |
|
$url, |
| 107 |
|
$method, |
| 108 |
|
$user, |
| 109 |
|
$password, |
| 110 |
|
$body, |
| 111 |
|
$post_data, |
| 112 |
|
$headers |
| 113 |
|
); |
| 114 |
|
|
| 115 |
|
$this->cache->save($id, $response->getBody()); |
| 116 |
|
|
| 117 |
|
return $response; |
| 118 |
|
} |
| 119 |
|
} |