ProcessUtils.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. * @return mixed The validated input
  34. *
  35. * @throws InvalidArgumentException In case the input is not valid
  36. */
  37. public static function validateInput($caller, $input)
  38. {
  39. if (null !== $input) {
  40. if (\is_resource($input)) {
  41. return $input;
  42. }
  43. if (\is_string($input)) {
  44. return $input;
  45. }
  46. if (is_scalar($input)) {
  47. return (string) $input;
  48. }
  49. if ($input instanceof Process) {
  50. return $input->getIterator($input::ITER_SKIP_ERR);
  51. }
  52. if ($input instanceof \Iterator) {
  53. return $input;
  54. }
  55. if ($input instanceof \Traversable) {
  56. return new \IteratorIterator($input);
  57. }
  58. throw new InvalidArgumentException(sprintf('%s only accepts strings, Traversable objects or stream resources.', $caller));
  59. }
  60. return $input;
  61. }
  62. }