Skip to content

Commit

Permalink
Adding default simple implementation for cache, it adheres to the hea…
Browse files Browse the repository at this point in the history
…ders sent by TMDB.
  • Loading branch information
wtfzdotnet committed Mar 1, 2014
1 parent a3a298f commit 5caa082
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 13 deletions.
10 changes: 1 addition & 9 deletions examples/movies/model/cache/basic_doctrine_cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
24 changes: 24 additions & 0 deletions examples/movies/model/cache/built-in.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* This file is part of the Tmdb PHP API created by Michael Roterman.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Tmdb
* @author Michael Roterman <[email protected]>
* @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();
113 changes: 109 additions & 4 deletions lib/Tmdb/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*
Expand All @@ -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
*
Expand Down Expand Up @@ -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;
}
}
38 changes: 38 additions & 0 deletions lib/Tmdb/HttpClient/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 5caa082

Please sign in to comment.