PluginProbe
WP-Stateless – Google Cloud Storage / 2.3.1
WP-Stateless – Google Cloud Storage v2.3.1
4.4.3 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.3.0 2.3.1 2.3.2 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 All 62 releases
wp-stateless / lib / cli / class-sm-cli-process.php

class-sm-cli-process.php in WP-Stateless – Google Cloud Storage 2.3.1, at lib/cli/class-sm-cli-process.php

104 lines 1.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 *
5 * based on WP_CLI\Process
6 *
7 */
8 class SM_CLI_Process {
9
10 private $command;
11 private $cwd;
12
13 private function __construct() {}
14
15 /**
16 * @param string $command Command to execute.
17 * @param string $cwd Directory to execute the command in.
18 */
19 public static function create( $command, $cwd = null ) {
20 $proc = new self;
21
22 $proc->command = $command;
23 $proc->cwd = $cwd;
24
25 return $proc;
26 }
27
28 /**
29 * Run the command.
30 *
31 * @return ProcessRun
32 */
33 public function run() {
34 $cwd = $this->cwd;
35
36 $descriptors = array(
37 0 => STDIN,
38 1 => array( 'pipe', 'w' ),
39 2 => array( 'pipe', 'w' ),
40 );
41
42 $proc = @proc_open( $this->command, $descriptors, $pipes, $cwd );
43
44 $stdout = stream_get_contents( $pipes[1] );
45 fclose( $pipes[1] );
46
47 $stderr = stream_get_contents( $pipes[2] );
48 fclose( $pipes[2] );
49
50 return new SM_CLI_ProcessRun( array(
51 'stdout' => $stdout,
52 'stderr' => $stderr,
53 'return_code' => proc_close( $proc ),
54 'command' => $this->command,
55 'cwd' => $cwd
56 ) );
57 }
58
59 /**
60 * Run the command, but throw an Exception on error.
61 *
62 * @return ProcessRun
63 */
64 public function run_check() {
65 $r = $this->run();
66
67 if ( $r->return_code || !empty( $r->STDERR ) ) {
68 throw new \RuntimeException( $r );
69 }
70
71 return $r;
72 }
73
74 }
75
76 /**
77 * Results of an executed command.
78 */
79 class SM_CLI_ProcessRun {
80
81 /**
82 * @var array $props Properties of executed command.
83 */
84 public function __construct( $props ) {
85 foreach ( $props as $key => $value ) {
86 $this->$key = $value;
87 }
88 }
89
90 /**
91 * Return properties of executed command as a string.
92 *
93 * @return string
94 */
95 public function __toString() {
96 $out = "$ $this->command\n";
97 $out .= "$this->stdout\n$this->stderr";
98 $out .= "cwd: $this->cwd\n";
99 $out .= "exit status: $this->return_code";
100
101 return $out;
102 }
103
104 }