Skip to content

Commit d0efa87

Browse files
authored
Merge pull request #2 from ace411/v0.x
v0.x changes
2 parents 5e99cd0 + 3d5a7d6 commit d0efa87

File tree

16 files changed

+379
-501
lines changed

16 files changed

+379
-501
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jobs:
66
strategy:
77
fail-fast: false
88
matrix:
9-
php: ['7.2', '7.3', '7.4', '8.0']
9+
php: ['7.2', '7.3', '7.4', '8.0', '8.1', '8.2']
1010
name: PHP ${{ matrix.php }}
1111
steps:
1212
- uses: actions/checkout@v1

README.md

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -36,30 +36,27 @@ $ composer require chemem/asyncify
3636
If you want to take a Functional Programming approach, facilitated by currying, the example below should suffice.
3737

3838
```php
39-
use React\EventLoop\Loop;
4039
use function Chemem\Asyncify\call;
4140

42-
$call = call(Loop::get(), __DIR__);
43-
44-
$exec = $call('file_get_contents', ['foo.txt'])->then(
45-
function (?string $contents) {
46-
echo $contents;
47-
},
48-
function (\Throwable $err) {
49-
echo $err->getMessage();
50-
}
51-
);
41+
$call = call('file_get_contents', ['foo.txt'])
42+
->then(
43+
function (?string $contents) {
44+
echo $contents;
45+
},
46+
function (\Throwable $err) {
47+
echo $err->getMessage();
48+
}
49+
);
5250
```
5351

5452
### Or
5553

5654
If you prefer a more conventional OOP approach, the snippet below should prove apt.
5755

5856
```php
59-
use React\EventLoop\Loop;
6057
use Chemem\Asyncify\Async;
6158

62-
$exec = Async::create(Loop::get())
59+
$exec = Async::create()
6360
->call('file_get_contents', ['foo.txt'])
6461
->then(
6562
function (?string $contents) {
@@ -71,9 +68,11 @@ $exec = Async::create(Loop::get())
7168
);
7269
```
7370

74-
Though both examples in the antecedent text utilize the new default loop, the old standard with the `Factory::create()` and `$loop->run()` artifacts is still supported.
71+
The examples directory contains more nuanced uses of the library that I recommend you check out.
7572

76-
**Note:** The examples directory contains more nuanced uses of the library that I recommend you check out.
73+
## Limitation
74+
75+
`asyncify` is no panacea, but is capable of asynchronously executing a plethora of blocking calls. As presently constituted, the library is incapable of processing inputs and outputs that cannot be serialized.
7776

7877
## API Reference
7978

