Skip to content

Commit 28fe002

Browse files
committed
refactor: replace bingo-functional helpers
- replace filePath and curry functions with similarly named local functions - remove redundant imports
1 parent 16481a3 commit 28fe002

File tree

6 files changed

+93
-61
lines changed

6 files changed

+93
-61
lines changed

src/Async.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919

2020
use function Chemem\Asyncify\Internal\asyncify;
2121
use function Chemem\Asyncify\Internal\thread;
22-
use function Chemem\Bingo\Functional\extend;
23-
use function Chemem\Bingo\Functional\filePath;
22+
use function Chemem\Asyncify\Internal\Functional\filepath;
2423

2524
use const Chemem\Asyncify\Internal\PHP_THREADABLE;
2625

@@ -55,7 +54,7 @@ public function __construct(?string $autoload = null, ?LoopInterface $loop = nul
5554
if (PHP_THREADABLE) {
5655
$this->runtime = new Runtime(
5756
new EventLoopBridge($this->loop),
58-
$this->autoload ?? filePath(0, 'vendor/autoload.php')
57+
$this->autoload ?? filepath(0, 'vendor/autoload.php')
5958
);
6059
}
6160
}
@@ -114,13 +113,13 @@ public function call($function, array $args): PromiseInterface
114113

115114
return PHP_THREADABLE ?
116115
thread(
117-
...extend(
116+
...\array_merge(
118117
$params,
119118
[$this->runtime]
120119
)
121120
) :
122121
asyncify(
123-
...extend(
122+
...\array_merge(
124123
$params,
125124
[
126125
$this->autoload,

src/call.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
use ReactParallel\EventLoop\EventLoopBridge;
1717
use ReactParallel\Runtime\Runtime;
1818

19-
use function Chemem\Bingo\Functional\curry;
20-
use function Chemem\Bingo\Functional\filePath;
19+
use function Chemem\Asyncify\Internal\Functional\curry;
20+
use function Chemem\Asyncify\Internal\Functional\filepath;
2121

2222
use const Chemem\Asyncify\Internal\asyncify;
2323
use const Chemem\Asyncify\Internal\thread;
@@ -59,7 +59,7 @@ function call(...$args)
5959
new EventLoopBridge(
6060
$args[3] ?? Loop::get()
6161
),
62-
$args[2] ?? filePath(0, 'vendor/autoload.php')
62+
$args[2] ?? filepath(0, 'vendor/autoload.php')
6363
);
6464
}
6565

src/index.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
<?php
22

33
/**
4-
* library entrypoint
4+
* library entry point
55
*
66
* @package chemem/asyncify
77
* @author Lochemem Bruno Michael
88
* @license Apache-2.0
99
*/
1010

11+
require_once __DIR__ . '/internal/functional/curry.php';
12+
require_once __DIR__ . '/internal/functional/filepath.php';
1113
require_once __DIR__ . '/internal/asyncify.php';
1214
require_once __DIR__ . '/internal/constants.php';
1315
require_once __DIR__ . '/internal/proc.php';

src/internal/asyncify.php

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

18-
use function Chemem\Bingo\Functional\concat;
19-
use function Chemem\Bingo\Functional\filePath;
20-
use function Chemem\Bingo\Functional\head;
21-
use function Chemem\Bingo\Functional\partial;
18+
use function Chemem\Asyncify\Internal\Functional\filepath;
2219
use function React\Promise\reject;
2320
use function React\Promise\resolve;
2421

25-
use const Chemem\Bingo\Functional\toException;
26-
2722
const asyncify = __NAMESPACE__ . '\\asyncify';
2823

2924
/**
@@ -65,10 +60,8 @@ function asyncify(
6560
\sprintf(
6661
PHP_EXECUTABLE_TEMPLATE,
6762
// path to autoloader
68-
$autoload ?? filePath(0, 'vendor/autoload.php'),
69-
// composable exception handler
70-
toException,
71-
// format inline functions
63+
$autoload ?? filepath(0, 'vendor/autoload.php'),
64+
// arbitrary function
7265
$function,
7366
// utilize only array values as arguments
7467
\base64_encode(

src/internal/constants.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,29 +35,29 @@
3535
function (...$args) {
3636
[$errno, $errmsg] = $args;
3737
throw new \Exception($errmsg, $errno);
38-
},
39-
E_ALL
38+
}
4039
);
4140
\set_exception_handler(
4241
function (Throwable $err) {
4342
echo $err->getMessage();
4443
}
4544
);
4645
require_once "%s";
46+
$result = null;
47+
try {
48+
$result = (
49+
function (...$args) {
50+
return %s(...$args);
51+
}
52+
)(
53+
...\unserialize(
54+
\base64_decode("%s")
55+
)
56+
);
57+
} catch (\Throwable $err) {
58+
$result = $err;
59+
}
4760
echo \base64_encode(
48-
\serialize(
49-
%s(
50-
function (...$args) {
51-
return %s(...$args);
52-
},
53-
function ($err) {
54-
return new \Exception(
55-
$err->getMessage(),
56-
$err->getCode(),
57-
$err->getPrevious()
58-
);
59-
}
60-
)(...\unserialize(\base64_decode("%s")))
61-
)
61+
\serialize($result)
6262
);
6363
PHP;

tests/AsyncTest.php

Lines changed: 64 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use PHPUnit\Framework\TestCase;
99

1010
use function Chemem\Asyncify\call;
11-
use function Chemem\Bingo\Functional\toException;
1211
use function React\Async\await;
1312

1413
use const Chemem\Asyncify\Internal\PHP_THREADABLE;
@@ -88,6 +87,23 @@ function (int $next) {
8887
[2]
8988
],
9089
'/(Invalid argument)/i'
90+
],
91+
[
92+
[
93+
(
94+
PHP_THREADABLE ?
95+
function (int $val) {
96+
if ($next < 10) {
97+
\trigger_error('Value is less than 10');
98+
99+
return $val * 2;
100+
}
101+
} :
102+
'(function (int $x) { if ($x < 10) { \trigger_error("Value is less than 10"); return $x; } return $x * 2; })'
103+
),
104+
[2]
105+
],
106+
'/(Value is less than 10)/i'
91107
]
92108
];
93109
}
@@ -97,20 +113,33 @@ function (int $next) {
97113
*/
98114
public function testcallRunsSynchronousPHPFunctionAsynchronously($args, $result): void
99115
{
100-
$exec = toException(
101-
function (...$args) {
102-
return await(call(...$args));
103-
},
104-
function (\Throwable $err) {
105-
return $err->getMessage();
106-
}
107-
)(...$args);
116+
$exec = null;
117+
try {
118+
$exec = await(
119+
call(...$args)
120+
);
121+
} catch (\Throwable $err) {
122+
$exec = $err->getMessage();
123+
}
124+
125+
$this->assertTrue(
126+
call($args[0]) instanceof \Closure
127+
);
108128

109129
if (\is_string($result)) {
110-
$this->assertMatchesRegularExpression(
111-
$result,
112-
$exec
113-
);
130+
if (PHP_VERSION_ID < 73000) {
131+
$this->assertTrue(
132+
(bool) \preg_match(
133+
$result,
134+
$exec
135+
)
136+
);
137+
} else {
138+
$this->assertMatchesRegularExpression(
139+
$result,
140+
$exec
141+
);
142+
}
114143
} else {
115144
$this->assertEquals(
116145
$result,
@@ -124,21 +153,30 @@ function (\Throwable $err) {
124153
*/
125154
public function testAsynccallMethodRunsSynchronousPHPFunctionAsynchronously($args, $result): void
126155
{
127-
$exec = toException(
128-
function (...$args) {
129-
$async = Async::create();
130-
return await($async->call(...$args));
131-
},
132-
function (\Throwable $err) {
133-
return $err->getMessage();
134-
}
135-
)(...$args);
156+
$exec = null;
157+
try {
158+
$async = Async::create();
159+
$exec = await(
160+
$async->call(...$args)
161+
);
162+
} catch (\Throwable $err) {
163+
$exec = $err->getMessage();
164+
}
136165

137166
if (\is_string($result)) {
138-
$this->assertMatchesRegularExpression(
139-
$result,
140-
$exec
141-
);
167+
if (PHP_VERSION_ID < 73000) {
168+
$this->assertTrue(
169+
(bool) \preg_match(
170+
$result,
171+
$exec
172+
)
173+
);
174+
} else {
175+
$this->assertMatchesRegularExpression(
176+
$result,
177+
$exec
178+
);
179+
}
142180
} else {
143181
$this->assertEquals(
144182
$result,

0 commit comments

Comments
 (0)