View file vendor/laravel/framework/src/Illuminate/Support/ProcessUtils.php

File size: 2Kb
  1. <?php
  2.  
  3. namespace Illuminate\Support;
  4.  
  5. /**
  6. * ProcessUtils is a bunch of utility methods.
  7. *
  8. * This class was originally copied from Symfony 3.
  9. */
  10. class ProcessUtils
  11. {
  12. /**
  13. * Escapes a string to be used as a shell argument.
  14. *
  15. * @param string $argument
  16. * @return string
  17. */
  18. public static function escapeArgument($argument)
  19. {
  20. // Fix for PHP bug #43784 escapeshellarg removes % from given string
  21. // Fix for PHP bug #49446 escapeshellarg doesn't work on Windows
  22. // @see https://bugs.php.net/bug.php?id=43784
  23. // @see https://bugs.php.net/bug.php?id=49446
  24. if ('\\' === DIRECTORY_SEPARATOR) {
  25. if ('' === $argument) {
  26. return '""';
  27. }
  28.  
  29. $escapedArgument = '';
  30. $quote = false;
  31.  
  32. foreach (preg_split('/(")/', $argument, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE) as $part) {
  33. if ('"' === $part) {
  34. $escapedArgument .= '\\"';
  35. } elseif (self::isSurroundedBy($part, '%')) {
  36. // Avoid environment variable expansion
  37. $escapedArgument .= '^%"'.substr($part, 1, -1).'"^%';
  38. } else {
  39. // escape trailing backslash
  40. if ('\\' === substr($part, -1)) {
  41. $part .= '\\';
  42. }
  43. $quote = true;
  44. $escapedArgument .= $part;
  45. }
  46. }
  47.  
  48. if ($quote) {
  49. $escapedArgument = '"'.$escapedArgument.'"';
  50. }
  51.  
  52. return $escapedArgument;
  53. }
  54.  
  55. return "'".str_replace("'", "'\\''", $argument)."'";
  56. }
  57.  
  58. /**
  59. * Is the given string surrounded by the given character?
  60. *
  61. * @param string $arg
  62. * @param string $char
  63. * @return bool
  64. */
  65. protected static function isSurroundedBy($arg, $char)
  66. {
  67. return 2 < strlen($arg) && $char === $arg[0] && $char === $arg[strlen($arg) - 1];
  68. }
  69. }