8
8
use Composer \Util \Platform ;
9
9
use Php \Pie \Platform \Architecture ;
10
10
use Php \Pie \Platform \OperatingSystem ;
11
+ use Php \Pie \Util \Process ;
11
12
use RuntimeException ;
12
13
use Symfony \Component \Process \PhpExecutableFinder ;
13
- use Symfony \Component \Process \Process ;
14
14
use Webmozart \Assert \Assert ;
15
15
16
16
use function array_combine ;
@@ -58,9 +58,7 @@ private static function assertValidLookingPhpBinary(string $phpBinaryPath): void
58
58
59
59
// This is somewhat of a rudimentary check that the target PHP really is a PHP instance; not sure why you
60
60
// WOULDN'T want to use a real PHP, but this should stop obvious hiccups at least (rather than for security)
61
- $ testOutput = trim ((new Process ([$ phpBinaryPath , '-r ' , 'echo "PHP"; ' ]))
62
- ->mustRun ()
63
- ->getOutput ());
61
+ $ testOutput = Process::run ([$ phpBinaryPath , '-r ' , 'echo "PHP"; ' ]);
64
62
65
63
if ($ testOutput !== 'PHP ' ) {
66
64
throw Exception \InvalidPhpBinaryPath::fromInvalidPhpBinary ($ phpBinaryPath );
@@ -118,7 +116,7 @@ public function extensionPath(): string
118
116
*/
119
117
public function extensions (): array
120
118
{
121
- $ extVersionsRawJson = trim (( new Process ([
119
+ $ extVersionsList = Process:: run ([
122
120
$ this ->phpBinaryPath ,
123
121
'-r ' ,
124
122
<<<'PHP'
@@ -141,13 +139,11 @@ static function ($k, $v) {
141
139
$extVersions
142
140
));
143
141
PHP,
144
- ]))
145
- ->mustRun ()
146
- ->getOutput ());
142
+ ]);
147
143
148
144
$ pairs = array_map (
149
145
static fn (string $ row ) => explode (': ' , $ row ),
150
- explode ("\n" , $ extVersionsRawJson ),
146
+ explode ("\n" , $ extVersionsList ),
151
147
);
152
148
153
149
return array_combine (
@@ -158,13 +154,11 @@ static function ($k, $v) {
158
154
159
155
public function operatingSystem (): OperatingSystem
160
156
{
161
- $ winOrNot = trim (( new Process ([
157
+ $ winOrNot = Process:: run ([
162
158
$ this ->phpBinaryPath ,
163
159
'-r ' ,
164
160
'echo \\defined( \'PHP_WINDOWS_VERSION_BUILD \') ? \'win \' : \'not \'; ' ,
165
- ]))
166
- ->mustRun ()
167
- ->getOutput ());
161
+ ]);
168
162
Assert::stringNotEmpty ($ winOrNot , 'Could not determine PHP version ' );
169
163
170
164
return $ winOrNot === 'win ' ? OperatingSystem::Windows : OperatingSystem::NonWindows;
@@ -173,13 +167,11 @@ public function operatingSystem(): OperatingSystem
173
167
/** @return non-empty-string */
174
168
public function version (): string
175
169
{
176
- $ phpVersion = trim (( new Process ([
170
+ $ phpVersion = Process:: run ([
177
171
$ this ->phpBinaryPath ,
178
172
'-r ' ,
179
173
'echo PHP_MAJOR_VERSION . "." . PHP_MINOR_VERSION . "." . PHP_RELEASE_VERSION; ' ,
180
- ]))
181
- ->mustRun ()
182
- ->getOutput ());
174
+ ]);
183
175
Assert::stringNotEmpty ($ phpVersion , 'Could not determine PHP version ' );
184
176
185
177
// normalizing the version will throw an exception if it is not a valid version
@@ -191,13 +183,11 @@ public function version(): string
191
183
/** @return non-empty-string */
192
184
public function majorMinorVersion (): string
193
185
{
194
- $ phpVersion = trim (( new Process ([
186
+ $ phpVersion = Process:: run ([
195
187
$ this ->phpBinaryPath ,
196
188
'-r ' ,
197
189
'echo PHP_MAJOR_VERSION . "." . PHP_MINOR_VERSION; ' ,
198
- ]))
199
- ->mustRun ()
200
- ->getOutput ());
190
+ ]);
201
191
Assert::stringNotEmpty ($ phpVersion , 'Could not determine PHP version ' );
202
192
203
193
// normalizing the version will throw an exception if it is not a valid version
@@ -208,27 +198,23 @@ public function majorMinorVersion(): string
208
198
209
199
public function machineType (): Architecture
210
200
{
211
- $ phpMachineType = trim (( new Process ([
201
+ $ phpMachineType = Process:: run ([
212
202
$ this ->phpBinaryPath ,
213
203
'-r ' ,
214
204
'echo php_uname("m"); ' ,
215
- ]))
216
- ->mustRun ()
217
- ->getOutput ());
205
+ ]);
218
206
Assert::stringNotEmpty ($ phpMachineType , 'Could not determine PHP machine type ' );
219
207
220
208
return Architecture::parseArchitecture ($ phpMachineType );
221
209
}
222
210
223
211
public function phpIntSize (): int
224
212
{
225
- $ phpIntSize = trim (( new Process ([
213
+ $ phpIntSize = Process:: run ([
226
214
$ this ->phpBinaryPath ,
227
215
'-r ' ,
228
216
'echo PHP_INT_SIZE; ' ,
229
- ]))
230
- ->mustRun ()
231
- ->getOutput ());
217
+ ]);
232
218
Assert::stringNotEmpty ($ phpIntSize , 'Could not fetch PHP_INT_SIZE ' );
233
219
Assert::same ($ phpIntSize , (string ) (int ) $ phpIntSize , 'PHP_INT_SIZE was not an integer processed %2$s from %s ' );
234
220
@@ -238,12 +224,10 @@ public function phpIntSize(): int
238
224
/** @return non-empty-string */
239
225
public function phpinfo (): string
240
226
{
241
- $ phpInfo = trim (( new Process ([
227
+ $ phpInfo = Process:: run ([
242
228
$ this ->phpBinaryPath ,
243
229
'-i ' ,
244
- ]))
245
- ->mustRun ()
246
- ->getOutput ());
230
+ ]);
247
231
248
232
Assert::stringNotEmpty ($ phpInfo , sprintf ('Could not run phpinfo using %s ' , $ this ->phpBinaryPath ));
249
233
@@ -264,9 +248,7 @@ public function phpConfigPath(): string|null
264
248
/** @param non-empty-string $phpConfig */
265
249
public static function fromPhpConfigExecutable (string $ phpConfig ): self
266
250
{
267
- $ phpExecutable = trim ((new Process ([$ phpConfig , '--php-binary ' ]))
268
- ->mustRun ()
269
- ->getOutput ());
251
+ $ phpExecutable = Process::run ([$ phpConfig , '--php-binary ' ]);
270
252
Assert::stringNotEmpty ($ phpExecutable , 'Could not find path to PHP executable. ' );
271
253
272
254
self ::assertValidLookingPhpBinary ($ phpExecutable );
0 commit comments