diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..788cf54 --- /dev/null +++ b/.env.example @@ -0,0 +1,3 @@ +SEVEN_ZIP_BINARY_PATH_LINUX=/usr/bin/7z +SEVEN_ZIP_BINARY_PATH_WINDOWS=C:\Users\runneradmin\scoop\apps\7zip\current\7z.exe +SEVEN_ZIP_BINARY_PATH_MACOS=/usr/local/bin/7z diff --git a/.github/workflows/run-macos.yml b/.github/workflows/run-macos.yml index 15a1808..2ad8178 100644 --- a/.github/workflows/run-macos.yml +++ b/.github/workflows/run-macos.yml @@ -49,6 +49,11 @@ jobs: sudo cp ./modules/rar.so $pecl_path sudo echo "extension=rar.so" > $phpini_path + - name: Create .env file + run: | + cp .env.example .env + shell: bash + - name: Check extension rar run: php -m | grep rar diff --git a/.github/workflows/run-tests-pz7ip.yml b/.github/workflows/run-tests-pz7ip.yml index 6d11e6b..954e35a 100644 --- a/.github/workflows/run-tests-pz7ip.yml +++ b/.github/workflows/run-tests-pz7ip.yml @@ -31,6 +31,11 @@ jobs: - name: Install dependencies run: composer update --prefer-dist --no-interaction + - name: Create .env file + run: | + cp .env.example .env + shell: bash + - name: Check extension imagick run: php -m | grep imagick diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index f0817c7..3f7e8fc 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -55,6 +55,11 @@ jobs: sudo cp ./modules/rar.so $pecl_path sudo echo "extension=rar.so" > $phpini_path + - name: Create .env file + run: | + cp .env.example .env + shell: bash + - name: Check extension rar run: php -m | grep rar diff --git a/.github/workflows/run-windows.yml b/.github/workflows/run-windows.yml index f0fbf55..ae968bf 100644 --- a/.github/workflows/run-windows.yml +++ b/.github/workflows/run-windows.yml @@ -47,6 +47,11 @@ jobs: - name: Install dependencies run: composer update --prefer-dist --no-interaction + - name: Create .env file + run: | + cp .env.example .env + shell: powershell + # - name: Check extension rar # run: php -m | grep rar diff --git a/.gitignore b/.gitignore index 9985a12..79ede7e 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ example temp CHANGELOG-draft.md .phpunit.cache +.env diff --git a/README.md b/README.md index de58793..c97d52d 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ [![tests][tests-src]][tests-href] [![codecov][codecov-src]][codecov-href] -PHP package to handle archives (`.zip`, `.rar`, `.tar`, `.7z`, `.pdf`) with unified API and hybrid solution (native/`p7zip`), designed to works with eBooks (`.epub`, `.cbz`, `.cbr`, `.cb7`, `.cbt`). +PHP package to handle archives (`.zip`, `.rar`, `.tar`, `.7z`, `.pdf`) with unified API and hybrid solution (native/`p7zip`), designed to works with EPUB and CBA (`.cbz`, `.cbr`, `.cb7`, `.cbt`). Supports Linux, macOS and Windows. diff --git a/composer.json b/composer.json index 239263b..86e2a84 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "kiwilan/php-archive", - "version": "2.2.0", - "description": "PHP package to handle archives (.zip, .rar, .tar, .7z, .pdf) with unified API and hybrid solution (native/p7zip), designed to works with eBooks (.epub, .cbz, .cbr, .cb7, .cbt).", + "version": "2.3.0", + "description": "PHP package to handle archives (.zip, .rar, .tar, .7z, .pdf) with unified API and hybrid solution (native/p7zip), designed to works with EPUB and CBA (.cbz, .cbr, .cb7, .cbt).", "keywords": [ "php", "archive", diff --git a/tests/ArchiveTest.php b/tests/ArchiveTest.php index a60c007..2c54ac6 100644 --- a/tests/ArchiveTest.php +++ b/tests/ArchiveTest.php @@ -159,14 +159,7 @@ function (Pest\Expectation $item) use ($ext) { })->with([ZIP_PASSWORD, RAR_PASSWORD, SEVENZIP_PASSWORD, TAR_PASSWORD]); it('can handle archive with binary path', function (string $path) { - if (PHP_OS_FAMILY === 'Windows') { - $current_user = exec('echo %USERNAME%'); - $binary_path = "C:\\Users\\{$current_user}\\scoop\\apps\\7zip\\current\\7z.exe"; - } elseif (PHP_OS_FAMILY === 'Darwin') { - $binary_path = '/opt/homebrew/bin/7z'; - } else { - $binary_path = '/usr/bin/7z'; - } + $binary_path = getSevenZipBinaryPath(); $archive = Archive::read($path)->overrideBinaryPath($binary_path); $files = $archive->getFileItems(); diff --git a/tests/Pest.php b/tests/Pest.php index 0d5d468..e0f1bc4 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -179,3 +179,53 @@ function recurseRmdir(string $dir) } // rmdir($dir); } + +function dotenv(): array +{ + $path = __DIR__.'/../'; + $lines = file($path.'.env'); + $dotenv = []; + + foreach ($lines as $line) { + if (! empty($line)) { + $data = explode('=', $line); + $key = $data[0]; + if ($key === " \n ") { + continue; + } + unset($data[0]); + $value = implode('=', $data); + + $key = $key ? trim($key) : ''; + $value = $value ? trim($value) : ''; + + if ($key === '') { + continue; + } + + $value = str_replace('"', '', $value); + $value = str_replace("'", '', $value); + + $dotenv[$key] = $value; + } + } + + return $dotenv; +} + +function getDotenv(string $key): string +{ + return dotenv()[$key] ?? ''; +} + +function getSevenZipBinaryPath(): string +{ + $os = PHP_OS_FAMILY; + $dotenv = match ($os) { + 'Windows' => 'SEVEN_ZIP_BINARY_PATH_WINDOWS', + 'Darwin' => 'SEVEN_ZIP_BINARY_PATH_MACOS', + default => 'SEVEN_ZIP_BINARY_PATH_LINUX', + }; + + return getDotenv($dotenv); +}