BufferIO.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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\Output\StreamOutput;
  13. use Symfony\Component\Console\Formatter\OutputFormatterInterface;
  14. use Symfony\Component\Console\Input\StringInput;
  15. use Symfony\Component\Console\Helper\HelperSet;
  16. /**
  17. * @author Jordi Boggiano <j.boggiano@seld.be>
  18. */
  19. class BufferIO extends ConsoleIO
  20. {
  21. /**
  22. * @param string $input
  23. * @param int $verbosity
  24. * @param OutputFormatterInterface $formatter
  25. */
  26. public function __construct($input = '', $verbosity = null, OutputFormatterInterface $formatter = null)
  27. {
  28. $input = new StringInput($input);
  29. $input->setInteractive(false);
  30. $output = new StreamOutput(fopen('php://memory', 'rw'), $verbosity === null ? StreamOutput::VERBOSITY_NORMAL : $verbosity, !empty($formatter), $formatter);
  31. parent::__construct($input, $output, new HelperSet(array()));
  32. }
  33. public function getOutput()
  34. {
  35. fseek($this->output->getStream(), 0);
  36. $output = stream_get_contents($this->output->getStream());
  37. $output = preg_replace_callback("{(?<=^|\n|\x08)(.+?)(\x08+)}", function ($matches) {
  38. $pre = strip_tags($matches[1]);
  39. if (strlen($pre) === strlen($matches[2])) {
  40. return '';
  41. }
  42. // TODO reverse parse the string, skipping span tags and \033\[([0-9;]+)m(.*?)\033\[0m style blobs
  43. return rtrim($matches[1])."\n";
  44. }, $output);
  45. return $output;
  46. }
  47. }