Skip to content

Commit

Permalink
v0.2.0
Browse files Browse the repository at this point in the history
* Add (hook for) data processing
* Update test suite
* Add missing doc strings
* Bump version
  • Loading branch information
S1SYPHOS authored Jul 10, 2021
1 parent ce01315 commit 1ea7baf
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 97 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": "0.1.0",
"version": "0.2.0",
"keywords": ["gratitude", "appreciation", "gratefulness", "thankfulness"],
"homepage": "https://github.com/S1SYPHOS",
"scripts": {
Expand Down
48 changes: 44 additions & 4 deletions lib/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ abstract class Driver
public $data = null;


/**
* Processed data
*
* @var array
*/
public $pkgs = null;


/**
* Operating mode identifier
*
Expand All @@ -44,15 +52,47 @@ abstract class Driver
*/
public function __construct(array $pkgData, string $lockFile)
{
$this->data = $this->load($pkgData, $lockFile);
# Load package data
# (1) Extract raw data
$this->data = $this->extract($pkgData, $lockFile);

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


/**
* Methods
* 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;


abstract protected function load(array $pkgData, string $lockFile): array;
/**
* Shared methods
*/

abstract public function packages(): array;
/**
* Exports package names
*
* @return array Package names
*/
public function packages(): array {
return $this->pluck($this->pkgs, 'name');
}
}
37 changes: 13 additions & 24 deletions lib/Drivers/Composer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@ class Composer extends Driver


/**
* Parses input files
* Extracts raw data from input files
*
* @param string $dataFile Path to data file
* @param string $lockFile Lockfile stream
* @param string $lockFile Lockfile contents
* @return array
*/
protected function load(array $pkgData, string $lockFile): array
protected function extract(array $pkgData, string $lockFile): array
{
$lockData = json_decode($lockFile, true);

$phpData = [];

foreach ($lockData['packages'] as $pkg) {
if (in_array($pkg['name'], array_keys($pkgData['require'])) === true) {
$phpData[$pkg['name']] = $this->ex($pkg);
$phpData[$pkg['name']] = $pkg;
}
}

Expand All @@ -44,28 +44,17 @@ protected function load(array $pkgData, string $lockFile): array


/**
* Methods
*/

/**
* Processes raw data from lockfile
*
* @return array Extracted packages
*/
public function packages(): array
{
return [];
}


/**
* Processes raw data from lockfile
* Processes raw data
*
* @param array $array The array to be processed
* @return string The result array
* @return array Processed data
*/
protected function ex(array $array): array
protected function process(): array
{
return $array;
return array_map(function($pkgName, $pkg) {
return [
'name' => $pkgName,
'version' => str_replace('v', '', strtolower($pkg['version'])),
];
}, array_keys($this->data), $this->data);
}
}
37 changes: 13 additions & 24 deletions lib/Drivers/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ class Node extends Driver


/**
* Parses input files
* Extracts raw data from input files
*
* @param string $dataFile Path to data file
* @param string $lockFile Lockfile stream
* @param string $lockFile Lockfile contents
* @return array
*/
protected function load(array $pkgData, string $lockFile): array
protected function extract(array $pkgData, string $lockFile): array
{
$npmData = [];

Expand All @@ -38,7 +38,7 @@ protected function load(array $pkgData, string $lockFile): array
$pkgName = str_replace('node_modules/', '', $pkgName);

if (in_array($pkgName, array_keys($pkgData['dependencies'])) === true) {
$npmData[$pkgName] = $this->ex($pkg);
$npmData[$pkgName] = $pkg;
}
}
}
Expand All @@ -48,28 +48,17 @@ protected function load(array $pkgData, string $lockFile): array


/**
* Methods
*/

/**
* Processes raw data from lockfile
*
* @return array Extracted packages
*/
public function packages(): array
{
return [];
}


/**
* Processes raw data from lockfile
* Processes raw data
*
* @param array $array The array to be processed
* @return string The result array
* @return array Processed data
*/
protected function ex(array $array): array
protected function process(): array
{
return $array;
return array_map(function($pkgName, $pkg) {
return [
'name' => $pkgName,
'version' => $pkg['version'],
];
}, array_keys($this->data), $this->data);
}
}
37 changes: 15 additions & 22 deletions lib/Drivers/Yarn.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ class Yarn extends Driver


