ProcessUtils.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. /**
  12. * ProcessUtils is a bunch of utility methods.
  13. *
  14. * This class contains static methods only and is not meant to be instantiated.
  15. *
  16. * @author Martin Hasoň <martin.hason@gmail.com>
  17. */
  18. class ProcessUtils
  19. {
  20. /**
  21. * This class should not be instantiated
  22. */
  23. private function __construct()
  24. {
  25. }
  26. /**
  27. * Escapes a string to be used as a shell argument.
  28. *
  29. * @param string $argument The argument that will be escaped
  30. *
  31. * @return string The escaped argument
  32. */
  33. public static function escapeArgument($argument)
  34. {
  35. //Fix for PHP bug #43784 escapeshellarg removes % from given string
  36. //Fix for PHP bug #49446 escapeshellarg dosn`t work on windows
  37. //@see https://bugs.php.net/bug.php?id=43784
  38. //@see https://bugs.php.net/bug.php?id=49446
  39. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  40. if ('' === $argument) {
  41. return escapeshellarg($argument);
  42. }
  43. $escapedArgument = '';
  44. foreach (preg_split('/([%"])/i', $argument, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE) as $part) {
  45. if ('"' === $part) {
  46. $escapedArgument .= '\\"';
  47. } elseif ('%' === $part) {
  48. $escapedArgument .= '^%';
  49. } else {
  50. $escapedArgument .= escapeshellarg($part);
  51. }
  52. }
  53. return $escapedArgument;
  54. }
  55. return escapeshellarg($argument);
  56. }
  57. }