BufferIO.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\IO;
  12. use Symfony\Component\Console\Helper\QuestionHelper;
  13. use Symfony\Component\Console\Output\StreamOutput;
  14. use Symfony\Component\Console\Formatter\OutputFormatterInterface;
  15. use Symfony\Component\Console\Input\StringInput;
  16. use Symfony\Component\Console\Helper\HelperSet;
  17. /**
  18. * @author Jordi Boggiano <j.boggiano@seld.be>
  19. */
  20. class BufferIO extends ConsoleIO
  21. {
  22. /**
  23. * @param string $input
  24. * @param int $verbosity
  25. * @param OutputFormatterInterface|null $formatter
  26. */
  27. public function __construct($input = '', $verbosity = StreamOutput::VERBOSITY_NORMAL, OutputFormatterInterface $formatter = null)
  28. {
  29. $input = new StringInput($input);
  30. $input->setInteractive(false);
  31. $output = new StreamOutput(fopen('php://memory', 'rw'), $verbosity, $formatter ? $formatter->isDecorated() : false, $formatter);
  32. parent::__construct($input, $output, new HelperSet(array(
  33. new QuestionHelper(),
  34. )));
  35. }
  36. public function getOutput()
  37. {
  38. fseek($this->output->getStream(), 0);
  39. $output = stream_get_contents($this->output->getStream());
  40. $output = preg_replace_callback("{(?<=^|\n|\x08)(.+?)(\x08+)}", function ($matches) {
  41. $pre = strip_tags($matches[1]);
  42. if (strlen($pre) === strlen($matches[2])) {
  43. return '';
  44. }
  45. // TODO reverse parse the string, skipping span tags and \033\[([0-9;]+)m(.*?)\033\[0m style blobs
  46. return rtrim($matches[1])."\n";
  47. }, $output);
  48. return $output;
  49. }
  50. }