Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converted to PSR-2 #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 46 additions & 43 deletions code/PackagistShortCode.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php
class PackagistShortCode {
public static function parse($arguments, $content=null, $parser=null) {
if(!array_key_exists('package', $arguments) || empty($arguments['package']) || strpos($arguments['package'], '/')<=0) {
class PackagistShortCode
{
public static function parse($arguments, $content=null, $parser=null)
{
if (!array_key_exists('package', $arguments) || empty($arguments['package']) || strpos($arguments['package'], '/')<=0) {
return '<p><i>Packagist package undefined</i></p>';
}

Expand All @@ -15,9 +17,9 @@ public static function parse($arguments, $content=null, $parser=null) {
$obj->Package=$arguments['package'];

//Add the button config
if(array_key_exists('mode', $arguments) && ($arguments['mode']=='total' || $arguments['mode']=='monthly' || $arguments['mode']=='daily')) {
if (array_key_exists('mode', $arguments) && ($arguments['mode']=='total' || $arguments['mode']=='monthly' || $arguments['mode']=='daily')) {
$obj->DisplayMode=$arguments['mode'];
}else {
} else {
$obj->DisplayMode='total';
}

Expand All @@ -27,18 +29,18 @@ public static function parse($arguments, $content=null, $parser=null) {
$cacheKey=md5('packagistshortcode_'.$arguments['package']);
$cache=SS_Cache::factory('PackagistShortCode');
$cachedData=$cache->load($cacheKey);
if($cachedData==null) {
if ($cachedData==null) {
$response=self::getFromAPI($arguments['package']);

//Verify a 200, if not say the repo errored out and cache false
if(empty($response) || $response===false || !property_exists($response, 'package')) {
if (empty($response) || $response===false || !property_exists($response, 'package')) {
$cachedData=array('total'=>'N/A', 'monthly'=>'N/A', 'daily'=>'N/A');
}else {
if($config->UseShortHandNumbers==true) {
} else {
if ($config->UseShortHandNumbers==true) {
$totalDownloads=self::shortHandNumber($response->package->downloads->total);
$monthlyDownloads=self::shortHandNumber($response->package->downloads->monthly);
$dailyDownloads=self::shortHandNumber($response->package->downloads->daily);
}else {
} else {
$totalDownloads=number_format($response->package->downloads->total);
$monthlyDownloads=number_format($response->package->downloads->monthly);
$dailyDownloads=number_format($response->package->downloads->daily);
Expand All @@ -47,15 +49,15 @@ public static function parse($arguments, $content=null, $parser=null) {
$cachedData=array('total'=>$totalDownloads, 'monthly'=>$monthlyDownloads, 'daily'=>$dailyDownloads);
}

//Cache response to file system
//Cache response to file system
$cache->save(serialize($cachedData), $cacheKey);
}else {
} else {
$cachedData=unserialize($cachedData);
}


$obj->TotalDownloads=$cachedData['total'];
$obj->MonthlyDownloads=$cachedData['monthly'];
$obj->TotalDownloads=$cachedData['total'];
$obj->MonthlyDownloads=$cachedData['monthly'];
$obj->DailyDownloads=$cachedData['daily'];


Expand All @@ -66,44 +68,45 @@ public static function parse($arguments, $content=null, $parser=null) {
return $ssViewer->process($obj);
}

/**
* Loads the data from the github api
* @param {string} $url URL to load
/**
* Loads the data from the github api
* @param {string} $url URL to load
* @return {stdObject} Returns the JSON Response from the GitHub API
*
* @see http://developer.github.com/v3/repos/#get
*/
final protected static function getFromAPI($repo) {
if(function_exists('curl_init') && $ch=curl_init()) {
curl_setopt($ch, CURLOPT_URL, 'https://packagist.org/packages/'.$repo.'.json');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
* @see http://developer.github.com/v3/repos/#get
*/
final protected static function getFromAPI($repo)
{
if (function_exists('curl_init') && $ch=curl_init()) {
curl_setopt($ch, CURLOPT_URL, 'https://packagist.org/packages/'.$repo.'.json');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);


$contents=json_decode(curl_exec($ch));
$contents=json_decode(curl_exec($ch));
curl_close($ch);

return $contents;
}else {
user_error('CURL is not available', E_USER_ERROR);
}
return $contents;
} else {
user_error('CURL is not available', E_USER_ERROR);
}
}

/**
/**
* Gets the short hand of the given number so 1000 becomes 1k, 2000 becomes 2k, and 1000000 becomes 1m etc
* @param {int} $number Number to convert
* @return {string} Short hand of the given number
* @param {int} $number Number to convert
* @return {string} Short hand of the given number
*/
protected static function shortHandNumber($number) {
if($number>=1000000000) {
return round($number/1000000000, 1).'B';
}else if($number>=1000000) {
return round($number/1000000, 1).'M';
}else if($number>=1000) {
return round($number/1000, 1).'K';
}

return $number;
}
protected static function shortHandNumber($number)
{
if ($number>=1000000000) {
return round($number/1000000000, 1).'B';
} elseif ($number>=1000000) {
return round($number/1000000, 1).'M';
} elseif ($number>=1000) {
return round($number/1000, 1).'K';
}

return $number;
}
}
?>