Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
S1SYPHOS committed Jul 10, 2021
0 parents commit ce01315
Show file tree
Hide file tree
Showing 20 changed files with 24,599 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# EditorConfig
# See https://EditorConfig.org

root = true

[*]
charset = utf-8
indent_size = 4
end_of_line = lf
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
6 changes: 6 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Set default behaviour to automatically normalize line endings
* text=auto

# Git stuff
.gitattributes export-ignore
.gitignore export-ignore
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Composer + sensitive files
/composer.lock
/vendor
*.cache

# Auto-generated
/dist
16 changes: 16 additions & 0 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

$finder = PhpCsFixer\Finder::create()
->exclude([
'tmp'
])
->in(__DIR__)
;

return PhpCsFixer\Config::create()
->setRules([
'@PSR2' => true,
'array_syntax' => ['syntax' => 'short'],
])
->setFinder($finder)
;
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Martin Folkers

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
63 changes: 63 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# php-thx
[![Release](https://img.shields.io/github/release/S1SYPHOS/php-thx.svg)](https://github.com/S1SYPHOS/php-thx/releases) [![License](https://img.shields.io/github/license/S1SYPHOS/php-thx.svg)](https://github.com/S1SYPHOS/php-thx/blob/main/LICENSE) [![Issues](https://img.shields.io/github/issues/S1SYPHOS/php-thx.svg)](https://github.com/S1SYPHOS/php-thx/issues) [![Status](https://travis-ci.org/S1SYPHOS/php-thx.svg?branch=main)](https://travis-ci.org/S1SYPHOS/php-thx)

A very simple PHP library for acknowledging the people behind your frontend dependencies - and giving thanks.


## Getting started

Install this package with [Composer](https://getcomposer.org):

```text
composer require S1SYPHOS/php-thx
```

**Note:**
For yarn v2 support, the `php-yaml` package is required!


# Usage

This example should get you started:

```php
<?php

require_once('vendor/autoload.php');

use S1SYPHOS\Thx;

$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);

# Dump package names
var_dump($obj->packages())

# Dump (raw) data extracted from lockfiles
var_dump($obj->data())

} catch (Exception $e) {
# No dependencies found, file not found, ..
echo $e->getMessage();
}
```


## Roadmap

- [ ] Add (more sophisticated) tests
- [ ] Gather information using public APIs
- [ ] Custom `Exception`s
- [ ] Provide more methods
- [x] Parse yarn v1 lockfiles


## Credits

Most of the helper functions were taken from [Kirby](https://getkirby.com)'s excellent [`toolkit`](https://github.com/getkirby-v2/toolkit) package by [Bastian Allgeier](https://github.com/bastianallgeier) (who's just awesome, btw).


**Happy coding!**
35 changes: 35 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "s1syphos/php-thx",
"description": "Acknowledge the people behind your frontend dependencies - and give thanks!",
"type": "library",
"license": "MIT",
"version": "0.1.0",
"keywords": ["gratitude", "appreciation", "gratefulness", "thankfulness"],
"homepage": "https://github.com/S1SYPHOS",
"scripts": {
"cs-dry": "php-cs-fixer fix --dry-run --diff",
"cs-fix": "php-cs-fixer fix",
"test": "phpunit"
},
"authors": [
{
"name": "Martin Folkers",
"homepage": "https://twobrain.io",
"role": "Maintainer"
}
],
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.14",
"phpunit/phpunit": "^8.1"
},
"autoload": {
"psr-4": {
"S1SYPHOS\\": "lib/"
}
},
"autoload-dev": {
"psr-4": {
"S1SYPHOS\\Tests\\": "tests/"
}
}
}
58 changes: 58 additions & 0 deletions lib/Driver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace S1SYPHOS;


use S1SYPHOS\Traits\Helpers;


abstract class Driver
{
/**
* Traits
*/

use Helpers;


/**
* Properties
*/

/**
* Raw data as extracted from lockfile
*
* @var array
*/
public $data = null;


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


/**
* Constructor
*
* @param string $dataFile Path to data file
* @param string $lockFile Lockfile stream
* @return void
*/
public function __construct(array $pkgData, string $lockFile)
{
$this->data = $this->load($pkgData, $lockFile);
}


/**
* Methods
*/

abstract protected function load(array $pkgData, string $lockFile): array;

abstract public function packages(): array;
}
71 changes: 71 additions & 0 deletions lib/Drivers/Composer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace S1SYPHOS\Drivers;


use S1SYPHOS\Driver;


class Composer extends Driver
{
/**
* Properties
*/

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


/**
* Parses input files
*
* @param string $dataFile Path to data file
* @param string $lockFile Lockfile stream
* @return array
*/
protected function load(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);
}
}

return $phpData;
}


/**
* Methods
*/

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


/**
* Processes raw data from lockfile
*
* @param array $array The array to be processed
* @return string The result array
*/
protected function ex(array $array): array
{
return $array;
}
}
75 changes: 75 additions & 0 deletions lib/Drivers/Node.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace S1SYPHOS\Drivers;


use S1SYPHOS\Driver;


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

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


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

$lockData = json_decode($lockFile, true);

foreach ($lockData['packages'] as $pkgName => $pkg) {
if ($this->contains($pkgName, 'node_modules/')) {
$pkgName = str_replace('node_modules/', '', $pkgName);

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

return $npmData;
}


/**
* Methods
*/

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


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

0 comments on commit ce01315

Please sign in to comment.