Repository navigation
Expand file tree
/
Copy pathSubprocessInstance.php
More file actions
195 lines (163 loc) · 5.49 KB
/
Copy pathSubprocessInstance.php
File metadata and controls
195 lines (163 loc) · 5.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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
declare(strict_types=1);
/*
* This file is part of the CleverAge/ProcessBundle package.
*
* Copyright (c) Clever-Age
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CleverAge\ProcessBundle\Model;
use CleverAge\ProcessBundle\Exception\RuntimeException;
use CleverAge\ProcessBundle\Filesystem\TemporaryFile;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Process\PhpExecutableFinder;
use Symfony\Component\Process\Process;
/**
* @final since 5.4
*/
class SubprocessInstance
{
final public const OPTION_JSON_BUFFERING = 'json-buffering';
protected Process $process;
/**
* JSON buffer, in the system temporary directory (the project directory may be read-only, e.g. in a container).
*/
protected string $bufferPath = '';
/**
* @var array<string, mixed>
*/
protected array $options;
protected string $consolePath;
/**
* @deprecated since 5.4, not used; only set when the kernel is passed to the constructor
*/
protected ?string $logDir = null;
/**
* @param string|KernelInterface $consolePath path of the Symfony console; passing the kernel is deprecated since 5.4
* @param array<string, mixed> $context
* @param array<string, mixed> $options
* @param string|null $environment environment of the sub-process (--env), the default one of the console
* if null; taken from the kernel when it is passed
*/
public function __construct(
KernelInterface|string $consolePath,
protected string $processCode,
protected ?string $input,
protected array $context = [],
array $options = [],
protected ?string $environment = null,
) {
$resolver = new OptionsResolver();
$this->configureOptions($resolver);
$this->options = $resolver->resolve($options);
if ($consolePath instanceof KernelInterface) {
trigger_deprecation(
'cleverage/process-bundle',
'5.4',
'Passing a "%s" to "%s::__construct()" is deprecated, pass the path of the console and the environment instead. The argument will only accept a string in v6.0.',
KernelInterface::class,
self::class,
);
$this->environment ??= $consolePath->getEnvironment();
$this->logDir = $consolePath->getLogDir().'/process';
$consolePath = $consolePath->getProjectDir().'/bin/console';
}
$this->consolePath = $consolePath;
}
/**
* Prepare the process before start.
*/
public function buildProcess(): static
{
$phpBinary = (new PhpExecutableFinder())->find();
if (false === $phpBinary) {
throw new RuntimeException('Unable to find the PHP executable');
}
$arguments = [
'nohup',
$phpBinary,
$this->consolePath,
...(null !== $this->environment ? ['--env='.$this->environment] : []),
'cleverage:process:execute',
'--input-from-stdin',
];
$fs = new Filesystem();
if (!$fs->exists($this->consolePath)) {
throw new RuntimeException("Unable to resolve path to symfony console '{$this->consolePath}'");
}
if ($this->options[self::OPTION_JSON_BUFFERING]) {
// Created before the sub-process writes into it: random name, exclusive creation, owner only
$this->bufferPath = TemporaryFile::create('cdm_buffer_', '.json-stream');
$arguments = [...$arguments, '--output='.$this->bufferPath, '--output-format=json-stream'];
}
foreach ($this->context as $key => $value) {
$arguments[] = \sprintf('--context=%s:%s', $key, $value);
}
$arguments[] = $this->processCode;
$this->process = new Process($arguments, null, null, $this->input);
$this->process->enableOutput();
return $this;
}
/**
* Start the process.
*/
public function start(): static
{
$this->process->start();
return $this;
}
/**
* Stop the process.
*/
public function stop(float $timeout = 10): static
{
$this->process->stop($timeout);
return $this;
}
public function getProcess(): Process
{
return $this->process;
}
public function getProcessCode(): string
{
return $this->processCode;
}
public function getInput(): ?string
{
return $this->input;
}
/**
* @return array<string, mixed>
*/
public function getOptions(): array
{
return $this->options;
}
/**
* @return array<string, mixed>
*/
public function getContext(): array
{
return $this->context;
}
public function getResult(): ?string
{
$fs = new Filesystem();
if ($this->process->isTerminated() && $fs->exists($this->bufferPath)) {
return $this->bufferPath;
}
return null;
}
/**
* Available options for process launcher.
*/
protected function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefault(self::OPTION_JSON_BUFFERING, false);
$resolver->setAllowedTypes(self::OPTION_JSON_BUFFERING, 'bool');
}
}