2015-08-18 00:00:26 +00:00
< ? php
/*
* This file is part of the Symfony package .
*
* ( c ) Fabien Potencier < fabien @ symfony . com >
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
namespace Symfony\Component\Process ;
use Symfony\Component\Process\Exception\InvalidArgumentException ;
/**
* ProcessUtils is a bunch of utility methods .
*
* This class contains static methods only and is not meant to be instantiated .
*
* @ author Martin Hasoň < martin . hason @ gmail . com >
*/
class ProcessUtils
{
/**
* This class should not be instantiated .
*/
private function __construct ()
{
}
/**
* Escapes a string to be used as a shell argument .
*
* @ param string $argument The argument that will be escaped
*
* @ return string The escaped argument
2018-11-23 12:29:20 +00:00
*
* @ deprecated since version 3.3 , to be removed in 4.0 . Use a command line array or give env vars to the `Process::start/run()` method instead .
2015-08-18 00:00:26 +00:00
*/
public static function escapeArgument ( $argument )
{
2018-11-23 12:29:20 +00:00
@ trigger_error ( 'The ' . __METHOD__ . '() method is deprecated since Symfony 3.3 and will be removed in 4.0. Use a command line array or give env vars to the Process::start/run() method instead.' , E_USER_DEPRECATED );
2015-08-18 00:00:26 +00:00
//Fix for PHP bug #43784 escapeshellarg removes % from given string
//Fix for PHP bug #49446 escapeshellarg doesn't work on Windows
//@see https://bugs.php.net/bug.php?id=43784
//@see https://bugs.php.net/bug.php?id=49446
2018-11-23 12:29:20 +00:00
if ( '\\' === \DIRECTORY_SEPARATOR ) {
2015-08-18 00:00:26 +00:00
if ( '' === $argument ) {
return escapeshellarg ( $argument );
}
$escapedArgument = '' ;
$quote = false ;
2015-08-27 19:03:05 +00:00
foreach ( preg_split ( '/(")/' , $argument , - 1 , PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE ) as $part ) {
2015-08-18 00:00:26 +00:00
if ( '"' === $part ) {
$escapedArgument .= '\\"' ;
} elseif ( self :: isSurroundedBy ( $part , '%' )) {
// Avoid environment variable expansion
$escapedArgument .= '^%"' . substr ( $part , 1 , - 1 ) . '"^%' ;
} else {
// escape trailing backslash
if ( '\\' === substr ( $part , - 1 )) {
$part .= '\\' ;
}
$quote = true ;
$escapedArgument .= $part ;
}
}
if ( $quote ) {
$escapedArgument = '"' . $escapedArgument . '"' ;
}
return $escapedArgument ;
}
2017-04-13 14:53:35 +00:00
return " ' " . str_replace ( " ' " , " ' \\ '' " , $argument ) . " ' " ;
2015-08-18 00:00:26 +00:00
}
/**
* Validates and normalizes a Process input .
*
* @ param string $caller The name of method call that validates the input
* @ param mixed $input The input to validate
*
2017-02-03 00:28:38 +00:00
* @ return mixed The validated input
2015-08-18 00:00:26 +00:00
*
* @ throws InvalidArgumentException In case the input is not valid
*/
public static function validateInput ( $caller , $input )
{
if ( null !== $input ) {
2018-11-23 12:29:20 +00:00
if ( \is_resource ( $input )) {
2015-08-18 00:00:26 +00:00
return $input ;
}
2018-11-23 12:29:20 +00:00
if ( \is_string ( $input )) {
2017-02-03 00:28:38 +00:00
return $input ;
}
2015-08-18 00:00:26 +00:00
if ( is_scalar ( $input )) {
return ( string ) $input ;
}
2018-11-23 12:29:20 +00:00
if ( $input instanceof Process ) {
return $input -> getIterator ( $input :: ITER_SKIP_ERR );
}
if ( $input instanceof \Iterator ) {
return $input ;
}
if ( $input instanceof \Traversable ) {
return new \IteratorIterator ( $input );
2015-08-18 00:00:26 +00:00
}
2018-11-23 12:29:20 +00:00
throw new InvalidArgumentException ( sprintf ( '%s only accepts strings, Traversable objects or stream resources.' , $caller ));
2015-08-18 00:00:26 +00:00
}
return $input ;
}
private static function isSurroundedBy ( $arg , $char )
{
2018-11-23 12:29:20 +00:00
return 2 < \strlen ( $arg ) && $char === $arg [ 0 ] && $char === $arg [ \strlen ( $arg ) - 1 ];
2015-08-18 00:00:26 +00:00
}
}