Skip to content

Commit

Permalink
Merge pull request #5 from webbuilders-group/SS4
Browse files Browse the repository at this point in the history
Ss4
  • Loading branch information
RobertLeCreux authored Jan 18, 2019
2 parents ccf6e0a + 1a855f5 commit 3ea6765
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 37 deletions.
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,17 @@ Add a short code for adding a Packagist installs button with a count to a HTMLTe
* Ed Chipman ([UndefinedOffset](https://github.com/UndefinedOffset))

## Requirements
* SilverStripe CMS 3.x
* SilverStripe CMS 4.x


## Installation
__Composer (recommended):__
```
composer require webbuilders-group/silverstripe-packagistshortcode
```


If you prefer you may also install manually:
* Download the module from here https://github.com/webbuilders-group/silverstripe-packagistshortcode/archive/master.zip
* Extract the downloaded archive into your site root so that the destination folder is called githubshortcode, opening the extracted folder should contain _config.php in the root along with other files/folders
* Run dev/build?flush=all to regenerate the manifest
Expand Down Expand Up @@ -37,7 +44,12 @@ In 3.1 the short codes above will work as included however the updated syntax fo
There are a few configuration options available to you:

```yml
SilverStripe\Core\Injector\Injector:
Psr\SimpleCache\CacheInterface.PackagistShortCode:
factory: SilverStripe\Core\Cache\CacheFactory
constructor:
namespace: "NewRelic"
defaultLifetime: 86400 #Cache time in seconds
PackagistShortCode:
CacheTime: 86400 #Cache time in seconds (default is 1 day, remember the GitHub api is rate limited)
UseShortHandNumbers: true #Use short hand numbers i.e 5.6K or not
```
3 changes: 1 addition & 2 deletions _config.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@
define('PACKAGISTSHORTCODE_BASE', basename(dirname(__FILE__)));

//Enable the parser
ShortcodeParser::get_active()->register('packagist', array('PackagistShortCode', 'parse'));
?>
SilverStripe\View\Parsers\ShortcodeParser::get_active()->register('packagist', array('PackagistShortCode', 'parse'));
7 changes: 6 additions & 1 deletion _config/config.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
---
Name: packagistbutton
---
SilverStripe\Core\Injector\Injector:
Psr\SimpleCache\CacheInterface.PackagistShortCode:
factory: SilverStripe\Core\Cache\CacheFactory
constructor:
namespace: "NewRelic"
defaultLifetime: 86400 #Cache time in seconds
PackagistShortCode:
CacheTime: 86400 #Cache time in seconds
UseShortHandNumbers: true #Use short hand numbers i.e 5.6K or not
11 changes: 8 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "webbuilders-group/silverstripe-packagistshortcode",
"description": "Add a short code for adding a Packagist installs button with a count to a HTMLText field.",
"type": "silverstripe-module",
"type": "silverstripe-vendormodule",
"keywords": ["silverstripe", "packagist", "shortcode"],
"license": "BSD-3-Clause",
"authors": [
Expand All @@ -14,10 +14,15 @@

"require":
{
"silverstripe/framework": "3.*",
"composer/installers": "*"
"silverstripe/framework": "~4.0"
},
"support": {
"issues": "https://github.com/webbuilders-group/silverstripe-packagistshortcode/issues"
},
"extra": {
"expose" : [
"css",
"images"
]
}
}
65 changes: 36 additions & 29 deletions code/PackagistShortCode.php → src/PackagistShortCode.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
<?php

namespace WebbuildersGroup\PackagistShortcode;

use SilverStripe\Core\Config\Config;
use Psr\SimpleCache\CacheInterface;
use SilverStripe\View\ViewableData;
use SilverStripe\View\Requirements;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\View\SSViewer;

class PackagistShortCode {
public static function parse($arguments, $content=null, $parser=null) {
if(!array_key_exists('package', $arguments) || empty($arguments['package']) || strpos($arguments['package'], '/')<=0) {
Expand All @@ -22,12 +32,10 @@ public static function parse($arguments, $content=null, $parser=null) {
}

//Retrieve Stats
SS_Cache::set_cache_lifetime('PackagistShortCode', $config->CacheTime);

$cacheKey=md5('packagistshortcode_'.$arguments['package']);
$cache=SS_Cache::factory('PackagistShortCode');
$cachedData=$cache->load($cacheKey);
if($cachedData==null) {
$cache=Injector::inst()->get(CacheInterface::class.'.PackagistShortCode');

if(!$cache->has($cacheKey)) {
$response=self::getFromAPI($arguments['package']);

//Verify a 200, if not say the repo errored out and cache false
Expand All @@ -47,15 +55,15 @@ public static function parse($arguments, $content=null, $parser=null) {
$cachedData=array('total'=>$totalDownloads, 'monthly'=>$monthlyDownloads, 'daily'=>$dailyDownloads);
}

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


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


Expand All @@ -66,33 +74,33 @@ 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) {
Expand All @@ -105,5 +113,4 @@ protected static function shortHandNumber($number) {

return $number;
}
}
?>
}

0 comments on commit 3ea6765

Please sign in to comment.