-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathPool.php
More file actions
118 lines (106 loc) · 2.95 KB
/
Pool.php
File metadata and controls
118 lines (106 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
namespace Illuminate\Process;
use InvalidArgumentException;
/**
* @mixin \Illuminate\Process\Factory
* @mixin \Illuminate\Process\PendingProcess
*/
class Pool
{
/**
* The process factory instance.
*
* @var \Illuminate\Process\Factory
*/
protected $factory;
/**
* The callback that resolves the pending processes.
*
* @var callable
*/
protected $callback;
/**
* The array of pending processes.
*
* @var array
*/
protected $pendingProcesses = [];
/**
* Create a new process pool.
*
* @param \Illuminate\Process\Factory $factory
* @param callable $callback
* @return void
*/
public function __construct(Factory $factory, callable $callback)
{
$this->factory = $factory;
$this->callback = $callback;
}
/**
* Add a process to the pool with a key.
*
* @param string $key
* @return \Illuminate\Process\PendingProcess
*/
public function as(string $key)
{
return tap($this->factory->newPendingProcess(), function ($pendingProcess) use ($key) {
$this->pendingProcesses[$key] = $pendingProcess;
});
}
/**
* Start all of the processes in the pool.
*
* @param callable|null $output
* @return \Illuminate\Process\InvokedProcessPool
*/
public function start(?callable $output = null)
{
call_user_func($this->callback, $this);
return new InvokedProcessPool(
collect($this->pendingProcesses)
->each(function ($pendingProcess) {
if (! $pendingProcess instanceof PendingProcess) {
throw new InvalidArgumentException('Process pool must only contain pending processes.');
}
})->mapWithKeys(function ($pendingProcess, $key) use ($output) {
return [$key => $pendingProcess->start(output: $output ? function ($type, $buffer) use ($key, $output) {
$output($type, $buffer, $key);
} : null)];
})
->all()
);
}
/**
* Start and wait for the processes to finish.
*
* @return \Illuminate\Process\ProcessPoolResults
*/
public function run()
{
return $this->wait();
}
/**
* Start and wait for the processes to finish.
*
* @return \Illuminate\Process\ProcessPoolResults
*/
public function wait()
{
return $this->start()->wait();
}
/**
* Dynamically proxy methods calls to a new pending process.
*
* @param string $method
* @param array $parameters
* @return \Illuminate\Process\PendingProcess
*/
public function __call($method, $parameters)
{
return tap($this->factory->{$method}(...$parameters), function ($pendingProcess) {
$this->pendingProcesses[] = $pendingProcess;
});
}
}