-
-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathDefaultContextFactory.php
More file actions
55 lines (45 loc) · 1.49 KB
/
DefaultContextFactory.php
File metadata and controls
55 lines (45 loc) · 1.49 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
<?php declare(strict_types=1);
namespace Amp\Parallel\Context;
use Amp\ByteStream;
use Amp\Cancellation;
use Amp\ForbidCloning;
use Amp\ForbidSerialization;
use Amp\Parallel\Ipc\IpcHub;
use Amp\Parallel\Ipc\LocalIpcHub;
use function Amp\async;
final class DefaultContextFactory implements ContextFactory
{
use ForbidCloning;
use ForbidSerialization;
private readonly ContextFactory $contextFactory;
/**
* @param IpcHub $ipcHub Optional IpcHub instance.
*/
public function __construct(IpcHub $ipcHub = new LocalIpcHub())
{
if (ThreadContext::isSupported()) {
$this->contextFactory = new ThreadContextFactory(ipcHub: $ipcHub);
} else {
$this->contextFactory = new ProcessContextFactory(ipcHub: $ipcHub);
}
}
/**
* @param string|non-empty-list<string> $script
*
* @throws ContextException
*/
#[\Override]
public function start(string|array $script, ?Cancellation $cancellation = null): Context
{
$context = $this->contextFactory->start($script, $cancellation);
if ($context instanceof ProcessContext) {
$stdout = $context->getStdout();
$stdout->unreference();
$stderr = $context->getStderr();
$stderr->unreference();
async(ByteStream\pipe(...), $stdout, ByteStream\getStdout())->ignore();
async(ByteStream\pipe(...), $stderr, ByteStream\getStderr())->ignore();
}
return $context;
}
}