From 79f6dbb2a370c2e50c11a98c944fe27287219ff0 Mon Sep 17 00:00:00 2001 From: alnutile Date: Tue, 27 May 2014 20:47:34 -0400 Subject: [PATCH] place the class into a psr-0 setup so that other modules can use that method of using the class --- composer.json | 7 +- github_api.info | 2 - github_api.module | 2 +- ...hubApiHttpClientCacheDrupalNativeCache.php | 98 +++++++++++++++++++ 4 files changed, 105 insertions(+), 4 deletions(-) create mode 100644 src/GithubApi/DrupalGithubApiHttpClientCacheDrupalNativeCache.php diff --git a/composer.json b/composer.json index 2be2dfb..d5263bc 100644 --- a/composer.json +++ b/composer.json @@ -8,5 +8,10 @@ "url": "https://github.com/KnpLabs/php-github-api" } ], + "autoload": { + "psr-0": { + "GithubApi\\": "src/" + } + }, "minimum-stability": "dev" -} +} \ No newline at end of file diff --git a/github_api.info b/github_api.info index 6a0ba5d..e25bf1b 100644 --- a/github_api.info +++ b/github_api.info @@ -5,5 +5,3 @@ core = 7.x dependencies[] = composer_manager -files[] = includes/github_api.nativecache.class.inc - diff --git a/github_api.module b/github_api.module index 32dcbe3..72ce61c 100644 --- a/github_api.module +++ b/github_api.module @@ -57,7 +57,7 @@ function github_api_client($auth = TRUE) { $cache_obj = NULL; if (variable_get('github_api_use_cache', TRUE)) { $cache_obj = new \Github\HttpClient\CachedHttpClient(); - $cache_obj->setCache(new DrupalGithubApiHttpClientCacheDrupalNativeCache()); + $cache_obj->setCache(new \GithubApi\DrupalGithubApiHttpClientCacheDrupalNativeCache()); } $client = new Github\Client($cache_obj); diff --git a/src/GithubApi/DrupalGithubApiHttpClientCacheDrupalNativeCache.php b/src/GithubApi/DrupalGithubApiHttpClientCacheDrupalNativeCache.php new file mode 100644 index 0000000..45b0a7d --- /dev/null +++ b/src/GithubApi/DrupalGithubApiHttpClientCacheDrupalNativeCache.php @@ -0,0 +1,98 @@ +loadRaw($id); + return (bool) $cached; + } + + /** + * {@inheritdoc} + */ + public function getModifiedSince($id) { + $cached = $this->loadRaw($id); + if ($cached) { + return $cached['modified_since']; + } + + return NULL; + } + + /** + * {@inheritdoc} + */ + public function getETag($id) { + $cached = $this->loadRaw($id); + if ($cached) { + return $cached['etag']; + } + + return NULL; + } + + /** + * {@inheritdoc} + */ + public function get($id) { + $cached = $this->loadRaw($id); + if (!$cached) { + throw new \InvalidArgumentException("Unable to load {$id}"); + } + + return $cached['response']; + } + + /** + * {@inheritdoc} + */ + public function set($id, Response $response) { + $key = "response_{$id}"; + $data = array( + 'response' => $response, + 'etag' => $response->getHeader('ETag'), + 'modified_since' => time(), + ); + + // cache_set can fail silently, so we have no way of knowing if this fails. + cache_set($key, $data, 'cache_github_api'); + } + + /** + * Retreieves raw cached objects from the backend. + * + * @param string $id The id of the cached resource + * + * @return bool|Guzzle\Http\Message\Response + * Returns FALSE if not found, otherwise the Guzzle response object. + */ + protected function loadRaw($id) { + $key = "response_{$id}"; + if (empty(self::$lookup[$key])) { + $cached = cache_get($key, 'cache_github_api'); + self::$lookup[$id] = $cached; + } + + if (!empty(self::$lookup[$key])) { + return self::$lookup[$key]; + } + + return NULL; + } +} \ No newline at end of file