ProcessStream.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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\Mailer\Transport\Smtp\Stream;
  11. use Symfony\Component\Mailer\Exception\TransportException;
  12. /**
  13. * A stream supporting local processes.
  14. *
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. * @author Chris Corbyn
  17. *
  18. * @internal
  19. */
  20. final class ProcessStream extends AbstractStream
  21. {
  22. private $command;
  23. public function setCommand(string $command)
  24. {
  25. $this->command = $command;
  26. }
  27. public function initialize(): void
  28. {
  29. $descriptorSpec = [
  30. 0 => ['pipe', 'r'],
  31. 1 => ['pipe', 'w'],
  32. 2 => ['pipe', 'w'],
  33. ];
  34. $pipes = [];
  35. $this->stream = proc_open($this->command, $descriptorSpec, $pipes);
  36. stream_set_blocking($pipes[2], false);
  37. if ($err = stream_get_contents($pipes[2])) {
  38. throw new TransportException('Process could not be started: '.$err);
  39. }
  40. $this->in = &$pipes[0];
  41. $this->out = &$pipes[1];
  42. }
  43. public function terminate(): void
  44. {
  45. if (null !== $this->stream) {
  46. fclose($this->in);
  47. fclose($this->out);
  48. proc_close($this->stream);
  49. }
  50. parent::terminate();
  51. }
  52. protected function getReadConnectionDescription(): string
  53. {
  54. return 'process '.$this->command;
  55. }
  56. }