-
Notifications
You must be signed in to change notification settings - Fork 438
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
136 additions
and
292 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace GrumPHP\Runner\Parallel; | ||
|
||
use Amp\Cancellation; | ||
use Amp\Parallel\Worker\Task; | ||
use Amp\Sync\Channel; | ||
use Laravel\SerializableClosure\SerializableClosure; | ||
|
||
/** | ||
* @template T | ||
*/ | ||
class SerializedClosureTask implements Task | ||
{ | ||
/** | ||
* @param (\Closure(): T) $closure | ||
*/ | ||
public function __construct( | ||
private string $serializedClosure | ||
) { | ||
} | ||
|
||
/** | ||
* @template O | ||
* @param \Closure(): O $closure | ||
* @return self<O> | ||
*/ | ||
public static function fromClosure(\Closure $closure): self | ||
{ | ||
return new self(serialize(SerializableClosure::unsigned($closure))); | ||
} | ||
|
||
/** | ||
* @return T | ||
*/ | ||
public function run(Channel $channel, Cancellation $cancellation): mixed | ||
{ | ||
$callable = \unserialize($this->serializedClosure, ['allowed_classes' => true]); | ||
|
||
if ($callable instanceof \__PHP_Incomplete_Class) { | ||
throw new \Error('When using a class instance as a callable, the class must be autoloadable'); | ||
} | ||
|
||
if (!$callable instanceof \Closure) { | ||
throw new \Error('This task can only deal with serialized Closures. You passed '.get_debug_type($callable)); | ||
} | ||
|
||
return $callable(); | ||
} | ||
} |
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
Oops, something went wrong.