Page Menu
Home
In-Portal Phabricator
Search
Configure Global Search
Log In
Files
F1177293
ProcessBuilder.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Wed, Oct 8, 7:12 AM
Size
5 KB
Mime Type
text/x-php
Expires
Fri, Oct 10, 7:12 AM (1 d, 13 h)
Engine
blob
Format
Raw Data
Handle
763933
Attached To
rINP In-Portal
ProcessBuilder.php
View Options
<?php
/**
* @version $Id: ProcessBuilder.php 16285 2015-10-04 19:31:25Z alex $
* @package In-Portal
* @copyright Copyright (C) 1997 - 2015 Intechnic. All rights reserved.
* @license GNU/GPL
* In-Portal is Open Source software.
* This means that this software may have been modified pursuant
* the GNU General Public License, and as distributed it includes
* or is derivative of works licensed under the GNU General Public License
* or other free or open source software licenses.
* See http://www.in-portal.org/license for copyright notices and details.
*/
use
Symfony\Component\Process\Process
;
use
Symfony\Component\Process\ProcessUtils
;
defined
(
'FULL_PATH'
)
or
die
(
'restricted access!'
);
class
ProcessBuilder
extends
kBase
{
/**
* Command to run.
*
* @var string
*/
protected
$command
;
/**
* Executable to run.
*
* @var string
*/
protected
$executable
;
/**
* The "executable" arguments.
*
* @var array
*/
protected
$arguments
;
/**
* The working directory.
*
* @var string|null
*/
protected
$workingDirectory
;
/**
* The environment variables.
*
* @var array
*/
protected
$env
;
/**
* The input.
*
* @var mixed
*/
protected
$input
;
/**
* The timeout in seconds or null to disable.
*
* @var integer|null
*/
protected
$timeout
;
/**
* An array of options for proc_open.
*
* @var array
*/
protected
$options
;
/**
* Creates class instance.
*/
public
function
__construct
()
{
parent
::
__construct
();
$this
->
reset
();
}
/**
* Set's prefix and special.
*
* @param string $prefix Prefix.
* @param string $special Special.
*
* @return void
* @throws Exception Every time, When called.
*/
public
function
Init
(
$prefix
,
$special
)
{
$error_msg
=
sprintf
(
'Please use "%s" method instead of "%s" to create "%s" object.'
,
'<strong>makeClass</strong>'
,
'<strong>recallObject</strong>'
,
'<strong>'
.
__CLASS__
.
'</strong>'
);
throw
new
Exception
(
$error_msg
);
}
/**
* Resets state to allow building another process.
*
* @return static
*/
public
function
reset
()
{
$this
->
command
=
null
;
$this
->
executable
=
null
;
$this
->
arguments
=
array
();
$this
->
workingDirectory
=
null
;
$this
->
env
=
array
();
$this
->
input
=
null
;
$this
->
timeout
=
60
;
$this
->
options
=
array
();
return
$this
;
}
/**
* Sets command.
*
* @param string $command Command.
*
* @return static
*/
public
function
setCommand
(
$command
)
{
$this
->
command
=
$command
;
return
$this
;
}
/**
* Sets executable.
*
* @param string $executable Executable.
*
* @return static
*/
public
function
setExecutable
(
$executable
)
{
$this
->
executable
=
$executable
;
return
$this
;
}
/**
* Adds an argument.
*
* @param mixed $argument Argument.
*
* @return static
*/
public
function
add
(
$argument
)
{
$this
->
arguments
[]
=
$argument
;
return
$this
;
}
/**
* Sets the working directory.
*
* @param string|null $cwd The working directory or null to use the working dir of the current PHP process.
*
* @return static
*/
public
function
setWorkingDirectory
(
$cwd
)
{
$this
->
workingDirectory
=
$cwd
;
return
$this
;
}
/**
* Sets an environment variable.
* Setting a variable overrides its previous value. Use `null` to unset a
* defined environment variable.
*
* @param string $name The variable name.
* @param null|string $value The variable value.
*
* @return static
*/
public
function
setEnv
(
$name
,
$value
)
{
$this
->
env
[
$name
]
=
$value
;
return
$this
;
}
/**
* Sets the input of the process.
*
* @param mixed $input The input as a string.
*
* @return static
*/
public
function
setInput
(
$input
)
{
$this
->
input
=
ProcessUtils
::
validateInput
(
sprintf
(
'%s::%s'
,
__CLASS__
,
__FUNCTION__
),
$input
);
return
$this
;
}
/**
* Adds a proc_open option.
*
* @param string $name The option name.
* @param string $value The option value.
*
* @return static
*/
public
function
setOption
(
$name
,
$value
)
{
$this
->
options
[
$name
]
=
$value
;
return
$this
;
}
/**
* Sets the process timeout.
*
* @param float|null $timeout Timeout or null to disable.
*
* @return static
* @throws InvalidArgumentException When negative timeout is given.
*/
public
function
setTimeout
(
$timeout
)
{
if
(
$timeout
===
null
)
{
$this
->
timeout
=
null
;
return
$this
;
}
$timeout
=
(
float
)
$timeout
;
if
(
$timeout
<
0
)
{
throw
new
InvalidArgumentException
(
'The timeout value must be a valid positive integer or float number.'
);
}
$this
->
timeout
=
$timeout
;
return
$this
;
}
/**
* Creates process.
*
* @return Process
* @throws LogicException When incompatible builder parameters are set.
*/
public
function
build
()
{
if
(
isset
(
$this
->
executable
)
&&
isset
(
$this
->
command
)
)
{
throw
new
LogicException
(
'Use setCommand() or setExecutable(), not both.'
);
}
if
(
isset
(
$this
->
command
)
&&
$this
->
arguments
)
{
throw
new
LogicException
(
'The add() can only be used with setExecutable().'
);
}
return
new
Process
(
$this
->
getCommandLine
(),
$this
->
workingDirectory
,
array_replace
(
$_ENV
,
$_SERVER
,
$this
->
env
),
$this
->
input
,
$this
->
timeout
,
$this
->
options
);
}
/**
* Builds commandline.
*
* @return string
* @throws LogicException When none of "command" or "executable" isn't set.
*/
protected
function
getCommandLine
()
{
if
(
isset
(
$this
->
command
)
)
{
return
$this
->
command
;
}
if
(
isset
(
$this
->
executable
)
)
{
$arguments
=
array_merge
(
array
(
$this
->
executable
),
$this
->
arguments
);
$escaped_arguments
=
array_map
(
array
(
'Symfony
\\
Component
\\
Process
\\
ProcessUtils'
,
'escapeArgument'
),
$arguments
);
return
implode
(
' '
,
$escaped_arguments
);
}
throw
new
LogicException
(
'You must use setCommand() or setExecutable() before getting the process.'
);
}
}
Event Timeline
Log In to Comment