Skip to content

Commit 11d1063

Browse files
committed
feat: add new class ProcCmd for quick exec command by proc_open
1 parent 4b8a58d commit 11d1063

File tree

8 files changed

+262
-110
lines changed

8 files changed

+262
-110
lines changed

example/proc0.php renamed to example/proc-fopen.php

+4-8
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,11 @@
77
* @license MIT
88
*/
99

10-
use Toolkit\Sys\Proc\ProcWrap;
10+
use Toolkit\Sys\Exec;
1111

1212
require dirname(__DIR__) . '/test/bootstrap.php';
1313

14-
// run: php example/proc0.php
15-
$proc = ProcWrap::new('ls -al')->run();
14+
// run: php example/proc-fopen.php
15+
$result = Exec::pexec('ls -al');
1616

17-
vdump($proc->getStatus());
18-
19-
$proc->closeAll();
20-
21-
// vdump($proc->getStatus()); // will ex
17+
vdump($result);

example/proc-open.php

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php declare(strict_types=1);
2+
/**
3+
* This file is part of toolkit/sys-utils.
4+
*
5+
* @author https://github.com/inhere
6+
* @link https://github.com/php-toolkit/sys-utils
7+
* @license MIT
8+
*/
9+
10+
use Toolkit\Sys\Proc\ProcCmd;
11+
12+
require dirname(__DIR__) . '/test/bootstrap.php';
13+
14+
// run: php example/proc-open.php
15+
$proc = ProcCmd::new('ls -al')->run();
16+
17+
vdump($proc->getResult());
18+
19+
// $proc = ProcWrap::new('ls -al')->run();
20+
// vdump($proc->getStatus());
21+
// $proc->closeAll();
22+
23+
// vdump($proc->getStatus()); // will ex

src/Cmd/AbstractCmdBuilder.php

+15-15
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ abstract class AbstractCmdBuilder
2525
/**
2626
* @var string
2727
*/
28-
protected string $command = '';
28+
protected string $cmdline = '';
2929

3030
/**
3131
* @var string
@@ -79,7 +79,7 @@ abstract class AbstractCmdBuilder
7979
*/
8080
public function __construct(string $command = '', string $workDir = '')
8181
{
82-
$this->command = $command;
82+
$this->cmdline = $command;
8383
$this->workDir = $workDir;
8484
}
8585

