diff --git a/examples/movies/model/cache/basic_doctrine_cache.php b/examples/movies/model/cache/basic_doctrine_cache.php index 7b61b290..3d6d3eba 100644 --- a/examples/movies/model/cache/basic_doctrine_cache.php +++ b/examples/movies/model/cache/basic_doctrine_cache.php @@ -16,15 +16,7 @@ $token = new \Tmdb\ApiToken(TMDB_API_KEY); $client = new \Tmdb\Client($token); -$cachePlugin = new \Guzzle\Plugin\Cache\CachePlugin(array( - 'storage' => new \Guzzle\Plugin\Cache\DefaultCacheStorage( - new \Guzzle\Cache\DoctrineCacheAdapter( - new \Doctrine\Common\Cache\FilesystemCache('/tmp/_php-tmdb-api') - ) - ) -)); - -$client->getHttpClient()->addSubscriber($cachePlugin); +$client->setCaching(true); $repository = new \Tmdb\Repository\MovieRepository($client); $movie = $repository->load(87421); diff --git a/examples/movies/model/cache/built-in.php b/examples/movies/model/cache/built-in.php new file mode 100644 index 00000000..50fea79c --- /dev/null +++ b/examples/movies/model/cache/built-in.php @@ -0,0 +1,24 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +require_once '../../../../vendor/autoload.php'; +require_once '../../../../apikey.php'; + +$token = new \Tmdb\ApiToken(TMDB_API_KEY); +$client = new \Tmdb\Client($token); + +$client->setCaching(true, '/tmp/php-tmdb-api'); + +$repository = new \Tmdb\Repository\MovieRepository($client); +$movie = $repository->load(87421); + +echo $movie->getTitle(); diff --git a/lib/Tmdb/Client.php b/lib/Tmdb/Client.php index a2be0d38..a00ce160 100644 --- a/lib/Tmdb/Client.php +++ b/lib/Tmdb/Client.php @@ -12,8 +12,13 @@ */ namespace Tmdb; +use Guzzle\Cache\DoctrineCacheAdapter; +use Guzzle\Common\Exception\RuntimeException; +use Guzzle\Common\HasDispatcherInterface; use Guzzle\Http\Client as GuzzleClient; use Guzzle\Http\ClientInterface; +use Guzzle\Plugin\Cache\CachePlugin; +use Guzzle\Plugin\Cache\DefaultCacheStorage; use Tmdb\HttpClient\HttpClient; use Tmdb\HttpClient\HttpClientInterface; use Tmdb\ApiToken as Token; @@ -70,6 +75,20 @@ class Client */ private $httpClient; + /** + * Stores the cache path + * + * @var string + */ + private $cachePath; + + /** + * Stores wether the cache is enabled or not + * + * @var boolean + */ + private $cacheEnabled = false; + /** * Construct our client * @@ -81,20 +100,67 @@ public function __construct(Token $token, ClientInterface $httpClient = null, $s { $this->setToken($token); $this->setSecure($secure); + $this->constructHttpClient($httpClient); + } + /** + * Construct the http client + * + * @param ClientInterface $httpClient + * @throws RuntimeException + * @return void + */ + private function constructHttpClient(ClientInterface $httpClient = null) + { $httpClient = $httpClient ?: new GuzzleClient($this->getBaseUrl()); - if ($httpClient instanceof \Guzzle\Common\HasDispatcherInterface) { - $apiTokenPlugin = new ApiTokenPlugin($token); - $httpClient->addSubscriber($apiTokenPlugin); - + if ($httpClient instanceof HasDispatcherInterface) { $acceptJsonHeaderPlugin = new AcceptJsonHeaderPlugin(); $httpClient->addSubscriber($acceptJsonHeaderPlugin); + + if ($this->getToken() instanceof Token) { + $apiTokenPlugin = new ApiTokenPlugin($this->getToken()); + $httpClient->addSubscriber($apiTokenPlugin); + } + + if ($this->cacheEnabled && !empty($this->cachePath)) { + if (!class_exists('Doctrine\Common\Cache\FilesystemCache')) { + throw new RuntimeException( + 'Could not find the doctrine cache library, have you added doctrone-cache to your composer.json?' + ); + } + + $cachePlugin = new CachePlugin(array( + 'storage' => new DefaultCacheStorage( + new DoctrineCacheAdapter( + new \Doctrine\Common\Cache\FilesystemCache($this->cachePath) + ) + ) + ) + ); + + $httpClient->addSubscriber($cachePlugin); + } + + if ($this->getSessionToken() instanceof SessionToken) { + $sessionTokenPlugin = new SessionTokenPlugin($this->getSessionToken()); + $httpClient->getClient()->addSubscriber($sessionTokenPlugin); + } } $this->httpClient = new HttpClient($this->getBaseUrl(), array(), $httpClient); } + /** + * Add the token subscriber + * + * @return Token + */ + public function getToken() + { + return $this->token !== null ? $this->token : null; + } + /** * Add the token subscriber * @@ -356,4 +422,43 @@ public function getSessionToken() { return $this->sessionToken; } + + /** + * @return boolean + */ + public function getCacheEnabled() + { + return $this->cacheEnabled; + } + + /** + * Set cache path + * + * You could simply pass an empty string to let the sys_get_temp_dir be used + * + * @param boolean $enabled + * @param string $path + * @return $this + */ + public function setCaching($enabled = true, $path = null) + { + $this->cacheEnabled = $enabled; + $this->cachePath = (null === $path) ? + sys_get_temp_dir() . '/php-tmdb-api' : + $path + ; + + // @todo doesn't cover a custom client, would require un-registering all known plugins + $this->constructHttpClient(); + + return $this; + } + + /** + * @return string + */ + public function getCachePath() + { + return $this->cachePath; + } } diff --git a/lib/Tmdb/HttpClient/HttpClient.php b/lib/Tmdb/HttpClient/HttpClient.php index 5792ab36..8618e0c3 100644 --- a/lib/Tmdb/HttpClient/HttpClient.php +++ b/lib/Tmdb/HttpClient/HttpClient.php @@ -199,4 +199,42 @@ public function getClient() { return $this->client; } + + /** + * @param \Guzzle\Http\Message\Request $lastRequest + * @return $this + */ + public function setLastRequest($lastRequest) + { + $this->lastRequest = $lastRequest; + + return $this; + } + + /** + * @return \Guzzle\Http\Message\Request + */ + public function getLastRequest() + { + return $this->lastRequest; + } + + /** + * @param \Guzzle\Http\Message\Response $lastResponse + * @return $this + */ + public function setLastResponse($lastResponse) + { + $this->lastResponse = $lastResponse; + + return $this; + } + + /** + * @return \Guzzle\Http\Message\Response + */ + public function getLastResponse() + { + return $this->lastResponse; + } }