|
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
|
|
|
* @param string $binary |
|
56
|
|
|
* @return \Spatie\Async\Process\Runnable |
|
57
|
|
|
*/ |
|
58
|
|
|
public static function createProcess($task, ?int $outputLength = null, string $binary = Pool::DEFAULT_PHP_BINARY): Runnable |
|
59
|
|
|
{ |
|
60
|
|
|
if (! self::$isInitialised) { |
|
61
|
|
|
self::init(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if (! Pool::isSupported()) { |
|
65
|
|
|
return SynchronousProcess::create($task, self::getId()); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$process = self::createProcessExecutable($task, $outputLength, $binary); |
|
69
|
|
|
|
|
70
|
|
|
return ParallelProcess::create($task, $process, self::getId(), $outputLength); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public static function createProcessExecutable($task, ?int $outputLength = null, string $binary = Pool::DEFAULT_PHP_BINARY): Process |
|
74
|
|
|
{ |
|
75
|
|
|
return new Process([ |
|
76
|
|
|
$binary, |
|
77
|
|
|
self::$childProcessScript, |
|
78
|
|
|
self::$autoloader, |
|
79
|
|
|
self::encodeTask($task), |
|
80
|
|
|
$outputLength, |
|
81
|
|
|
]); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param \Spatie\Async\Task|callable $task |
|
86
|
|
|
* |
|
87
|
|
|
* @return string |
|
88
|
|
|
*/ |
|
89
|
|
|
public static function encodeTask($task): string |
|
90
|
|
|
{ |
|
91
|
|
|
if ($task instanceof Closure) { |
|
92
|
|
|
$task = new SerializableClosure($task); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return base64_encode(serialize($task)); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public static function decodeTask(string $task) |
|
99
|
|
|
{ |
|
100
|
|
|
return unserialize(base64_decode($task)); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
protected static function getId(): string |
|
104
|
|
|
{ |
|
105
|
|
|
if (self::$myPid === null) { |
|
106
|
|
|
self::$myPid = getmypid(); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
self::$currentId += 1; |
|
110
|
|
|
|
|
111
|
|
|
return (string) self::$currentId.(string) self::$myPid; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: