Skip to content

Commit

Permalink
Release v1.2.0
Browse files Browse the repository at this point in the history
* Improve & annotate 'fetchRemote()'
* Convert some properties' visibility, clean up
* Split repo / project name & maintainer
* Sort processed data
  • Loading branch information
S1SYPHOS authored Jul 11, 2021
1 parent e48b799 commit 43c60dc
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 48 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Acknowledge the people behind your frontend dependencies - and give thanks!",
"type": "library",
"license": "MIT",
"version": "1.1.2",
"version": "1.2.0",
"keywords": ["gratitude", "appreciation", "gratefulness", "thankfulness"],
"homepage": "https://github.com/S1SYPHOS",
"scripts": {
Expand Down
25 changes: 18 additions & 7 deletions lib/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,33 @@ public function spreadLove(\Shieldon\SimpleCache\Cache $cache, array $config): \
}


/**
* Fetches information from API endpoint
*
* @param string $apiURL API endpoint
* @param int $timeout Request timeout in seconds
* @param string $userAgent User-Agent header
* @return string Response text - empty if connection failed
*/
protected function fetchRemote(string $apiURL, int $timeout = 3, string $userAgent = ''): string
{
# Initialize HTTP client
$client = new \GuzzleHttp\Client(['timeout' => $timeout]);

try {
# Fetch data from API
# (1) Send GET request
$response = $client->get($apiURL, ['headers' => ['User-Agent' => $userAgent]]);
} catch (\GuzzleHttp\Exception\TransferException $e) {
return '';
}

if ($response->getStatusCode() === 200) {
return $response->getBody();
}
# (2) If successful ..
if ($response->getStatusCode() === 200) {
# .. save response
return $response->getBody();
}

# .. otherwise, return empty text
} catch (\GuzzleHttp\Exception\TransferException $e) {}

# (3) .. otherwise, transmission *may* have worked
return '';
}

Expand Down
21 changes: 12 additions & 9 deletions lib/Drivers/Composer.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,30 +62,33 @@ protected function process(\Shieldon\SimpleCache\Cache $cache, array $config): \
# Build unique caching key
$hash = md5($pkgName);

# Determine whether data was stored in cache
$fromCache = false;

# Fetch information about package ..
# (1) .. from cache (if available)
if ($cache->has($hash)) {
# (1) .. from cache (if available)
$data = $cache->get($hash);
$fromCache = true;
}

# (2) .. from API (if not)
if (empty($data)) {
# (2) .. from API
# Block unwanted libraries
if (in_array($pkgName, $config['blockList']) === true) return false;

# Prepare data for each repository
$data['name'] = $pkgName;
# Prepare data for each repository by determining ..
# (1) .. name of repository
$splitList = static::split($pkgName, '/');
$data['name'] = $splitList[1];

# (2) .. exact version
$data['version'] = str_replace('v', '', strtolower($pkg['version']));

# (3) .. maintainer
$data['maintainer'] = $splitList[0];

# Fetch additional information from https://packagist.org
$apiURL = 'https://repo.packagist.org/p/' . $pkgName . '.json';
$response = $this->fetchRemote($apiURL, $config['timeout'], $config['userAgent']);

# Skip processing if connection is faulty
# Skip processing if connection failed
if (empty($response)) {
return $data;
}
Expand Down
9 changes: 8 additions & 1 deletion lib/Drivers/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,16 @@ protected function process(\Shieldon\SimpleCache\Cache $cache, array $config): \

$response = json_decode($response)->collected->metadata;

# Split URL & set pointer to last entry
$repoURL = $response->links->repository;

$splitList = static::split($repoURL, '/');
end($splitList);

$data['maintainer'] = prev($splitList);
$data['license'] = $response->license ?? '';
$data['description'] = $response->description;
$data['url'] = $response->links->repository;
$data['url'] = $repoURL;

# Cache result
$cache->set($hash, $data, $this->days2seconds($config['cacheDuration']));
Expand Down
7 changes: 0 additions & 7 deletions lib/Packaging/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@ class Collection implements Countable, Iterator
use Helpers;


/**
* Properties
*/

protected $index = 0;


/**
* Methods
*/
Expand Down
6 changes: 3 additions & 3 deletions lib/Packaging/Packages.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ class Packages extends Collection
*
* @var array
*/
public $data = null;
protected $data;


/**
* Constructor
*
* @param array $pkgs Processed data
* @param array $data Processed data
*/
public function __construct(array $data)
{
$this->data = $data;
$this->data = static::sort($data, 'name', 'asc');
}


Expand Down
4 changes: 2 additions & 2 deletions lib/Thx.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Thx
/**
* Current version
*/
const VERSION = '1.1.2';
const VERSION = '1.2.0';


/**
Expand All @@ -44,7 +44,7 @@ class Thx
*
* @var array
*/
public $blockList = [];
protected $blockList = [];


/**
Expand Down
26 changes: 9 additions & 17 deletions lib/Traits/Caching.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ trait Caching
* Properties
*/

/**
* Cache driver
*
* @var \Shieldon\SimpleCache\Cache
*/
public $cache;


/**
* Holds tokens of all possible cache drivers
*
Expand All @@ -33,28 +41,12 @@ trait Caching
];


/**
* Cache driver
*
* @var \Psr\SimpleCache\CacheInterface
*/
protected $cache;


/**
* Defines cache duration (in days)
*
* @var int
*/
protected $cacheDuration = 2;


/**
* Determines whether output was fetched from cache
*
* @var bool
*/
public $fromCache = false;
protected $cacheDuration = 7;


/**
Expand Down
36 changes: 36 additions & 0 deletions lib/Traits/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,42 @@ protected static function pluck(array $array, string $key): array
}


/**
* Sorts a multi-dimensional array by a certain column
*
* @param array $array The source array
* @param string $field The name of the column
* @param string $direction desc (descending) or asc (ascending)
* @param int $method A PHP sort method flag or 'natural' for natural sorting, which is not supported in PHP by sort flags
* @return array The sorted array
*/
protected static function sort(array $array, string $field, string $direction = 'desc', $method = SORT_REGULAR): array
{
$direction = strtolower($direction) === 'desc' ? SORT_DESC : SORT_ASC;
$helper = [];
$result = [];

// build the helper array
foreach ($array as $key => $row) {
$helper[$key] = $row[$field];
}

// natural sorting
if ($direction === SORT_DESC) {
arsort($helper, $method);
} else {
asort($helper, $method);
}

// rebuild the original array
foreach ($helper as $key => $val) {
$result[$key] = $array[$key];
}

return $result;
}


/**
* Miscellaneous
*/
Expand Down
2 changes: 1 addition & 1 deletion test.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
var_dump($obj->giveBack()->packages());
}

if ($argv[2] === 'license') {
if ($argv[2] === 'license' || $argv[2] === 'licenses') {
var_dump($obj->giveBack()->byLicense());
var_dump($obj->giveBack()->licenses());
}
Expand Down

0 comments on commit 43c60dc

Please sign in to comment.