Skip to content

Instantly share code, notes, and snippets.

@kalmanolah
Last active December 20, 2015 13:39
Show Gist options
  • Save kalmanolah/6140331 to your computer and use it in GitHub Desktop.
Save kalmanolah/6140331 to your computer and use it in GitHub Desktop.
A Symfony2 service for fetching all of a Github user's gists. Gist content for the 10 latest gists is downloaded, and Github API responses are cached using APC.
<?php
/**
* A Symfony2 service for fetching all of a Github user's gists.
* Gist content for the 10 latest gists is downloaded, and Github API responses are cached using APC.
* The APC extension for PHP needs to be enabled.
*
* In order to use this Service, you should add the following services to Symfony2:
*
* services:
* cache:
* class: Doctrine\Common\Cache\ApcCache
* arguments: [%kernel.cache_dir%]
* gist.service:
* class: My\ExampleBundle\Service\GistService
* arguments: [@cache]
*
* Usage: $this->get('gist.service')->getGists();
*
* @author Kalman Olah <hello _AT_ kalmanolah _DOT_ net>
*/
namespace My\ExampleBundle\Service;
class GistService {
private $gh_username = 'kalmanolah';
// Replace this with your Github access token (https://github.com/settings/applications)
private $gh_access_token = 'yoursupersecretgithubaccesstoken';
private $gh_api_endpoint = 'https://api.github.com/';
private $cache;
private $cache_ttl = 3600; // It's not like we need instant updates
public function __construct($cache)
{
$this->cache = $cache;
$this->cache->setNamespace('gist.cache');
}
public function getGists()
{
$response = $this->getApiResponse('users/' . $this->gh_username . '/gists');
$i = 0; // Only fetch content for the first 10 gists
foreach($response as $key => $single) {
if ($i < 10) {
$response[$key] = $this->getGist($single->id);
$i++;
}
}
return $response;
}
private function getApiResponse($string, $FETCH_AS_ARRAY = FALSE)
{
$url = $this->generateApiUrl($string);
$response = $this->getResponse($url);
$response = json_decode($response, $FETCH_AS_ARRAY);
return $response;
}
private function getResponse($url, $NO_CACHE = FALSE)
{
$response = null;
if($NO_CACHE || false === ($response = $this->cache->fetch($url))) {
$options = array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_HTTPHEADER => array('Content-type: text/plain; application/json'),
CURLOPT_URL => $url,
);
$response = $this->getCurlResponse($options);
if (!$NO_CACHE) {
$this->cache->save($url, $response, $this->cache_ttl);
}
}
return $response;
}
private function generateApiUrl($string)
{
return $this->gh_api_endpoint . $string . '?access_token=' . $this->gh_access_token;
}
private function getGist($id)
{
// If we don't fetch these responses as arrays,
// Twig will cry and/or we'll have to cast stuff to arrays later on
$response = $this->getApiResponse('gists/' . $id, TRUE);
return $response;
}
private function getCurlResponse($options)
{
try {
$ch = curl_init();
if (FALSE === $ch)
throw new \Exception('cURL failed to initialize');
foreach($options as $key => $value) {
curl_setopt($ch, $key, $value);
}
$content = curl_exec($ch);
if (FALSE === $content)
throw new \Exception(curl_error($ch), curl_errno($ch));
return $content;
} catch(\Exception $e) {
trigger_error(sprintf(
'cURL failed: Error #%d: %s',
$e->getCode(), $e->getMessage()),
E_USER_ERROR);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment