1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Async\Runtime; |
4
|
|
|
|
5
|
|
|
use Spatie\Async\Task; |
6
|
|
|
use Spatie\Async\ParallelProcess; |
7
|
|
|
use function Opis\Closure\serialize; |
8
|
|
|
use Opis\Closure\SerializableClosure; |
9
|
|
|
use function Opis\Closure\unserialize; |
10
|
|
|
use Symfony\Component\Process\Process; |
11
|
|
|
|
12
|
|
|
class ParentRuntime |
13
|
|
|
{ |
14
|
|
|
/** @var bool */ |
15
|
|
|
protected static $isInitialised = false; |
16
|
|
|
|
17
|
|
|
/** @var string */ |
18
|
|
|
protected static $autoloader; |
19
|
|
|
|
20
|
|
|
/** @var string */ |
21
|
|
|
protected static $childProcessScript; |
22
|
|
|
|
23
|
|
|
protected static $currentId = 0; |
24
|
|
|
|
25
|
|
|
protected static $myPid = null; |
26
|
|
|
|
27
|
|
|
public static function init(string $autoloader = null) |
28
|
|
|
{ |
29
|
|
|
if (! $autoloader) { |
|
|
|
|
30
|
|
|
$existingAutoloaderFiles = array_filter([ |
31
|
|
|
__DIR__.'/../../../../autoload.php', |
32
|
|
|
__DIR__.'/../../../autoload.php', |
33
|
|
|
__DIR__.'/../../vendor/autoload.php', |
34
|
|
|
__DIR__.'/../../../vendor/autoload.php', |
35
|
|
|
], function (string $path) { |
36
|
|
|
return file_exists($path); |
37
|
|
|
}); |
38
|
|
|
|
39
|
|
|
$autoloader = reset($existingAutoloaderFiles); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
self::$autoloader = $autoloader; |
43
|
|
|
self::$childProcessScript = __DIR__.'/ChildRuntime.php'; |
44
|
|
|
|
45
|
|
|
self::$isInitialised = true; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param \Spatie\Async\Task|callable $task |
50
|
|
|
* |
51
|
|
|
* @return \Spatie\Async\ParallelProcess |
52
|
|
|
*/ |
53
|
|
|
public static function createChildProcess($task): ParallelProcess |
54
|
|
|
{ |
55
|
|
|
if (! self::$isInitialised) { |
56
|
|
|
self::init(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$process = new Process(implode(' ', [ |
60
|
|
|
'exec php', |
61
|
|
|
self::$childProcessScript, |
62
|
|
|
self::$autoloader, |
63
|
|
|
self::encodeTask($task), |
64
|
|
|
])); |
65
|
|
|
|
66
|
|
|
return ParallelProcess::create($process, self::getId()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param \Spatie\Async\Task|callable $task |
71
|
|
|
* |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
public static function encodeTask($task): string |
75
|
|
|
{ |
76
|
|
|
if (! $task instanceof Task) { |
77
|
|
|
$task = new SerializableClosure($task); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return base64_encode(serialize($task)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public static function decodeTask(string $task) |
84
|
|
|
{ |
85
|
|
|
return unserialize(base64_decode($task)); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
protected static function getId(): string |
89
|
|
|
{ |
90
|
|
|
if (self::$myPid === null) { |
91
|
|
|
self::$myPid = getmypid(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
self::$currentId += 1; |
95
|
|
|
|
96
|
|
|
return (string) self::$currentId . (string) self::$myPid; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: