Skip to content

Commit

Permalink
init lib
Browse files Browse the repository at this point in the history
  • Loading branch information
GlucNAc committed May 17, 2024
1 parent e11fbdc commit a02bde2
Show file tree
Hide file tree
Showing 21 changed files with 1,055 additions and 0 deletions.
120 changes: 120 additions & 0 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
name: continuous-integration

on: [push, pull_request]

env:
# PHP ENV vars
XDEBUG_MODE: 'off'

jobs:
install:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
extensions: pcov
coverage: pcov

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v3
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
- name: Install dependencies
run: composer install --prefer-dist --no-progress --no-scripts --optimize-autoloader --apcu-autoloader

quality:
needs: install
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v3
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
- name: Check code style
run: vendor/bin/ecs

- name: Static analysis
run: vendor/bin/phpstan analyze

test:
needs: quality
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v3
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
- name: Run tests
run: vendor/bin/phpunit --log-junit report.xml

coverage:
needs: test
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
extensions: pcov
coverage: pcov

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v3
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
- name: Run tests with coverage
run: php -dpcov.enabled=1 -dpcov.directory=src -dpcov.exclude="~vendor~" vendor/bin/phpunit --coverage-clover=coverage.xml

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: ./coverage.xml
55 changes: 55 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"name": "glucnac/ziparchivemanager",
"type": "library",
"description": "A simple wrapper around PHP's ZipArchive class to make it easier to work with zip archives.",
"keywords": ["Zip", "Archive", "Wrapper"],
"homepage": "https://github.com/GlucNAc/ZipArchiveManager",
"license": "MIT",
"authors": [
{
"name": "GlucNAc",
"homepage": "https://github.com/GlucNAc"
}
],
"support": {
"issues": "https://github.com/GlucNAc/ZipArchiveManager/-/issues",
"source": "https://github.com/GlucNAc/ZipArchiveManager"
},
"require": {
"php": ">=8.1",
"ext-zip": "*",
"doctrine/collections": "^2.0|^3.0",
"symfony/finder": "^5.4|^6.0|^7.0",
"symfony/options-resolver": "^5.4|^6.0|^7.0"
},
"require-dev": {
"mockery/mockery": "^1.5",
"php-parallel-lint/php-parallel-lint": "^1.3",
"phpstan/phpstan": "^1.8",
"phpstan/phpstan-mockery": "^1.1",
"phpunit/phpunit": "^10.1",
"symfony/filesystem": "^6.0",
"symplify/easy-coding-standard": "^12.0"
},
"autoload": {
"psr-4": {
"GlucNAc\\ZipArchiveManager\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"GlucNAc\\ZipArchiveManager\\Test\\": "tests/"
}
},
"minimum-stability": "dev",
"prefer-stable": true,
"config": {
"allow-plugins": {
"php-http/discovery": true,
"ramsey/composer-repl": true,
"phpstan/extension-installer": true,
"ergebnis/composer-normalize": true
},
"sort-packages": true
}
}
6 changes: 6 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
parameters:
ignoreErrors:
-
message: "#^Parameter \\#1 \\$collection of static method GlucNAc\\\\ZipArchiveManager\\\\Transformer\\\\AbstractToArchivableFileTransformer\\:\\:getArchivableFiles\\(\\) expects Doctrine\\\\Common\\\\Collections\\\\Collection\\<int, object\\>, Doctrine\\\\Common\\\\Collections\\\\Collection\\<int, SplFileInfo\\> given\\.$#"
count: 1
path: tests/Transformer/SplFileInfoToArchivableFileTransformerTest.php
77 changes: 77 additions & 0 deletions src/AssertCollectionTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

declare(strict_types=1);

namespace GlucNAc\ZipArchiveManager;

use Doctrine\Common\Collections\Collection;

trait AssertCollectionTrait
{
/**
* @template T of object
*
* @param Collection<int, T> $collection
* @param class-string<T> $expectedClass
*/
public static function assertIsCollectionOf(Collection $collection, string $expectedClass): void
{
self::assertClassExists($expectedClass);

foreach ($collection as $key => $entity) {
if (!$entity instanceof $expectedClass) {
throw new \InvalidArgumentException(
sprintf(
'Argument 1 passed to "%s()" must be collection of "%s", but element "%s" is of "%s".',
__METHOD__,
$expectedClass,
$key,
\get_class($entity),
)
);
}
}
}

/**
* @template T of object
*
* @param iterable<T> $iterable
* @param class-string<T> $expectedClass
*/
public static function assertIsIterableOf(iterable $iterable, string $expectedClass): void
{
self::assertClassExists($expectedClass);

foreach ($iterable as $entity) {
if (!$entity instanceof $expectedClass) {
throw new \InvalidArgumentException(
sprintf(
'Argument 1 passed to "%s()" must be an iterable of "%s", "%s" given.',
__METHOD__,
$expectedClass,
\get_class($entity),
)
);
}
}
}

/**
* @template T of object
*
* @param class-string<T> $expectedClass
*/
private static function assertClassExists(string $expectedClass): void
{
if (!class_exists($expectedClass) && !interface_exists($expectedClass)) {
throw new \InvalidArgumentException(
sprintf(
'Argument 2 passed to "%s()" must be a valid FQCN. "%s" given.',
__METHOD__,
$expectedClass,
)
);
}
}
}
51 changes: 51 additions & 0 deletions src/File/ArchivableFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace GlucNAc\ZipArchiveManager\File;

class ArchivableFile implements ArchivableFileInterface
{
private string $fullPath;
private string|null $entryName = null;

public function getFileName(): string
{
return \pathinfo($this->getFullPath(), PATHINFO_FILENAME);
}

public function getExtension(): string
{
return \pathinfo($this->getFullPath(), PATHINFO_EXTENSION);
}

public function getFullPath(): string
{
return $this->fullPath;
}

public function setFullPath(string $fullPath): static
{
$this->fullPath = $fullPath;

return $this;
}

/**
* This method is used to get the name of the file inside the archive. It can be useful to rename the file
* on the fly, or to put it in a subdirectory by returning a relative path.
*
* When equals to null, the entry name will be the same as the file name.
*/
public function getEntryName(): string
{
return $this->entryName ?? \pathinfo($this->getFullPath(), PATHINFO_BASENAME);
}

public function setEntryName(string|null $entryName): static
{
$this->entryName = $entryName;

return $this;
}
}
24 changes: 24 additions & 0 deletions src/File/ArchivableFileInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace GlucNAc\ZipArchiveManager\File;

interface ArchivableFileInterface
{
public function getFullPath(): string;

public function setFullPath(string $fullPath): static;

public function getFileName(): string;

public function getExtension(): string;

/**
* This method is used to get the name of the file inside the archive. It can be useful to rename the file
* on the fly, or to put it in a subdirectory by returning a relative path.
*/
public function getEntryName(): string;

public function setEntryName(string|null $entryName): static;
}
Loading

0 comments on commit a02bde2

Please sign in to comment.