Skip to content

Commit fc7575e

Browse files
Merge pull request #39 from kiwilan/develop
2.2.0
2 parents d8aafb1 + dad5667 commit fc7575e

File tree

14 files changed

+81
-62
lines changed

14 files changed

+81
-62
lines changed

.github/workflows/run-windows.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ jobs:
1414
scoop update
1515
scoop install 7zip
1616
scoop checkup
17-
scoop install unrar
1817
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
1918
shell: powershell
2019

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
[![tests][tests-src]][tests-href]
1010
[![codecov][codecov-src]][codecov-href]
1111

12-
PHP package to handle archives (`.zip`, `.rar`, `.tar`, `.7z`) or `.pdf` with hybrid solution (native/`p7zip`), designed to works with eBooks (`.epub`, `.cbz`, `.cbr`, `.cb7`, `.cbt`).
12+
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`).
1313

1414
Supports Linux, macOS and Windows.
1515

@@ -252,15 +252,15 @@ The MIT License (MIT). Please see [License File](LICENSE.md) for more informatio
252252

253253
[<img src="https://user-images.githubusercontent.com/48261459/201463225-0a5a084e-df15-4b11-b1d2-40fafd3555cf.svg" height="120rem" width="100%" />](https://github.com/kiwilan)
254254

255-
[version-src]: https://img.shields.io/packagist/v/kiwilan/php-archive.svg?style=flat-square&colorA=18181B&colorB=777BB4
255+
[version-src]: https://img.shields.io/packagist/v/kiwilan/php-archive.svg?style=flat&colorA=18181B&colorB=777BB4
256256
[version-href]: https://packagist.org/packages/kiwilan/php-archive
257-
[php-version-src]: https://img.shields.io/static/v1?style=flat-square&label=PHP&message=v8.1&color=777BB4&logo=php&logoColor=ffffff&labelColor=18181b
257+
[php-version-src]: https://img.shields.io/static/v1?style=flat&label=PHP&message=v8.1&color=777BB4&logo=php&logoColor=ffffff&labelColor=18181b
258258
[php-version-href]: https://www.php.net/
259-
[downloads-src]: https://img.shields.io/packagist/dt/kiwilan/php-archive.svg?style=flat-square&colorA=18181B&colorB=777BB4
259+
[downloads-src]: https://img.shields.io/packagist/dt/kiwilan/php-archive.svg?style=flat&colorA=18181B&colorB=777BB4
260260
[downloads-href]: https://packagist.org/packages/kiwilan/php-archive
261-
[license-src]: https://img.shields.io/github/license/kiwilan/php-archive.svg?style=flat-square&colorA=18181B&colorB=777BB4
261+
[license-src]: https://img.shields.io/github/license/kiwilan/php-archive.svg?style=flat&colorA=18181B&colorB=777BB4
262262
[license-href]: https://github.com/kiwilan/php-archive/blob/main/README.md
263-
[tests-src]: https://img.shields.io/github/actions/workflow/status/kiwilan/php-archive/run-tests.yml?branch=main&label=tests&style=flat-square&colorA=18181B
263+
[tests-src]: https://img.shields.io/github/actions/workflow/status/kiwilan/php-archive/run-tests.yml?branch=main&label=tests&style=flat&colorA=18181B
264264
[tests-href]: https://github.com/kiwilan/php-archive/actions/workflows/run-tests.yml
265-
[codecov-src]: https://codecov.io/gh/kiwilan/php-archive/branch/main/graph/badge.svg?token=P9XIK2KV9G
265+
[codecov-src]: https://img.shields.io/codecov/c/gh/kiwilan/php-archive/main?style=flat&colorA=18181B&colorB=777BB4
266266
[codecov-href]: https://codecov.io/gh/kiwilan/php-archive

composer.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "kiwilan/php-archive",
3-
"version": "2.1.02",
4-
"description": "PHP package to handle archives (.zip, .rar, .tar, .7z) or .pdf with hybrid solution (native/p7zip), designed to works with eBooks (.epub, .cbz, .cbr, .cb7, .cbt).",
3+
"version": "2.2.0",
4+
"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).",
55
"keywords": [
66
"php",
77
"archive",
@@ -34,8 +34,7 @@
3434
"require": {
3535
"php": "^8.1",
3636
"smalot/pdfparser": "^2.4",
37-
"spatie/temporary-directory": "^2.1",
38-
"symfony/process": "^6.2"
37+
"spatie/temporary-directory": "^2.1"
3938
},
4039
"require-dev": {
4140
"laravel/pint": "^1.6",

src/ArchiveTemporaryDirectory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ protected function __construct(
1313
) {
1414
}
1515

16-
public static function make(string $filename = null): self
16+
public static function make(?string $filename = null): self
1717
{
1818
return new self(uniqid(), $filename);
1919
}

src/Enums/ArchiveEnum.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ enum ArchiveEnum: string
1010
case rar = 'rar';
1111
case pdf = 'pdf';
1212

13-
public static function fromExtension(string $extension, string $mimeType = null): self
13+
public static function fromExtension(string $extension, ?string $mimeType = null): self
1414
{
1515
$extension = strtolower($extension);
1616
if (str_contains($extension, '.')) {

src/Models/ArchiveItem.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function __construct(
3434
) {
3535
}
3636

37-
public static function fromP7zip(array $data, string $archivePath = null): self
37+
public static function fromP7zip(array $data, ?string $archivePath = null): self
3838
{
3939
if (empty($data)) {
4040
throw new \Exception('No data provided.');

src/Processes/SevenZipProcess.php

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
use Kiwilan\Archive\ArchiveTemporaryDirectory;
77
use Kiwilan\Archive\Models\ArchiveItem;
88
use Kiwilan\Archive\Readers\BaseArchive;
9-
use Symfony\Component\Process\Exception\ProcessFailedException;
10-
use Symfony\Component\Process\Process;
119

1210
class SevenZipProcess
1311
{
@@ -40,21 +38,62 @@ public static function make(string $path): self
4038
return $self;
4139
}
4240

41+
public static function test(bool $exception = true): bool
42+
{
43+
exec('7z', $output, $res);
44+
// $process = new Process(['7z']);
45+
// $process->run();
46+
47+
$isValid = $res === 0;
48+
// $isValid = $process->isSuccessful();
49+
50+
// check if 7z is installed
51+
if (! $isValid) {
52+
if ($exception) {
53+
$osFamily = PHP_OS_FAMILY;
54+
$isDarwin = $osFamily === 'Darwin';
55+
$message = "p7zip is not installed or not in the PATH. Please install p7zip and try again.\nYou can check this guide: https://gist.github.com/ewilan-riviere/85d657f9283fa6af255531d97da5d71d";
56+
57+
if ($isDarwin) {
58+
$message .= "\nYou have to install `rar` binary with brew on macOS.";
59+
}
60+
61+
throw new Exception($message);
62+
}
63+
64+
return false;
65+
}
66+
67+
return true;
68+
}
69+
4370
/**
4471
* @param string[] $args
72+
* @return string[]
4573
*/
46-
public function execute(string $command, array $args): string
74+
public function execute(string $command, array $args): array
4775
{
48-
BaseArchive::binaryP7zipTest();
76+
SevenZipProcess::test();
77+
78+
$command = "{$command} ".implode(' ', $args);
4979

50-
$process = new Process([$command, ...$args]);
51-
$process->run();
80+
// $process = new Process([$command, ...$args]);
81+
// $process->run();
5282

53-
if (! $process->isSuccessful()) {
54-
throw new ProcessFailedException($process);
83+
// if (! $process->isSuccessful()) {
84+
// throw new ProcessFailedException($process);
85+
// }
86+
87+
try {
88+
exec($command, $output, $res);
89+
} catch (\Throwable $th) {
90+
throw new \Error($th->getMessage());
5591
}
5692

57-
return $process->getOutput();
93+
// $output = explode(PHP_EOL, $output);
94+
array_unshift($output, '');
95+
96+
return $output;
5897
}
5998

6099
/**
@@ -64,9 +103,6 @@ public function list(): array
64103
{
65104
$output = $this->execute('7z', ['l', '-ba', '-slt', $this->path]);
66105

67-
$output = explode(PHP_EOL, $output);
68-
array_unshift($output, '');
69-
70106
$temp = [];
71107
foreach ($output as $string) {
72108
if (empty($string)) {
@@ -96,8 +132,13 @@ public function list(): array
96132
$key = array_key_exists(0, $data) ? $data[0] : null;
97133
$value = array_key_exists(1, $data) ? $data[1] : null;
98134

99-
$key = trim($key);
100-
$value = trim($value);
135+
if ($key) {
136+
$key = trim($key);
137+
}
138+
139+
if ($value) {
140+
$value = trim($value);
141+
}
101142

102143
$item[$key] = $value;
103144
}
@@ -115,7 +156,7 @@ public function list(): array
115156
/**
116157
* @param ArchiveItem[] $files
117158
*/
118-
public function extract(string $toPath, array $files = null): bool
159+
public function extract(string $toPath, ?array $files = null): bool
119160
{
120161
if ($this->isRar && $this->isDarwin) {
121162
if ($files) {

src/Readers/ArchiveRar.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function getText(ArchiveItem $file): ?string
8484
throw new \Exception("Error, {$file->getFilename()} is an image");
8585
}
8686

87-
return $this->getContent($file);
87+
return $this->getContents($file);
8888
}
8989

9090
private function parse(): static

src/Readers/ArchiveSevenZip.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function getText(ArchiveItem $file): ?string
4545
throw new \Exception("Error, {$file->getFilename()} is an image");
4646
}
4747

48-
return $this->getContent($file);
48+
return $this->getContents($file);
4949
}
5050

5151
public function extract(string $toPath, array $files): array

src/Readers/ArchiveZip.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function extract(string $toPath, array $files): array
2323
$paths = [];
2424
$this->parser(function (ArchiveItem $item, ZipArchive $archive, int $i) use (&$files, $toPath, &$paths) {
2525
if (in_array($item, $files)) {
26-
$content = $this->getContent($item);
26+
$content = $this->getContents($item);
2727
$toPathFile = "{$toPath}{$item->getRootPath()}";
2828

2929
if (! is_dir(dirname($toPathFile))) {
@@ -82,7 +82,7 @@ public function getText(ArchiveItem $file): ?string
8282
throw new \Exception("Error, {$file->getFilename()} is an image");
8383
}
8484

85-
return $this->getContent($file);
85+
return $this->getContents($file);
8686
}
8787

8888
private function parse(): static

0 commit comments

Comments
 (0)