![]() |
Bearsampp 2026.9.23
|
Static Public Member Functions | |
| static | background (string $command) |
| static | exec (string $executable, array $args=[], string &$stderr='') |
| static | execCombined (string $executable, array $args=[]) |
| static | execToFile (string $executable, array $args, string $stdoutFile, string $stderrFile) |
| static | shellExec (string $command) |
| static | stream (string $executable, array $args, callable $lineCallback) |
Static Private Member Functions | |
| static | writeLog (string $log) |
Class CommandRunner
Centralizes all PHP shell-execution primitives (proc_open, popen, shell_exec) so that every external command goes through a single, auditable point with consistent argument escaping and logging.
Three execution modes:
Rules:
Definition at line 27 of file class.commandrunner.php.
|
static |
Launch a command in the background without waiting for output.
Uses the Windows "start /B" cmd.exe idiom to detach the process immediately. The caller is responsible for ensuring $command is properly constructed; use escapeshellarg() on any dynamic values before passing them in.
| string | $command | Fully-formed command string to run in the background. |
Definition at line 220 of file class.commandrunner.php.
References $command, and writeLog().
Referenced by QuickPick\installModule().
|
static |
Execute an executable with arguments, capturing stdout and stderr.
Each argument is individually escaped with escapeshellarg(). The console window is hidden on Windows via bypass_shell + CREATE_NO_WINDOW semantics.
| string | $executable | Path to the executable (will be escapeshellarg'd). |
| array | $args | Arguments, each will be escapeshellarg'd. |
| string | $stderr | Populated with any stderr output on return. |
Definition at line 52 of file class.commandrunner.php.
References writeLog().
Referenced by execCombined().
|
static |
Execute an executable with arguments, combining stdout and stderr.
Convenience wrapper around exec() for callers that need both streams merged (e.g. sc.exe which may send status messages to either stream).
| string | $executable | Path to the executable (will be escapeshellarg'd). |
| array | $args | Arguments, each will be escapeshellarg'd. |
Definition at line 147 of file class.commandrunner.php.
References exec().
Referenced by Win32Service\fastServiceCheck().
|
static |
Execute an executable with arguments, redirecting stdout and stderr to files.
Capturing large output through a pipe with stream_get_contents() can be extremely slow on Windows, so this variant lets the child process write its output directly to the given files. The caller reads and cleans up the files.
| string | $executable | Path to the executable (will be escapeshellarg'd). |
| array | $args | Arguments, each will be escapeshellarg'd. |
| string | $stdoutFile | File path the child's stdout is redirected to. |
| string | $stderrFile | File path the child's stderr is redirected to. |
Definition at line 103 of file class.commandrunner.php.
References writeLog().
Referenced by Core\isSafeDestinationFileList().
|
static |
Execute a fully-formed shell command and return its output.
Only use this for commands that contain no dynamic user input (e.g. hardcoded system queries such as "net session" or "whoami /groups"). For commands with dynamic arguments, use exec() or stream() instead so that escaping is enforced at the boundary.
| string | $command | Fully-formed command string. Must not contain unescaped user input. |
Definition at line 238 of file class.commandrunner.php.
References $command, and writeLog().
Referenced by Util\isAdmin().
|
static |
Execute an executable with arguments, streaming output line-by-line.
Useful for long-running processes that emit progress information. Each argument is individually escaped with escapeshellarg(). Lines are split on carriage-return (\r) to match Windows progress-reporting conventions. Any data remaining in the buffer after EOF is flushed as a final line.
| string | $executable | Path to the executable (will be escapeshellarg'd). |
| array | $args | Arguments, each will be escapeshellarg'd. |
| callable | $lineCallback | Invoked with each trimmed output line as a string. |
Definition at line 177 of file class.commandrunner.php.
References writeLog().
Referenced by Core\unzipFile().
|
staticprivate |
Writes a log entry to the batch log file.
| string | $log | The message to log. |
Definition at line 34 of file class.commandrunner.php.
References $bearsamppRoot, Log\debug(), and Path\getBatchLogFilePath().
Referenced by background(), exec(), execToFile(), shellExec(), and stream().