ProcessUtils.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Process;
  11. use Symfony\Component\Process\Exception\InvalidArgumentException;
  12. /**
  13. * ProcessUtils is a bunch of utility methods.
  14. *
  15. * This class contains static methods only and is not meant to be instantiated.
  16. *
  17. * @author Martin Hasoň <martin.hason@gmail.com>
  18. */
  19. class ProcessUtils
  20. {
  21. /**
  22. * This class should not be instantiated.
  23. */
  24. private function __construct()
  25. {
  26. }
  27. /**
  28. * Escapes a string to be used as a shell argument.
  29. *
  30. * @param string $argument The argument that will be escaped
  31. *
  32. * @return string The escaped argument
  33. *
  34. * @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.
  35. */
  36. public static function escapeArgument($argument)
  37. {
  38. @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);
  39. //Fix for PHP bug #43784 escapeshellarg removes % from given string
  40. //Fix for PHP bug #49446 escapeshellarg doesn't work on Windows
  41. //@see https://bugs.php.net/bug.php?id=43784
  42. //@see https://bugs.php.net/bug.php?id=49446
  43. if ('\\' === \DIRECTORY_SEPARATOR) {
  44. if ('' === $argument) {
  45. return escapeshellarg($argument);
  46. }
  47. $escapedArgument = '';
  48. $quote = false;
  49. foreach (preg_split('/(")/', $argument, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE) as $part) {
  50. if ('"' === $part) {
  51. $escapedArgument .= '\\"';
  52. } elseif (self::isSurroundedBy($part, '%')) {
  53. // Avoid environment variable expansion
  54. $escapedArgument .= '^%"'.substr($part, 1, -1).'"^%';
  55. } else {
  56. // escape trailing backslash
  57. if ('\\' === substr($part, -1)) {
  58. $part .= '\\';
  59. }
  60. $quote = true;
  61. $escapedArgument .= $part;
  62. }
  63. }
  64. if ($quote) {
  65. $escapedArgument = '"'.$escapedArgument.'"';
  66. }
  67. return $escapedArgument;
  68. }
  69. return "'".str_replace("'", "'\\''", $argument)."'";
  70. }
  71. /**
  72. * Validates and normalizes a Process input.
  73. *
  74. * @param string $caller The name of method call that validates the input
  75. * @param mixed $input The input to validate
  76. *
  77. * @return mixed The validated input
  78. *
  79. * @throws InvalidArgumentException In case the input is not valid
  80. */
  81. public static function validateInput($caller, $input)
  82. {
  83. if (null !== $input) {
  84. if (\is_resource($input)) {
  85. return $input;
  86. }
  87. if (\is_string($input)) {
  88. return $input;
  89. }
  90. if (is_scalar($input)) {
  91. return (string) $input;
  92. }
  93. if ($input instanceof Process) {
  94. return $input->getIterator($input::ITER_SKIP_ERR);
  95. }
  96. if ($input instanceof \Iterator) {
  97. return $input;
  98. }
  99. if ($input instanceof \Traversable) {
  100. return new \IteratorIterator($input);
  101. }
  102. throw new InvalidArgumentException(sprintf('%s only accepts strings, Traversable objects or stream resources.', $caller));
  103. }
  104. return $input;
  105. }
  106. private static function isSurroundedBy($arg, $char)
  107. {
  108. return 2 < \strlen($arg) && $char === $arg[0] && $char === $arg[\strlen($arg) - 1];
  109. }
  110. }