/**
* Parses input files
* Extracts raw data from input files
*
* @param string $dataFile Path to data file
* @param string $lockFile Lockfile stream
* @param string $lockFile Lockfile contents
* @return array
*/
protected function load(array $pkgData, string $lockFile): array
protected function extract(array $pkgData, string $lockFile): array
{
$npmData = [];

Expand All @@ -44,7 +44,7 @@ protected function load(array $pkgData, string $lockFile): array
$pkgName = substr($pkgName, 0, strpos($pkgName, '@'));

if (in_array($pkgName, array_keys($pkgData['dependencies'])) === true) {
$npmData[$pkgName] = $this->ex($pkg);
$npmData[$pkgName] = $pkg;
}
}

Expand All @@ -59,7 +59,7 @@ protected function load(array $pkgData, string $lockFile): array
$pkgName = $this->split($pkgName, '@npm')[0];

if (in_array($pkgName, array_keys($pkgData['dependencies'])) === true) {
$npmData[$pkgName] = $this->ex($pkg);
$npmData[$pkgName] = $pkg;
}
}
}
Expand All @@ -70,31 +70,24 @@ protected function load(array $pkgData, string $lockFile): array


/**
* Methods
*/

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


/**
* Processes raw data from lockfile
*
* @param array $array The array to be processed
* @return string The result array
* Methods
*/
protected function ex(array $array): array
{
return $array;
}


/**
* Removes redundant characters from strings
Expand Down
30 changes: 28 additions & 2 deletions lib/Thx.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Thx
/**
* Current version
*/
const VERSION = '0.1.0';
const VERSION = '0.2.0';


/**
Expand Down Expand Up @@ -109,9 +109,35 @@ public function __construct(string $dataFile, string $lockFile)
* Methods
*/


/**
* Exports raw package data
*
* @return array Raw package data
*/
public function data(): array
{
return $this->driver->data;
}


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


/**
* Exports package names
*
* @return array Package names
*/
public function packages(): array
{
return $this->driver->packages();
}
}
52 changes: 32 additions & 20 deletions test.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,47 @@
require_once('vendor/autoload.php');


$drivers = [
'composer',
'yarn1',
'yarn2',
'npm',
];

if (in_array($argv[1], $drivers) === false) {
throw new Exception('Invalid driver, exiting ..');
}


switch ($argv[1]) {
case 'composer':
if (isset($argv[1])) {
if ($argv[1] === 'composer') {
$obj = new S1SYPHOS\Thx('tests/composer/composer.json', 'tests/composer/composer.lock');
}

case 'yarn1':
if ($argv[1] === 'yarn1') {
$obj = new S1SYPHOS\Thx('tests/yarn-v1/package.json', 'tests/yarn-v1/yarn.lock');
}

case 'yarn2':
if ($argv[1] === 'yarn2') {
$obj = new S1SYPHOS\Thx('tests/yarn-v2/package.json', 'tests/yarn-v2/yarn.lock');
}

case 'npm':
if ($argv[1] === 'npm') {
$obj = new S1SYPHOS\Thx('tests/npm/package.json', 'tests/npm/package-lock.json');
}
}


var_dump($obj->data());
if (!isset($obj)) {
echo sprintf('Invalid driver, exiting ..%s', "\n");
exit(1);
}


if (isset($argv[2])) {
# Extend proper geeting
echo sprintf('Loading tests for "%s" ..%s', $argv[1], "\n");

# Extend proper geeting
echo sprintf('Loading tests for %s .. done.%s', $argv[1], "\n");
if ($argv[2] === 'data') {
var_dump($obj->data());
}

if ($argv[2] === 'pkgs') {
var_dump($obj->pkgs());
}

if ($argv[2] === 'packages') {
var_dump($obj->packages());
}

} else {
echo sprintf('Invalid data source for "%s", exiting ..%s', $argv[1], "\n");
}

0 comments on commit 1ea7baf

Please sign in to comment.