Skip to content

Commit

Permalink
upgrade to amphp/amp ^3.0, migrate to fibers!
Browse files Browse the repository at this point in the history
- removed continueScan from Session, as the command is not supported there
- add unit tests
  • Loading branch information
Pato05 committed Mar 8, 2024
1 parent 4ba8077 commit ea533dd
Show file tree
Hide file tree
Showing 19 changed files with 398 additions and 3,756 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/vendor/
.php-cs-fixer.cache
.phpunit.result.cache
composer.lock
5 changes: 3 additions & 2 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

$config = new Amp\CodeStyle\Config;
$config->getFinder()
->in(__DIR__ . "/examples")
->in(__DIR__ . "/lib");
->in(__DIR__ . '/examples')
->in(__DIR__ . '/lib')
->in(__DIR__ . '/tests');

$config->setCacheFile(__DIR__ . '/.php-cs-fixer.cache');

Expand Down
75 changes: 13 additions & 62 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
# amphp-clamav

![license: MIT](https://img.shields.io/badge/license-MIT-blue)

An asynchronous ClamAV wrapper written with amphp/socket

## Migrating from v1.x.x

The updated v2.0.0 brought some breaking changes because of the changes in the underlying `amphp/amp`. These underlying changes give a great boost to the asynchronous event loop, being it now based on `Fibers` instead of `Generators`.

Mostly you only need to remove the `yield` keyword before any calls to the library's asynchronous function. Learn more on [Amp's Upgrade Guide](https://amphp.org/upgrade).

## Installing

```
Expand All @@ -10,66 +18,9 @@ composer require pato05/amphp-clamav

## Examples

Ping and scan of a file/directory

[`examples/scan.php`](https://github.com/Pato05/amphp-clamav/blob/main/examples/scan.php):

```php
<?php
require_once __DIR__ . '/../vendor/autoload.php';

use Amp\ClamAV;
use Amp\Loop;

Loop::run(function () {
echo 'connecting...' . PHP_EOL;

if (yield ClamAV\ping()) {
echo 'connected successfully!' . PHP_EOL;
} else {
echo 'connection failed!' . PHP_EOL;
return;
}
echo 'running test scan...' . PHP_EOL;

/** @var ClamAV\ScanResult */
$result = yield ClamAV\scan('/tmp/eicar.com');
echo (string) $result . PHP_EOL;
});
```

Scanning from a `InputStream` (in this case a `File` instance which implements `InputStream`)
Ping and scan of a file/directory: [`examples/scan.php`](https://github.com/Pato05/amphp-clamav/blob/main/examples/scan.php)

[`examples/scan_stream.php`](https://github.com/Pato05/amphp-clamav/blob/main/examples/scan_stream.php):

```php
<?php

require_once __DIR__ . '/../vendor/autoload.php';

use Amp\ClamAV;
use Amp\Loop;

Loop::run(function () {
echo 'connecting...' . PHP_EOL;

if (yield ClamAV\ping()) {
echo 'connected!' . PHP_EOL;
} else {
echo 'connection failed.' . PHP_EOL;
return;
}
echo 'running a streamed scan...' . PHP_EOL;

/** @var \Amp\File\File */
$file = yield \Amp\File\openFile('/tmp/eicar.com', 'r');

/** @var \Amp\ClamAV\ScanResult */
$res = yield ClamAV\scanFromStream($file);
yield $file->close(); // always close files to avoid memory leaks
echo (string) $res . PHP_EOL;
});
```
Scanning from a `ReadableStream` (in this case a `File` instance which implements `ReadableStream`): [`examples/scan_stream.php`](https://github.com/Pato05/amphp-clamav/blob/main/examples/scan_stream.php)

## Using a TCP/IP socket instead

Expand All @@ -90,7 +41,7 @@ MULTISCAN is supported but can only be used on non-session instances (due to a C
MULTISCAN allows you to make a multithreaded scan.

```php
$result = yield \Amp\ClamAV\multiScan('FILEPATH');
$result = \Amp\ClamAV\multiScan('FILEPATH');
```

## Differences between running a session and without
Expand All @@ -100,13 +51,13 @@ Sessions run on the same socket connection, while non-session instances will rec
Instantiating a session is pretty straight forward, just use the `ClamAV::session()` method like this:

```php
$clamSession = yield \Amp\ClamAV\session();
$clamSession = \Amp\ClamAV\session();
```

Though you MUST end every session by using the method `Session::end()`:

```php
yield $clamSession->end();
$clamSession->end();
```

Be aware that in a session you can only execute ONE COMMAND AT A TIME, therefore, if you want to run more than one command in parallel, use the main `ClamAV` class instead.
Expand Down
76 changes: 40 additions & 36 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,41 +1,45 @@
{
"name": "pato05/amphp-clamav",
"description": "An asynchronous clamd wrapper written with amphp/socket",
"type": "library",
"require": {
"php": "^8.0",
"amphp/amp": "^2.6",
"amphp/socket": "^1.2",
"ext-sockets": "*"
"name": "pato05/amphp-clamav",
"description": "An asynchronous clamd wrapper written with amphp/socket",
"type": "library",
"require": {
"php": "^8.0",
"amphp/amp": "^3.0",
"amphp/socket": "^2.0",
"ext-sockets": "*"
},
"license": "MIT",
"autoload": {
"psr-4": {
"Amp\\ClamAV\\": "lib/"
},
"license": "MIT",
"autoload": {
"psr-4": {
"Amp\\ClamAV\\": "lib/"
},
"files": [
"lib/functions.php"
]
},
"authors": [
{
"name": "Pato05",
"email": "pato05mc@gmail.com"
}
"files": [
"lib/functions.php"
]
},
"authors": [
{
"name": "Pato05",
"email": "pato05mc@gmail.com"
}
],
"require-dev": {
"amphp/php-cs-fixer-config": "^2.0",
"amphp/file": "^3.0",
"amphp/phpunit-util": "^3.0",
"phpunit/phpunit": "^9.6"
},
"scripts": {
"check": [
"@fix",
"@test"
],
"require-dev": {
"amphp/php-cs-fixer-config": "dev-master",
"amphp/file": "^2.0",
"phabel/phabel": "1.0.49.80"
},
"config": {
"allow-plugins": {
"phabel/phabel": true
}
},
"extra": {
"phabel": {
"revision": 0
}
"fix": "@php ./vendor/bin/php-cs-fixer fix",
"test": "@php -dzend.assertions=1 -dassert.exception=1 -dxdebug.mode=coverage ./vendor/bin/phpunit --coverage-text"
},
"config": {
"allow-plugins": {
"phabel/phabel": false
}
}
}
Loading

0 comments on commit ea533dd

Please sign in to comment.