|
1
|
|
|
<?php // phpcs:ignore WordPress.Files.FileName |
|
2
|
|
|
/** |
|
3
|
|
|
* Utilities for the changelogger tool. |
|
4
|
|
|
* |
|
5
|
|
|
* @package automattic/jetpack-changelogger |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
// phpcs:disable WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid |
|
9
|
|
|
|
|
10
|
|
|
namespace Automattic\Jetpack\Changelogger; |
|
11
|
|
|
|
|
12
|
|
|
use Symfony\Component\Console\Helper\DebugFormatterHelper; |
|
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
14
|
|
|
use Symfony\Component\Process\Process; |
|
15
|
|
|
use function error_clear_last; // phpcs:ignore PHPCompatibility.FunctionUse.NewFunctions.error_clear_lastFound |
|
16
|
|
|
use function Wikimedia\quietCall; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Utilities for the changelogger tool. |
|
20
|
|
|
*/ |
|
21
|
|
|
class Utils { |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Calls `error_clear_last()` or emulates it. |
|
25
|
|
|
*/ |
|
26
|
|
|
public static function error_clear_last() { |
|
27
|
|
|
if ( is_callable( 'error_clear_last' ) ) { |
|
28
|
|
|
// phpcs:ignore PHPCompatibility.FunctionUse.NewFunctions.error_clear_lastFound |
|
29
|
|
|
error_clear_last(); |
|
30
|
|
|
} else { |
|
31
|
|
|
// @codeCoverageIgnoreStart |
|
32
|
|
|
quietCall( 'trigger_error', '', E_USER_NOTICE ); |
|
33
|
|
|
// @codeCoverageIgnoreEnd |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Helper to run a process. |
|
39
|
|
|
* |
|
40
|
|
|
* @param string[] $command Command to execute. |
|
41
|
|
|
* @param OutputInterface $output OutputInterface to write debug output to. |
|
42
|
|
|
* @param DebugFormatterHelper $formatter Formatter to use to format debug output. |
|
43
|
|
|
* @param array $options An associative array with the following optional keys. Defaults are null unless otherwise specified. |
|
44
|
|
|
* - cwd: (string|null) The working directory or null to use the working dir of the current PHP process. |
|
45
|
|
|
* - env: (array|null) The environment variables or null to use the same environment as the current PHP process. |
|
46
|
|
|
* - input: (mixed|null) The input as stream resource, scalar or \Traversable, or null for no input. |
|
47
|
|
|
* - timeout: (float|null) The timeout in seconds or null to disable. Default 60. |
|
48
|
|
|
* - mustRun: (boolean) If set true, an exception will be thrown if the command fails. Default false. |
|
49
|
|
|
* @return Process The process, which has already been run. |
|
50
|
|
|
*/ |
|
51
|
|
|
public static function runCommand( array $command, OutputInterface $output, DebugFormatterHelper $formatter, array $options = array() ) { |
|
52
|
|
|
$options += array( |
|
53
|
|
|
'cwd' => null, |
|
54
|
|
|
'env' => null, |
|
55
|
|
|
'input' => null, |
|
56
|
|
|
'timeout' => 60, |
|
57
|
|
|
'mustRun' => false, |
|
58
|
|
|
); |
|
59
|
|
|
|
|
60
|
|
|
$process = new Process( $command, $options['cwd'], $options['env'], $options['input'], $options['timeout'] ); |
|
61
|
|
|
$output->writeln( |
|
62
|
|
|
$formatter->start( spl_object_hash( $process ), $process->getCommandLine() ), |
|
63
|
|
|
OutputInterface::VERBOSITY_DEBUG |
|
64
|
|
|
); |
|
65
|
|
|
$func = $options['mustRun'] ? 'mustRun' : 'run'; |
|
66
|
|
|
$process->$func( |
|
67
|
|
|
function ( $type, $buffer ) use ( $output, $formatter, $process ) { |
|
68
|
|
|
$output->writeln( |
|
69
|
|
|
$formatter->progress( spl_object_hash( $process ), $buffer, Process::ERR === $type ), |
|
70
|
|
|
OutputInterface::VERBOSITY_DEBUG |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
); |
|
74
|
|
|
return $process; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
|