-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from darkwood-fr/spatie-driver
✨ Add Flow\Driver\SpatieDriver
- Loading branch information
Showing
14 changed files
with
218 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
declare(ticks=1000); | ||
|
||
namespace Flow\Driver; | ||
|
||
use Closure; | ||
use Flow\DriverInterface; | ||
use Flow\Exception\RuntimeException; | ||
use RuntimeException as NativeRuntimeException; | ||
use Spatie\Async\Pool; | ||
use Throwable; | ||
|
||
/** | ||
* @template TArgs | ||
* @template TReturn | ||
* | ||
* @implements DriverInterface<TArgs,TReturn> | ||
*/ | ||
class SpatieDriver implements DriverInterface | ||
{ | ||
/** | ||
* @var array<string> | ||
*/ | ||
private array $ticksIds = []; | ||
|
||
private Pool $pool; /** @phpstan-ignore-line */ | ||
public function __construct() | ||
{ | ||
if (!class_exists('Spatie\\Async\\Pool')) { | ||
throw new NativeRuntimeException('Spatie Async is not loaded. Suggest install it with composer require spatie/async'); | ||
} | ||
|
||
$this->pool = Pool::create(); | ||
if (!$this->pool->isSupported()) { | ||
throw new NativeRuntimeException('Spatie Async will not run asynchronously. PHP PCNTL extension is required'); | ||
} | ||
} | ||
|
||
public function __serialize() | ||
{ | ||
return []; | ||
} | ||
|
||
/** | ||
* @param array<mixed> $data | ||
*/ | ||
public function __unserialize(array $data) | ||
{ | ||
$this->pool = Pool::create(); // @phpstan-ignore-line | ||
} | ||
|
||
public function async(Closure $callback, Closure $onResolve = null): Closure | ||
{ | ||
return function (...$args) use ($callback, $onResolve): void { | ||
$this->pool->add(static function () use ($callback, $args) {// @phpstan-ignore-line | ||
return $callback(...$args, ...($args = [])); | ||
})->then(static function ($return) use ($onResolve) { | ||
if ($onResolve) { | ||
$onResolve($return); | ||
} | ||
})->catch(static function (Throwable $exception) use ($onResolve) { | ||
if ($onResolve) { | ||
$onResolve(new RuntimeException($exception->getMessage(), $exception->getCode(), $exception)); | ||
} | ||
}); | ||
}; | ||
} | ||
|
||
public function delay(float $seconds): void | ||
{ | ||
sleep((int) $seconds); | ||
} | ||
|
||
public function tick(int $interval, Closure $callback): Closure | ||
{ | ||
$tickId = uniqid('flow_spatie_tick_id'); | ||
|
||
$closure = static fn () => $callback(); | ||
register_tick_function($closure); | ||
|
||
$cancel = function () use ($tickId, $closure) { | ||
unset($this->ticksIds[$tickId]); | ||
unregister_tick_function($closure); | ||
}; | ||
|
||
$this->ticksIds[$tickId] = $cancel; | ||
|
||
return $cancel; | ||
} | ||
|
||
public function start(): void | ||
{ | ||
$this->pool->wait(); // @phpstan-ignore-line | ||
} | ||
|
||
public function stop(): void | ||
{ | ||
foreach ($this->ticksIds as $cancel) { | ||
$cancel(); | ||
} | ||
|
||
$this->pool->stop(); // @phpstan-ignore-line | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Flow\Test\Driver; | ||
|
||
use Flow\Driver\SpatieDriver; | ||
use Flow\DriverInterface; | ||
|
||
/** | ||
* @template T1 | ||
* @template T2 | ||
* | ||
* @extends DriverTestCase<T1,T2> | ||
*/ | ||
class SpatieDriverTest extends DriverTestCase | ||
{ | ||
public function testAsync(): void | ||
{ | ||
self::assertTrue(true); | ||
} | ||
|
||
public function testAsyncReturn(): void | ||
{ | ||
self::assertTrue(true); | ||
} | ||
|
||
public function testAsyncError(): void | ||
{ | ||
self::assertTrue(true); | ||
} | ||
|
||
public function testDelay(): void | ||
{ | ||
self::assertTrue(true); | ||
} | ||
|
||
/** | ||
* @return DriverInterface<T1,T2> | ||
*/ | ||
protected function createDriver(): DriverInterface | ||
{ | ||
return new SpatieDriver(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ RUN set -eux; \ | |
opcache \ | ||
zip \ | ||
openswoole \ | ||
pcntl \ | ||
; | ||
|
||
WORKDIR /flow | ||
|
Oops, something went wrong.