Skip to content

Commit b52d232

Browse files
Merge pull request #13 from Vectorial1024/serial
Use opis/closure 4.0
2 parents c89946f + 9fb937d commit b52d232

File tree

4 files changed

+32
-14
lines changed

4 files changed

+32
-14
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ Note: you may refer to `README.md` for description of features.
33

44
## Dev (WIP)
55
- Task IDs can be given to tasks (generated or not) (https://github.com/Vectorial1024/laravel-process-async/issues/5)
6+
- Updated to use `opis/closure` 4.0 for task details serialization (https://github.com/Vectorial1024/laravel-process-async/issues/12)
7+
- Technically a breaking internal change, but no code change expected and downtime is expected to be almost negligible
68

79
## 0.2.0 (2025-01-04)
810
- Task runners are now detached from the task giver (https://github.com/Vectorial1024/laravel-process-async/issues/7)

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Utilize Laravel Processes to run PHP code asynchronously, as if using Laravel Co
1010
## What really is this?
1111
[Laravel Processes](https://laravel.com/docs/10.x/processes) was first introduced in Laravel 10. This library wraps around `Process::start()` to let you execute code in the background to achieve async, albeit with some caveats:
1212
- You may only execute PHP code
13-
- Restrictions from `laravel/serializable-closure` apply (see [their README](https://github.com/laravel/serializable-closure))
13+
- Restrictions from `opis/closure` apply (see [their README](https://github.com/opis/closure))
1414
- Hands-off execution: no built-in result-checking, check the results yourself (e.g. via database, file cache, etc)
1515

1616
This library internally uses an Artisan command to run the async code, which is similar to Laravel 11 [Concurrency](https://laravel.com/docs/11.x/concurrency).

composer.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
"require": {
3434
"php": "^8.1",
3535
"illuminate/support": "^10.0|^11.0",
36-
"laravel/serializable-closure": "^1.0",
37-
"loophp/phposinfo": "^1.8"
36+
"loophp/phposinfo": "^1.8",
37+
"opis/closure": "^4.0"
3838
},
3939
"require-dev": {
4040
"phpunit/phpunit": "^10",
@@ -50,5 +50,8 @@
5050
"Vectorial1024\\LaravelProcessAsync\\ProcessAsyncServiceProvider"
5151
]
5252
}
53+
},
54+
"config": {
55+
"sort-packages": true
5356
}
5457
}

src/AsyncTask.php

+24-11
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,22 @@
99
use Illuminate\Support\Facades\Process;
1010
use Illuminate\Support\Str;
1111
use InvalidArgumentException;
12-
use Laravel\SerializableClosure\SerializableClosure;
1312
use LogicException;
1413
use loophp\phposinfo\OsInfo;
1514
use RuntimeException;
1615

16+
use function Opis\Closure\{serialize, unserialize};
17+
1718
/**
1819
* The common handler of an AsyncTask; this can be a closure (will be wrapped inside AsyncTask) or an interface instance.
1920
*/
2021
class AsyncTask
2122
{
2223
/**
2324
* The task to be executed in the background.
24-
* @var SerializableClosure|AsyncTaskInterface
25+
* @var Closure|AsyncTaskInterface
2526
*/
26-
private SerializableClosure|AsyncTaskInterface $theTask;
27+
private Closure|AsyncTaskInterface $theTask;
2728

2829
/**
2930
* The user-specified ID of the current task. (Null means user did not specify any ID).
@@ -105,17 +106,31 @@ class AsyncTask
105106
*/
106107
public function __construct(Closure|AsyncTaskInterface $theTask, string|null $taskID = null)
107108
{
108-
if ($theTask instanceof Closure) {
109-
// convert to serializable closure first
110-
$theTask = new SerializableClosure($theTask);
111-
}
109+
// opis/closure allows direct storage of closure
112110
$this->theTask = $theTask;
113111
if ($taskID === "") {
114112
throw new InvalidArgumentException("AsyncTask ID cannot be empty.");
115113
}
116114
$this->taskID = $taskID;
117115
}
118116

117+
public function __serialize(): array
118+
{
119+
// serialize only the necessary info to reduce runner cmd length
120+
return [
121+
'theTask' => $this->theTask,
122+
'timeLimit' => $this->timeLimit,
123+
];
124+
}
125+
126+
public function __unserialize($data): void
127+
{
128+
[
129+
'theTask' => $this->theTask,
130+
'timeLimit' => $this->timeLimit,
131+
] = $data;
132+
}
133+
119134
/**
120135
* Inside an available PHP process, runs this AsyncTask instance.
121136
*
@@ -159,10 +174,8 @@ public function run(): void
159174
}
160175

161176
// then, execute the task itself
162-
if ($this->theTask instanceof SerializableClosure) {
163-
$innerClosure = $this->theTask->getClosure();
164-
$innerClosure();
165-
unset($innerClosure);
177+
if ($this->theTask instanceof Closure) {
178+
($this->theTask)();
166179
} else {
167180
// must be AsyncTaskInterface
168181
$this->theTask->execute();

0 commit comments

Comments
 (0)