/frameworks/lithium/0.9.9/libraries/lithium/console/Response.php

https://github.com/ggunlugu/ornekler · PHP · 144 lines · 64 code · 17 blank · 63 comment · 6 complexity · ad6dd75d1b762d1f3a742cba9e835bf0 MD5 · raw file

  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2010, 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 evalutated.
  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. * @return void
  46. */
  47. public function __construct($config = array()) {
  48. $defaults = array('output' => null, 'error' => null);
  49. $config += $defaults;
  50. $this->output = $config['output'];
  51. if (!is_resource($this->output)) {
  52. $this->output = fopen('php://stdout', 'r');;
  53. }
  54. $this->error = $config['error'];
  55. if (!is_resource($this->error)) {
  56. $this->error = fopen('php://stderr', 'r');;
  57. }
  58. parent::__construct($config);
  59. }
  60. /**
  61. * Writes string to output stream
  62. *
  63. * @param string $output
  64. * @return mixed
  65. */
  66. public function output($output) {
  67. return fwrite($this->output, String::insert($output, $this->styles()));
  68. }
  69. /**
  70. * Writes string to error stream
  71. *
  72. * @param string $error
  73. * @return mixed
  74. */
  75. public function error($error) {
  76. return fwrite($this->error, String::insert($error, $this->styles()));
  77. }
  78. /**
  79. * Destructor to close streams
  80. *
  81. * @return void
  82. *
  83. **/
  84. public function __destruct() {
  85. if ($this->output) {
  86. fclose($this->output);
  87. }
  88. if ($this->error) {
  89. fclose($this->error);
  90. }
  91. }
  92. /**
  93. * Handles styling output.
  94. *
  95. * @param array $styles
  96. * @return array
  97. */
  98. public function styles($styles = array()) {
  99. $defaults = array(
  100. 'heading1' => "\033[1;30;46m",
  101. 'heading2' => "\033[1;35m",
  102. 'heading3' => "\033[1;34m",
  103. 'option' => "\033[40;37m",
  104. 'command' => "\033[1;40;37m",
  105. 'error' => "\033[0;31m",
  106. 'success' => "\033[0;32m",
  107. 'black' => "\033[0;30m",
  108. 'red' => "\033[0;31m",
  109. 'green' => "\033[0;32m",
  110. 'yellow' => "\033[0;33m",
  111. 'blue' => "\033[0;34m",
  112. 'purple' => "\033[0;35m",
  113. 'cyan' => "\033[0;36m",
  114. 'white' => "\033[0;37m",
  115. 'end' => "\033[0m",
  116. );
  117. if ($styles === false) {
  118. return array_combine(array_keys($defaults), array_pad(array(), count($defaults), null));
  119. }
  120. $styles += $defaults;
  121. if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
  122. return $this->styles(false);
  123. }
  124. return $styles;
  125. }
  126. }
  127. ?>