Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F22401498
PhutilExecPassthru.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Authored By
Unknown
Size
2 KB
Referenced Files
None
Subscribers
None
PhutilExecPassthru.php
View Options
<?php
/**
* Execute a command which takes over stdin, stdout and stderr, similar to
* `passthru()`, but which preserves TTY semantics, escapes arguments, and is
* traceable.
*
* Passthru commands use the `STDIN`, `STDOUT` and `STDERR` of the parent
* process, so input can be read from the console and output is printed to it.
* This is primarily useful for executing things like `$EDITOR` from command
* line scripts.
*
* $exec = new PhutilExecPassthru('ls %s', $dir);
* $err = $exec->execute();
*
* You can set the current working directory for the command with
* @{method:setCWD}, and set the environment with @{method:setEnv}.
*
* @task command Executing Passthru Commands
*/
final
class
PhutilExecPassthru
extends
PhutilExecutableFuture
{
/* -( Executing Passthru Commands )---------------------------------------- */
/**
* Execute this command.
*
* @return int Error code returned by the subprocess.
*
* @task command
*/
public
function
execute
()
{
$command
=
$this
->
getCommand
();
$profiler
=
PhutilServiceProfiler
::
getInstance
();
$call_id
=
$profiler
->
beginServiceCall
(
array
(
'type'
=>
'exec'
,
'subtype'
=>
'passthru'
,
'command'
=>
$command
,
));
$spec
=
array
(
STDIN
,
STDOUT
,
STDERR
);
$pipes
=
array
();
$unmasked_command
=
$command
->
getUnmaskedString
();
if
(
$this
->
hasEnv
())
{
$env
=
$this
->
getEnv
();
}
else
{
$env
=
null
;
}
$cwd
=
$this
->
getCWD
();
$options
=
array
();
if
(
phutil_is_windows
())
{
// Without 'bypass_shell', things like launching vim don't work properly,
// and we can't execute commands with spaces in them, and all commands
// invoked from git bash fail horridly, and everything is a mess in
// general.
$options
[
'bypass_shell'
]
=
true
;
}
$trap
=
new
PhutilErrorTrap
();
$proc
=
@
proc_open
(
$unmasked_command
,
$spec
,
$pipes
,
$cwd
,
$env
,
$options
);
$errors
=
$trap
->
getErrorsAsString
();
$trap
->
destroy
();
if
(!
is_resource
(
$proc
))
{
throw
new
Exception
(
pht
(
'Failed to passthru %s: %s'
,
'proc_open()'
,
$errors
));
}
$err
=
proc_close
(
$proc
);
$profiler
->
endServiceCall
(
$call_id
,
array
(
'err'
=>
$err
,
));
return
$err
;
}
/* -( Future )------------------------------------------------------------- */
public
function
isReady
()
{
// This isn't really a future because it executes synchronously and has
// full control of the console. We're just implementing the interfaces to
// make it easier to share code with ExecFuture.
if
(!
$this
->
hasResult
())
{
$result
=
$this
->
execute
();
$this
->
setResult
(
$result
);
}
return
true
;
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Fri, Nov 27, 18:21 (11 h, 26 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1229360
Default Alt Text
PhutilExecPassthru.php (2 KB)
Attached To
Mode
R118 Arcanist - fork
Attached
Detach File
Event Timeline