@@ -88,7 +88,7 @@ public function __construct(string $command = '', string $workDir = '')
8888
*
8989
* @return $this
9090
*/
91-
public function chDir(string $workDir): self
91+
public function chDir(string $workDir): static
9292
{
9393
return $this->changeDir($workDir);
9494
}
@@ -98,7 +98,7 @@ public function chDir(string $workDir): self
9898
*
9999
* @return $this
100100
*/
101-
public function changeDir(string $workDir): self
101+
public function changeDir(string $workDir): static
102102
{
103103
$this->workDir = $workDir;
104104
return $this;
@@ -119,7 +119,7 @@ public function runAndPrint(): void
119119
*
120120
* @return $this
121121
*/
122-
abstract public function run(bool $printOutput = false): self;
122+
abstract public function run(bool $printOutput = false): static;
123123

124124
/**************************************************************************
125125
* helper methods
@@ -206,22 +206,22 @@ protected function printMessage(string $msg, string $scene): void
206206
*************************************************************************/
207207

208208
/**
209-
* @param string $command
209+
* @param string $cmdline
210210
*
211211
* @return $this
212212
*/
213-
public function setCommand(string $command): self
213+
public function setCmdline(string $cmdline): static
214214
{
215-
$this->command = $command;
215+
$this->cmdline = $cmdline;
216216
return $this;
217217
}
218218

219219
/**
220220
* @return string
221221
*/
222-
public function getCommand(): string
222+
public function getCmdline(): string
223223
{
224-
return $this->command;
224+
return $this->cmdline;
225225
}
226226

227227
/**
@@ -237,7 +237,7 @@ public function getWorkDir(): string
237237
*
238238
* @return $this
239239
*/
240-
public function setWorkDir(string $workDir): self
240+
public function setWorkDir(string $workDir): static
241241
{
242242
$this->workDir = $workDir;
243243
return $this;
@@ -293,7 +293,7 @@ public function getResult(): array
293293
*
294294
* @return $this
295295
*/
296-
public function setPrintCmd(bool $printCmd): self
296+
public function setPrintCmd(bool $printCmd): static
297297
{
298298
$this->printCmd = $printCmd;
299299
return $this;
@@ -304,7 +304,7 @@ public function setPrintCmd(bool $printCmd): self
304304
*
305305
* @return $this
306306
*/
307-
public function setPrintOutput(bool $printOutput): self
307+
public function setPrintOutput(bool $printOutput): static
308308
{
309309
$this->printOutput = $printOutput;
310310
return $this;
@@ -323,7 +323,7 @@ public function isIgnoreError(): bool
323323
*
324324
* @return $this
325325
*/
326-
public function setIgnoreError(bool $ignoreError): self
326+
public function setIgnoreError(bool $ignoreError): static
327327
{
328328
$this->ignoreError = $ignoreError;
329329
return $this;
@@ -334,7 +334,7 @@ public function setIgnoreError(bool $ignoreError): self
334334
*
335335
* @return $this
336336
*/
337-
public function setDryRun(bool $dryRun): self
337+
public function setDryRun(bool $dryRun): static
338338
{
339339
$this->dryRun = $dryRun;
340340
return $this;

src/Cmd/CmdBuilder.php

+13-9
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function __construct(string $bin = '', string $workDir = '')
7373
*
7474
* @return $this
7575
*/
76-
public function add(int|string $arg): self
76+
public function add(int|string $arg): static
7777
{
7878
$this->args[] = $arg;
7979
return $this;
@@ -85,7 +85,7 @@ public function add(int|string $arg): self
8585
*
8686
* @return $this
8787
*/
88-
public function addf(string $format, ...$a): self
88+
public function addf(string $format, ...$a): static
8989
{
9090
$this->args[] = sprintf($format, ...$a);
9191
return $this;
@@ -97,7 +97,7 @@ public function addf(string $format, ...$a): self
9797
*
9898
* @return $this
9999
*/
100-
public function addIf(int|string $arg, bool|int|string $ifExpr): self
100+
public function addIf(int|string $arg, bool|int|string $ifExpr): static
101101
{
102102
if ($ifExpr) {
103103
$this->args[] = $arg;
@@ -111,7 +111,7 @@ public function addIf(int|string $arg, bool|int|string $ifExpr): self
111111
*
112112
* @return $this
113113
*/
114-
public function addArg(int|string $arg): self
114+
public function addArg(int|string $arg): static
115115
{
116116
$this->args[] = $arg;
117117
return $this;
@@ -122,7 +122,7 @@ public function addArg(int|string $arg): self
122122
*
123123
* @return $this
124124
*/
125-
public function addArgs(...$args): self
125+
public function addArgs(...$args): static
126126
{
127127
if ($args) {
128128
$this->args = array_merge($this->args, $args);
@@ -134,9 +134,9 @@ public function addArgs(...$args): self
134134
/**
135135
* @param bool $printOutput
136136
*
137-
* @return AbstractCmdBuilder
137+
* @return static
138138
*/
139-
public function run(bool $printOutput = false): AbstractCmdBuilder
139+
public function run(bool $printOutput = false): static
140140
{
141141
$this->printOutput = $printOutput;
142142

@@ -151,6 +151,10 @@ public function run(bool $printOutput = false): AbstractCmdBuilder
151151
*/
152152
protected function buildCommandLine(): string
153153
{
154+
if ($this->cmdline) {
155+
return $this->cmdline;
156+
}
157+
154158
$argList = [];
155159
foreach ($this->args as $arg) {
156160
$argList[] = Str::shellQuote((string)$arg);
@@ -165,7 +169,7 @@ protected function buildCommandLine(): string
165169
*
166170
* @return CmdBuilder
167171
*/
168-
public function setBin(string $bin): self
172+
public function setBin(string $bin): static
169173
{
170174
$this->bin = $bin;
171175
return $this;
@@ -176,7 +180,7 @@ public function setBin(string $bin): self
176180
*
177181
* @return CmdBuilder
178182
*/
179-
public function setArgs(array $args): self
183+
public function setArgs(array $args): static
180184
{
181185
$this->args = $args;
182186
return $this;

src/Exec.php

+8-6
Original file line numberDiff line numberDiff line change
@@ -196,19 +196,21 @@ public static function getOutput(string $command, string $cwd = ''): string
196196

197197
/**
198198
* Method to execute a command in the sys
199-
* Uses :
199+
*
200+
* Will try uses :
200201
* 1. system
201-
* X. passthru - will report error on windows
202+
* 2. passthru - will report error on windows
202203
* 3. exec
203204
* 4. shell_exec
205+
* 5. popen
204206
*
205207
* @param string $command
206-
* @param bool $returnStatus
208+
* @param bool $getStatus
207209
* @param string $cwd
208210
*
209-
* @return array|string
211+
* @return string|array{status:int, output:string}
210212
*/
211-
public static function auto(string $command, bool $returnStatus = true, string $cwd = ''): array|string
213+
public static function auto(string $command, bool $getStatus = true, string $cwd = ''): array|string
212214
{
213215
$status = 1;
214216
$curDir = '';
@@ -249,7 +251,7 @@ public static function auto(string $command, bool $returnStatus = true, string $
249251
chdir($curDir);
250252
}
251253

252-
if ($returnStatus) {
254+
if ($getStatus) {
253255
return [
254256
'output' => trim($output),
255257
'status' => $status

0 commit comments

Comments
 (0)