@@ -83,8 +82,8 @@ Though both examples in the antecedent text utilize the new default loop, the ol
8382
class Async {
8483

8584
/* Methods */
86-
public static create( LoopInterface $loop [, ?string $rootDir = null ] ) : Async;
87-
public function call( string $function [, array $args ] ) : PromiseInterface;
85+
public static create( ?string $autoload = null [, ?React\EventLoop\LoopInterface $rootDir = null ] ) : Async;
86+
public function call( string $function [, array $args ] ) : React\Promise\PromiseInterface;
8887
}
8988
```
9089

@@ -95,14 +94,12 @@ class Async {
9594
### Function
9695

9796
```php
98-
call ( mixed ...$args ) : callable
99-
100-
asyncify ( LoopInterface $loop [, ?string $rootDir [, string $func [, array $args ] ] ] ) : PromiseInterface
97+
call ( string $func [, array $args [, ?string $autoload = null [, ?React\EventLoop\LoopInterface $args = null ] ] ] ) : React\Promise\PromiseInterface;
10198
```
10299

103-
`call` - Curryied version of asyncify (bootstraps asynchronous function calls)
100+
`call` - Curryied function that bootstraps asynchronous function calls
104101

105-
`asyncify` - Runs a synchronous PHP function asynchronously
102+
> **Note:** `asyncify` utilizes the autoload file in the root directory of the project from which it is invoked.
106103
107104
## Dealing with problems
108105

composer.json

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "chemem/asyncify",
3-
"type": "library",
43
"description": "A package that runs synchronous PHP functions asynchronously.",
54
"license": "Apache-2.0",
5+
"type": "library",
66
"authors": [
77
{
88
"name": "Lochemem Bruno Michael",
@@ -18,25 +18,29 @@
1818
},
1919
"require-dev": {
2020
"ergebnis/composer-normalize": "~2",
21-
"friendsofphp/php-cs-fixer": "~2",
21+
"friendsofphp/php-cs-fixer": "~2 || ~3",
2222
"phpunit/phpunit": "~8 || ~9",
23-
"seregazhuk/react-promise-testing": "~0"
23+
"react/async": "~3 || ~4"
2424
},
25+
"minimum-stability": "stable",
2526
"autoload": {
2627
"psr-4": {
2728
"Chemem\\Asyncify\\": "src/"
2829
},
2930
"files": [
30-
"src/Internal/functions.php",
31-
"src/functions.php"
31+
"src/index.php"
3232
]
3333
},
3434
"autoload-dev": {
3535
"psr-4": {
3636
"Chemem\\Asyncify\\Tests\\": "tests/"
3737
}
3838
},
39-
"minimum-stability": "stable",
39+
"config": {
40+
"allow-plugins": {
41+
"ergebnis/composer-normalize": true
42+
}
43+
},
4044
"scripts": {
4145
"cs:fix": "php-cs-fixer fix --config=.php-cs-fixer.php --diff --verbose --allow-risky=yes",
4246
"test": "phpunit -c phpunit.xml.dist"

examples/arbitrary.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
/**
44
* simple script that runs a user-defined function asynchronously and wraps its return value in a promise
5-
* -> works only if the arbitrary function is expressed as a string
5+
* -> works only if the arbitrary function is expressed as an anonymous function - in a string
66
*
77
* @package chemem/asyncify
88
* @author Lochemem Bruno Michael
@@ -14,6 +14,7 @@
1414
require __DIR__ . '/../vendor/autoload.php';
1515

1616
use React\EventLoop\Loop;
17+
1718
use function Chemem\Asyncify\call;
1819

1920
$square = <<<'CODE'
@@ -22,13 +23,12 @@
2223
})
2324
CODE;
2425

25-
$call = call(Loop::get());
26-
27-
$proc = $call($square, [12])->then(
28-
function (int $square) {
29-
echo $square . PHP_EOL;
30-
},
31-
function ($err) {
32-
echo $err->getMessage() . PHP_EOL;
33-
}
34-
);
26+
$call = call($square, [12])
27+
->then(
28+
function (int $square) {
29+
echo $square . PHP_EOL;
30+
},
31+
function ($err) {
32+
echo $err->getMessage() . PHP_EOL;
33+
}
34+
);

examples/multiple.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515

1616
use React\EventLoop\Loop;
1717
use Chemem\Asyncify\Async;
18+
1819
use function React\Promise\all;
1920

2021
const BASE_URI = 'https://jsonplaceholder.typicode.com/';
2122

22-
$async = Async::create(Loop::get());
23+
$async = Async::create();
2324

2425
$proc = all([
2526
$async->call('file_get_contents', [BASE_URI . 'todos/1']),

examples/readFile.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,15 @@
1414
require __DIR__ . '/../vendor/autoload.php';
1515

1616
use React\EventLoop\Loop;
17-
use function Chemem\Asyncify\call;
1817

19-
$call = call(Loop::get());
18+
use function Chemem\Asyncify\call;
2019

21-
$proc = $call('file_get_contents', [])
20+
$call = call('file_get_contents', [])
2221
->then(
23-
function (?string $result) {
24-
\var_dump($result);
22+
function (?int $result) {
23+
echo \sprintf("Wrote %d bytes\n", $result);
2524
},
2625
function (\Throwable $err) {
27-
echo $err->getMessage();
26+
echo $err->getMessage() . PHP_EOL;
2827
}
2928
);

src/Async.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use React\EventLoop\LoopInterface;
1616
use React\Promise\PromiseInterface;
1717

18+
use function Chemem\Asyncify\Internal\asyncify;
19+
1820
class Async
1921
{
2022
/**
@@ -25,16 +27,16 @@ class Async
2527
private $loop;
2628

2729
/**
28-
* project root directory
30+
* path to autoloader
2931
*
30-
* @var string $rootDir
32+
* @var string $autoload
3133
*/
32-
private $rootDir;
34+
private $autoload;
3335

34-
public function __construct(LoopInterface $loop, ?string $rootDir = null)
36+
public function __construct(?string $autoload = null, ?LoopInterface $loop = null)
3537
{
36-
$this->loop = $loop;
37-
$this->rootDir = $rootDir;
38+
$this->loop = $loop;
39+
$this->autoload = $autoload;
3840
}
3941

4042
/**
@@ -44,18 +46,17 @@ public function __construct(LoopInterface $loop, ?string $rootDir = null)
4446
* create :: Object -> String -> Object
4547
*
4648
* @param LoopInterface $loop
47-
* @param string $rootDir
49+
* @param string $autoload
4850
* @return Async
4951
*/
50-
public static function create(LoopInterface $loop, ?string $rootDir = null): Async
52+
public static function create(?string $autoload = null, ?LoopInterface $loop = null): Async
5153
{
52-
return new static($loop, $rootDir);
54+
return new static($autoload, $loop);
5355
}
5456

5557
/**
5658
* call
57-
* asynchronously calls a synchronous PHP function
58-
* -> returns result subsumed in promise
59+
* asynchronously calls a synchronous PHP function and subsumes result in promise
5960
*
6061
* call :: String -> Array -> Promise s a
6162
*
@@ -65,6 +66,6 @@ public static function create(LoopInterface $loop, ?string $rootDir = null): Asy
6566
*/
6667
public function call(string $function, array $args): PromiseInterface
6768
{
68-
return asyncify($this->loop, $this->rootDir, $function, $args);
69+
return asyncify($function, $args, $this->autoload, $this->loop);
6970
}
7071
}

src/Internal/functions.php

Lines changed: 0 additions & 100 deletions
This file was deleted.

0 commit comments

Comments
 (0)