Skip to content

Commit

Permalink
Release v0.3
Browse files Browse the repository at this point in the history
* Implement basic caching support
* Add guzzle to the mix
* Change 'Thx' class into factory
* Update test suite & README
  • Loading branch information
S1SYPHOS authored Jul 11, 2021
1 parent 1ea7baf commit a58c6e0
Show file tree
Hide file tree
Showing 11 changed files with 482 additions and 136 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ $pkgFile = 'path/to/composer.json'; # or 'package.json' for NPM / Yarn
$lockFile = 'path/to/composer.lock' # or 'package-lock.json' for NPM / 'yarn.lock' for Yarn

try {
$obj = new Thx($pkgFile, $lockFile);
$obj = Thx::giveBack($pkgFile, $lockFile)->spreadLove();

# Dump package names
var_dump($obj->packages())
Expand All @@ -49,7 +49,7 @@ try {
## Roadmap

- [ ] Add (more sophisticated) tests
- [ ] Gather information using public APIs
- [x] Gather information using public APIs
- [ ] Custom `Exception`s
- [ ] Provide more methods
- [x] Parse yarn v1 lockfiles
Expand Down
6 changes: 5 additions & 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": "0.2.0",
"version": "0.3.0",
"keywords": ["gratitude", "appreciation", "gratefulness", "thankfulness"],
"homepage": "https://github.com/S1SYPHOS",
"scripts": {
Expand All @@ -18,6 +18,10 @@
"role": "Maintainer"
}
],
"require": {
"shieldon/simple-cache": "^1.3",
"guzzlehttp/guzzle": "^7.3"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.14",
"phpunit/phpunit": "^8.1"
Expand Down
96 changes: 81 additions & 15 deletions lib/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace S1SYPHOS;


use S1SYPHOS\Traits\Caching;
use S1SYPHOS\Traits\Helpers;
use S1SYPHOS\Traits\Remote;


abstract class Driver
Expand All @@ -12,7 +14,9 @@ abstract class Driver
* Traits
*/

use Caching;
use Helpers;
use Remote;


/**
Expand Down Expand Up @@ -43,49 +47,89 @@ abstract class Driver
public $mode;


/**
* List of packages not to be processed
*
* @var array
*/
public $blockList = [];


/**
* Constructor
*
* @param string $dataFile Path to data file
* @param string $lockFile Lockfile stream
* @param string $cacheDriver Cache driver
* @param array $cacheSettings Cache settings
* @return void
*/
public function __construct(array $pkgData, string $lockFile)
public function __construct(array $pkgData, string $lockFile, string $cacheDriver, array $cacheSettings)
{
# Load package data
# (1) Extract raw data
# Create cache instance
$this->createCache($cacheDriver, $cacheSettings);

# Extract raw data
$this->data = $this->extract($pkgData, $lockFile);
}

# (2) Process raw data
$this->pkgs = $this->process();

/**
* Setters & getters
*/

public function setBlockList(int $blockList): void
{
$this->blockList = $blockList;
}


public function getBlockList(): array
{
return $this->blockList;
}


/**
* Required methods
* Shared methods
*/

/**
* Extracts raw data from input files
* Spreads love
*
* @param string $dataFile Path to data file
* @param string $lockFile Lockfile contents
* @return array
* @return \S1SYPHOS\Driver
*/
abstract protected function extract(array $pkgData, string $lockFile): array;
public function spreadLove(): \S1SYPHOS\Driver
{
# Process raw data
$this->pkgs = $this->process();

# Enable chaining
return $this;
}


/**
* Processes raw data
* Exports raw package data
*
* @return array Processed data
* @return array Raw package data
*/
abstract protected function process(): array;
public function data(): array
{
return $this->data;
}


/**
* Shared methods
* Exports processed package data
*
* @return array Processed package data
*/
public function pkgs(): array
{
return $this->pkgs;
}


/**
* Exports package names
Expand All @@ -95,4 +139,26 @@ abstract protected function process(): array;
public function packages(): array {
return $this->pluck($this->pkgs, 'name');
}


/**
* Required methods
*/

/**
* Extracts raw data from input files
*
* @param string $dataFile Path to data file
* @param string $lockFile Lockfile contents
* @return array
*/
abstract protected function extract(array $pkgData, string $lockFile): array;


/**
* Processes raw data
*
* @return array Processed data
*/
abstract protected function process(): array;
}
54 changes: 50 additions & 4 deletions lib/Drivers/Composer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ class Composer extends Driver
public $mode = 'php';


/**
* List of packages not to be processed
*
* @var array
*/
public $blockList = [
'php',
];


/**
* Methods
*/

/**
* Extracts raw data from input files
*
Expand Down Expand Up @@ -51,10 +65,42 @@ protected function extract(array $pkgData, string $lockFile): array
protected function process(): array
{
return array_map(function($pkgName, $pkg) {
return [
'name' => $pkgName,
'version' => str_replace('v', '', strtolower($pkg['version'])),
];
$data = [];

# Build unique caching key
$hash = md5($pkgName);

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

$this->fromCache = true;
}

if (empty($data)) {
# (2) .. from API
# Block unwanted libraries
if (in_array($pkgName, $this->blockList) === true) return false;

# Prepare data for each repository
$data['name'] = $pkgName;
$data['version'] = str_replace('v', '', strtolower($pkg['version']));

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

# Enrich data with results
$data['license'] = $response[$pkg['version']]['license'][0] ?? '';
$data['description'] = $response[$pkg['version']]['description'];
$data['url'] = static::rtrim($response[$pkg['version']]['source']['url'], '.git');

# Cache result
$this->cache->set($hash, $data, $this->days2seconds($this->cacheDuration));
}

return $data;
}, array_keys($this->data), $this->data);
}
}
62 changes: 58 additions & 4 deletions lib/Drivers/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class Node extends Driver
public $mode = 'npm';


/**
* Methods
*/

/**
* Extracts raw data from input files
*
Expand Down Expand Up @@ -55,10 +59,60 @@ protected function extract(array $pkgData, string $lockFile): array
protected function process(): array
{
return array_map(function($pkgName, $pkg) {
return [
'name' => $pkgName,
'version' => $pkg['version'],
];
return $this->_process($pkgName, $pkg);
}, array_keys($this->data), $this->data);
}


/**
* Processes raw data
*
* @return array Processed data
*/
protected function _process(string $pkgName, array $pkg): array
{
$data = [];

# Build unique caching key
$hash = md5($pkgName);

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

$this->fromCache = true;
}

if (empty($data)) {
# (2) .. from API
# Block unwanted libraries
if (in_array($pkgName, $this->blockList) === true) return false;

# Prepare data for each repository
$data['name'] = $pkgName;
$data['version'] = $pkg['version'];

# Fetch additional information from https://api.npms.io
$response = $this->fetchRemote('https://api.npms.io/v2/package/' . rawurlencode($pkgName));
$response = json_decode($response)->collected->metadata;

$data['license'] = $response->license ?? '';
$data['description'] = $response->description;
$data['url'] = $response->links->repository;
$data['forked'] = false;

# Check if it's a forked repository
if (preg_match('/(([0-9])+(\.{0,1}([0-9]))*)/', $data['version']) == false) {
# TODO: Check if that's even a thing
# $data['version'] = $data->version;
$data['forked'] = true;
}

# Cache result
$this->cache->set($hash, $data, $this->days2seconds($this->cacheDuration));
}

return $data;
}
}
34 changes: 3 additions & 31 deletions lib/Drivers/Yarn.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,15 @@
namespace S1SYPHOS\Drivers;


use S1SYPHOS\Driver;
use S1SYPHOS\Drivers\Node;


class Yarn extends Driver
class Yarn extends Node
{
/**
* Properties
* Methods
*/

/**
* Operating mode identifier
*
* @var string
*/
public $mode = null;


/**
* Extracts raw data from input files
*
Expand Down Expand Up @@ -69,26 +61,6 @@ protected function extract(array $pkgData, string $lockFile): array
}


/**
* Processes raw data
*
* @return array Processed data
*/
protected function process(): array
{
return array_map(function($pkgName, $pkg) {
return [
'name' => $pkgName,
'version' => $pkg['version'],
];
}, array_keys($this->data), $this->data);
}


/**
* Methods
*/

/**
* Removes redundant characters from strings
*
Expand Down
Loading

0 comments on commit a58c6e0

Please sign in to comment.