Response.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2012, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\console;
  9. use lithium\util\String;
  10. /**
  11. * The `Response` class is used by other console classes to generate output. It contains stream
  12. * resources for writing output and errors, as well as shell coloring information, and the response
  13. * status code for the currently-executing command.
  14. */
  15. class Response extends \lithium\core\Object {
  16. /**
  17. * Output stream, STDOUT
  18. *
  19. * @var stream
  20. */
  21. public $output = null;
  22. /**
  23. * Error stream, STDERR
  24. *
  25. * @var stream
  26. */
  27. public $error = null;
  28. /**
  29. * Status code, most often used for setting an exit status.
  30. *
  31. * It should be expected that only status codes in the range of 0-255
  32. * can be properly evaluated.
  33. *
  34. * @var integer
  35. * @see lithium\console\Command
  36. */
  37. public $status = 0;
  38. /**
  39. * Construct Request object
  40. *
  41. * @param array $config
  42. * - request object lithium\console\Request
  43. * - output stream
  44. * _ error stream
  45. */
  46. public function __construct($config = array()) {
  47. $defaults = array('output' => null, 'error' => null);
  48. $config += $defaults;
  49. $this->output = $config['output'];
  50. if (!is_resource($this->output)) {
  51. $this->output = fopen('php://stdout', 'r');
  52. }
  53. $this->error = $config['error'];
  54. if (!is_resource($this->error)) {
  55. $this->error = fopen('php://stderr', 'r');
  56. }
  57. parent::__construct($config);
  58. }
  59. /**
  60. * Writes string to output stream
  61. *
  62. * @param string $output
  63. * @return mixed
  64. */
  65. public function output($output) {
  66. return fwrite($this->output, String::insert($output, $this->styles()));
  67. }
  68. /**
  69. * Writes string to error stream
  70. *
  71. * @param string $error
  72. * @return mixed
  73. */
  74. public function error($error) {
  75. return fwrite($this->error, String::insert($error, $this->styles()));
  76. }
  77. /**
  78. * Destructor to close streams
  79. *
  80. * @return void
  81. *
  82. */
  83. public function __destruct() {
  84. if ($this->output) {
  85. fclose($this->output);
  86. }
  87. if ($this->error) {
  88. fclose($this->error);
  89. }
  90. }
  91. /**
  92. * Handles styling output.
  93. *
  94. * @param array $styles
  95. * @return array
  96. */
  97. public function styles($styles = array()) {
  98. $defaults = array(
  99. 'end' => "\033[0m",
  100. 'black' => "\033[0;30m",
  101. 'red' => "\033[0;31m",
  102. 'green' => "\033[0;32m",
  103. 'yellow' => "\033[0;33m",
  104. 'blue' => "\033[0;34m",
  105. 'purple' => "\033[0;35m",
  106. 'cyan' => "\033[0;36m",
  107. 'white' => "\033[0;37m",
  108. 'heading' => "\033[1;36m",
  109. 'option' => "\033[0;35m",
  110. 'command' => "\033[0;35m",
  111. 'error' => "\033[0;31m",
  112. 'success' => "\033[0;32m",
  113. 'bold' => "\033[1m",
  114. );
  115. if ($styles === false) {
  116. return array_combine(array_keys($defaults), array_pad(array(), count($defaults), null));
  117. }
  118. $styles += $defaults;
  119. if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
  120. return $this->styles(false);
  121. }
  122. return $styles;
  123. }
  124. }
  125. ?>