PageRenderTime 30ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Symfony/Component/Console/Output/StreamOutput.php

https://bitbucket.org/ksekar/campus
PHP | 113 lines | 38 code | 12 blank | 63 comment | 6 complexity | e47e124fc56bd4b89eb0ef7dff98f9f2 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, MIT
  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\Console\Output;
  11. use Symfony\Component\Console\Formatter\OutputFormatter;
  12. /**
  13. * StreamOutput writes the output to a given stream.
  14. *
  15. * Usage:
  16. *
  17. * $output = new StreamOutput(fopen('php://stdout', 'w'));
  18. *
  19. * As `StreamOutput` can use any stream, you can also use a file:
  20. *
  21. * $output = new StreamOutput(fopen('/path/to/output.log', 'a', false));
  22. *
  23. * @author Fabien Potencier <fabien@symfony.com>
  24. *
  25. * @api
  26. */
  27. class StreamOutput extends Output
  28. {
  29. private $stream;
  30. /**
  31. * Constructor.
  32. *
  33. * @param mixed $stream A stream resource
  34. * @param integer $verbosity The verbosity level (self::VERBOSITY_QUIET, self::VERBOSITY_NORMAL,
  35. * self::VERBOSITY_VERBOSE)
  36. * @param Boolean $decorated Whether to decorate messages or not (null for auto-guessing)
  37. * @param OutputFormatter $formatter Output formatter instance
  38. *
  39. * @throws \InvalidArgumentException When first argument is not a real stream
  40. *
  41. * @api
  42. */
  43. public function __construct($stream, $verbosity = self::VERBOSITY_NORMAL, $decorated = null, OutputFormatter $formatter = null)
  44. {
  45. if (!is_resource($stream) || 'stream' !== get_resource_type($stream)) {
  46. throw new \InvalidArgumentException('The StreamOutput class needs a stream as its first argument.');
  47. }
  48. $this->stream = $stream;
  49. if (null === $decorated) {
  50. $decorated = $this->hasColorSupport($decorated);
  51. }
  52. parent::__construct($verbosity, $decorated, $formatter);
  53. }
  54. /**
  55. * Gets the stream attached to this StreamOutput instance.
  56. *
  57. * @return resource A stream resource
  58. */
  59. public function getStream()
  60. {
  61. return $this->stream;
  62. }
  63. /**
  64. * Writes a message to the output.
  65. *
  66. * @param string $message A message to write to the output
  67. * @param Boolean $newline Whether to add a newline or not
  68. *
  69. * @throws \RuntimeException When unable to write output (should never happen)
  70. */
  71. public function doWrite($message, $newline)
  72. {
  73. if (false === @fwrite($this->stream, $message.($newline ? PHP_EOL : ''))) {
  74. // @codeCoverageIgnoreStart
  75. // should never happen
  76. throw new \RuntimeException('Unable to write output.');
  77. // @codeCoverageIgnoreEnd
  78. }
  79. fflush($this->stream);
  80. }
  81. /**
  82. * Returns true if the stream supports colorization.
  83. *
  84. * Colorization is disabled if not supported by the stream:
  85. *
  86. * - windows without ansicon
  87. * - non tty consoles
  88. *
  89. * @return Boolean true if the stream supports colorization, false otherwise
  90. */
  91. protected function hasColorSupport()
  92. {
  93. // @codeCoverageIgnoreStart
  94. if (DIRECTORY_SEPARATOR == '\\') {
  95. return false !== getenv('ANSICON');
  96. }
  97. return function_exists('posix_isatty') && @posix_isatty($this->stream);
  98. // @codeCoverageIgnoreEnd
  99. }
  100. }