|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Vectorial1024\LaravelProcessAsync; |
| 4 | + |
| 5 | +use Closure; |
| 6 | +use Illuminate\Support\Facades\Process; |
| 7 | +use Laravel\SerializableClosure\SerializableClosure; |
| 8 | + |
| 9 | +/** |
| 10 | + * The common handler of an AsyncTask; this can be a closure (will be wrapped inside AsyncTask) or an interface instance. |
| 11 | + */ |
| 12 | +class AsyncTask |
| 13 | +{ |
| 14 | + /** |
| 15 | + * The back to be executed in the background. |
| 16 | + * @var SerializableClosure|AsyncTaskInterface |
| 17 | + */ |
| 18 | + private SerializableClosure|AsyncTaskInterface $theTask; |
| 19 | + |
| 20 | + /** |
| 21 | + * Creates an AsyncTask instance. |
| 22 | + * @param Closure|AsyncTaskInterface $theTask The task to be executed in the background. |
| 23 | + */ |
| 24 | + public function __construct(Closure|AsyncTaskInterface $theTask) |
| 25 | + { |
| 26 | + if ($theTask instanceof Closure) { |
| 27 | + // convert to serializable closure first |
| 28 | + $theTask = new SerializableClosure($theTask); |
| 29 | + } |
| 30 | + $this->theTask = $theTask; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Inside an available PHP process, runs this AsyncTask instance. |
| 35 | + * |
| 36 | + * This should only be called from the runner so that we are really inside an available PHP process. |
| 37 | + * @return void |
| 38 | + * @see AsyncTaskRunnerCommand |
| 39 | + */ |
| 40 | + public function run(): void |
| 41 | + { |
| 42 | + // todo startup configs |
| 43 | + |
| 44 | + // then, execute the task itself |
| 45 | + if ($this->theTask instanceof SerializableClosure) { |
| 46 | + $innerClosure = $this->theTask->getClosure(); |
| 47 | + $innerClosure(); |
| 48 | + unset($innerClosure); |
| 49 | + } else { |
| 50 | + // must be AsyncTaskInterface |
| 51 | + $this->theTask->execute(); |
| 52 | + } |
| 53 | + |
| 54 | + // todo what if want some "task complete" event handling? |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Starts this AsyncTask immediately in the background. |
| 60 | + * @return void |
| 61 | + */ |
| 62 | + public function start(): void |
| 63 | + { |
| 64 | + // assume unix for now |
| 65 | + $serializedTask = $this->toBase64Serial(); |
| 66 | + Process::start("php artisan async:run $serializedTask 2>&1"); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Returns the base64-encoded serialization for this object. |
| 71 | + * |
| 72 | + * This has the benefit of entirely ignoring potential encoding problems, such as '\0' from private instance variables. |
| 73 | + * |
| 74 | + * This mechanism might have problems if the task closure is too long, but let's be honest: long closures are best converted to dedicated interface objects. |
| 75 | + * @return string The special serialization. |
| 76 | + * @see self::fromBase64Serial() |
| 77 | + */ |
| 78 | + public function toBase64Serial(): string |
| 79 | + { |
| 80 | + return base64_encode(serialize($this)); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Returns the AsyncTask instance represented by the given base64-encoded serial. |
| 85 | + * @param string $serial The special serialization. |
| 86 | + * @return static|null If the serial is valid, then the reconstructed instance will be returned, else returns null. |
| 87 | + * @see self::toBase64Serial() |
| 88 | + */ |
| 89 | + public static function fromBase64Serial(string $serial): ?static |
| 90 | + { |
| 91 | + $temp = base64_decode($serial, true); |
| 92 | + if ($temp === false) { |
| 93 | + // bad data |
| 94 | + return null; |
| 95 | + } |
| 96 | + try { |
| 97 | + $temp = unserialize($temp); |
| 98 | + // also check that we are unserializing into ourselves |
| 99 | + if ($temp instanceof static) { |
| 100 | + // correct value type |
| 101 | + return $temp; |
| 102 | + } |
| 103 | + // incorrect value type |
| 104 | + return null; |
| 105 | + } catch (ErrorException) { |
| 106 | + // bad data |
| 107 | + return null; |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments