Skip to content

Commit 5b703b0

Browse files
committed
add __invoke support
1 parent e078e9d commit 5b703b0

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

src/Unity.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
namespace rabbit\pool;
55

6-
use common\Ldap\Ldap;
76
use rabbit\App;
87
use rabbit\exception\NotSupportedException;
98

@@ -25,6 +24,26 @@ public function __construct(BasePool $pool)
2524
$this->pool = $pool;
2625
}
2726

27+
/**
28+
* @param callable $function
29+
* @throws \rabbit\core\Exception
30+
*/
31+
public function __invoke(callable $function)
32+
{
33+
$client = $this->pool->get();
34+
if ($client instanceof IUnity) {
35+
$client = $client->build();
36+
}
37+
try {
38+
return call_user_func($function, $client);
39+
} catch (Throwable $exception) {
40+
App::error($exception->getMessage());
41+
throw $exception;
42+
} finally {
43+
$this->pool->release($client);
44+
}
45+
}
46+
2847
/**
2948
* @param $name
3049
* @param $arguments

0 commit comments

Comments
 (0)