ProcessUtils.php 1.7 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. 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. * Validates and normalizes a Process input.
  29. *
  30. * @param string $caller The name of method call that validates the input
  31. * @param mixed $input The input to validate
  32. *
  33. * @throws InvalidArgumentException In case the input is not valid
  34. */
  35. public static function validateInput(string $caller, mixed $input): mixed
  36. {
  37. if (null !== $input) {
  38. if (\is_resource($input)) {
  39. return $input;
  40. }
  41. if (\is_scalar($input)) {
  42. return (string) $input;
  43. }
  44. if ($input instanceof Process) {
  45. return $input->getIterator($input::ITER_SKIP_ERR);
  46. }
  47. if ($input instanceof \Iterator) {
  48. return $input;
  49. }
  50. if ($input instanceof \Traversable) {
  51. return new \IteratorIterator($input);
  52. }
  53. throw new InvalidArgumentException(sprintf('"%s" only accepts strings, Traversable objects or stream resources.', $caller));
  54. }
  55. return $input;
  56. }
  57. }