1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Async\Runtime; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Opis\Closure\SerializableClosure; |
7
|
|
|
use function Opis\Closure\serialize; |
8
|
|
|
use function Opis\Closure\unserialize; |
9
|
|
|
use Spatie\Async\Pool; |
10
|
|
|
use Spatie\Async\Process\ParallelProcess; |
11
|
|
|
use Spatie\Async\Process\Runnable; |
12
|
|
|
use Spatie\Async\Process\SynchronousProcess; |
13
|
|
|
use Symfony\Component\Process\Process; |
14
|
|
|
|
15
|
|
|
class ParentRuntime |
16
|
|
|
{ |
17
|
|
|
/** @var bool */ |
18
|
|
|
protected static $isInitialised = false; |
19
|
|
|
|
20
|
|
|
/** @var string */ |
21
|
|
|
protected static $autoloader; |
22
|
|
|
|
23
|
|
|
/** @var string */ |
24
|
|
|
protected static $childProcessScript; |
25
|
|
|
|
26
|
|
|
protected static $currentId = 0; |
27
|
|
|
|
28
|
|
|
protected static $myPid = null; |
29
|
|
|
|
30
|
|
|
public static function init(string $autoloader = null) |
31
|
|
|
{ |
32
|
|
|
if (! $autoloader) { |
|
|
|
|
33
|
|
|
$existingAutoloaderFiles = array_filter([ |
34
|
|
|
__DIR__.'/../../../../autoload.php', |
35
|
|
|
__DIR__.'/../../../autoload.php', |
36
|
|
|
__DIR__.'/../../vendor/autoload.php', |
37
|
|
|
__DIR__.'/../../../vendor/autoload.php', |
38
|
|
|
], function (string $path) { |
39
|
|
|
return file_exists($path); |
40
|
|
|
}); |
41
|
|
|
|
42
|
|
|
$autoloader = reset($existingAutoloaderFiles); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
self::$autoloader = $autoloader; |
46
|
|
|
self::$childProcessScript = __DIR__.'/ChildRuntime.php'; |
47
|
|
|
|
48
|
|
|
self::$isInitialised = true; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param \Spatie\Async\Task|callable $task |
53
|
|
|
* @param int|null $outputLength |
54
|
|
|
* |
55
|
|
|
* @return \Spatie\Async\Process\Runnable |
56
|
|
|
*/ |
57
|
|
|
public static function createProcess($task, ?int $outputLength = null, ?string $binary = 'php'): Runnable |
58
|
|
|
{ |
59
|
|
|
if (! self::$isInitialised) { |
60
|
|
|
self::init(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if (! Pool::isSupported()) { |
64
|
|
|
return SynchronousProcess::create($task, self::getId()); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$process = new Process([ |
68
|
|
|
$binary, |
69
|
|
|
self::$childProcessScript, |
70
|
|
|
self::$autoloader, |
71
|
|
|
self::encodeTask($task), |
72
|
|
|
$outputLength, |
73
|
|
|
]); |
74
|
|
|
|
75
|
|
|
return ParallelProcess::create($process, self::getId()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param \Spatie\Async\Task|callable $task |
80
|
|
|
* |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
public static function encodeTask($task): string |
84
|
|
|
{ |
85
|
|
|
if ($task instanceof Closure) { |
86
|
|
|
$task = new SerializableClosure($task); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return base64_encode(serialize($task)); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public static function decodeTask(string $task) |
93
|
|
|
{ |
94
|
|
|
return unserialize(base64_decode($task)); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
protected static function getId(): string |
98
|
|
|
{ |
99
|
|
|
if (self::$myPid === null) { |
100
|
|
|
self::$myPid = getmypid(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
self::$currentId += 1; |
104
|
|
|
|
105
|
|
|
return (string) self::$currentId.(string) self::$myPid; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
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: