Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 142 lines (125 sloc) 3.054 kb
e7f3c31 @gwoo going lithium
gwoo authored
1 <?php
2 /**
3 * Lithium: the most rad php framework
4 *
14de7bf @gwoo Happy 2012!
gwoo authored
5 * @copyright Copyright 2012, Union of RAD (http://union-of-rad.org)
e7f3c31 @gwoo going lithium
gwoo authored
6 * @license http://opensource.org/licenses/bsd-license.php The BSD License
7 */
f65a402 @gwoo updating headers
gwoo authored
8
e7f3c31 @gwoo going lithium
gwoo authored
9 namespace lithium\console;
10
690f4c3 @nateabele Fixing formatting in console classes. Adding test coverage for `Extra…
nateabele authored
11 use lithium\util\String;
fc0c326 Refactoring to move filter out to Response.
psychic authored
12
e7f3c31 @gwoo going lithium
gwoo authored
13 /**
8a1491d @nateabele Adding class-level docblocks for various classes.
nateabele authored
14 * The `Response` class is used by other console classes to generate output. It contains stream
15 * resources for writing output and errors, as well as shell coloring information, and the response
16 * status code for the currently-executing command.
17 */
e7f3c31 @gwoo going lithium
gwoo authored
18 class Response extends \lithium\core\Object {
19
20 /**
21 * Output stream, STDOUT
22 *
23 * @var stream
c690b8b @davidpersson QA: Fixing docblock closings `**/` -> `*/`.
davidpersson authored
24 */
e7f3c31 @gwoo going lithium
gwoo authored
25 public $output = null;
26
27 /**
28 * Error stream, STDERR
29 *
30 * @var stream
c690b8b @davidpersson QA: Fixing docblock closings `**/` -> `*/`.
davidpersson authored
31 */
e7f3c31 @gwoo going lithium
gwoo authored
32 public $error = null;
33
34 /**
9fc85ec @davidpersson Updating console return value handling.
davidpersson authored
35 * Status code, most often used for setting an exit status.
36 *
37 * It should be expected that only status codes in the range of 0-255
7ecf90a @daschl QA: typo fixes.
daschl authored
38 * can be properly evaluated.
9fc85ec @davidpersson Updating console return value handling.
davidpersson authored
39 *
40 * @var integer
41 * @see lithium\console\Command
42 */
43 public $status = 0;
44
45 /**
e7f3c31 @gwoo going lithium
gwoo authored
46 * Construct Request object
47 *
48 * @param array $config
49 * - request object lithium\console\Request
50 * - output stream
51 * _ error stream
aa5fa27 @nateabele Removing extra lines from header blocks, removing `@access` docblock …
nateabele authored
52 */
e7f3c31 @gwoo going lithium
gwoo authored
53 public function __construct($config = array()) {
9816be1 @gwoo some cleanup to \console\Command and \console\Response
gwoo authored
54 $defaults = array('output' => null, 'error' => null);
e7f3c31 @gwoo going lithium
gwoo authored
55 $config += $defaults;
56
57 $this->output = $config['output'];
9816be1 @gwoo some cleanup to \console\Command and \console\Response
gwoo authored
58
e7f3c31 @gwoo going lithium
gwoo authored
59 if (!is_resource($this->output)) {
799bd2d @nateabele Fixing formatting in `\console\Response`.
nateabele authored
60 $this->output = fopen('php://stdout', 'r');
e7f3c31 @gwoo going lithium
gwoo authored
61 }
62
63 $this->error = $config['error'];
9fc85ec @davidpersson Updating console return value handling.
davidpersson authored
64
e7f3c31 @gwoo going lithium
gwoo authored
65 if (!is_resource($this->error)) {
799bd2d @nateabele Fixing formatting in `\console\Response`.
nateabele authored
66 $this->error = fopen('php://stderr', 'r');
e7f3c31 @gwoo going lithium
gwoo authored
67 }
68 parent::__construct($config);
69 }
70
71 /**
72 * Writes string to output stream
73 *
0a65d07 @gwoo refactoring \console\Command
gwoo authored
74 * @param string $output
e7f3c31 @gwoo going lithium
gwoo authored
75 * @return mixed
76 */
0a65d07 @gwoo refactoring \console\Command
gwoo authored
77 public function output($output) {
78 return fwrite($this->output, String::insert($output, $this->styles()));
e7f3c31 @gwoo going lithium
gwoo authored
79 }
80
81 /**
82 * Writes string to error stream
83 *
0a65d07 @gwoo refactoring \console\Command
gwoo authored
84 * @param string $error
e7f3c31 @gwoo going lithium
gwoo authored
85 * @return mixed
86 */
fc0c326 Refactoring to move filter out to Response.
psychic authored
87 public function error($error) {
917e4e5 @gwoo some more work on refactor of \console\Command
gwoo authored
88 return fwrite($this->error, String::insert($error, $this->styles()));
e7f3c31 @gwoo going lithium
gwoo authored
89 }
90
91 /**
92 * Destructor to close streams
93 *
94 * @return void
95 *
c690b8b @davidpersson QA: Fixing docblock closings `**/` -> `*/`.
davidpersson authored
96 */
e7f3c31 @gwoo going lithium
gwoo authored
97 public function __destruct() {
164f2ed @nateabele Adding sanity checking to destructors in `\console\Request` and `\con…
nateabele authored
98 if ($this->output) {
99 fclose($this->output);
100 }
101 if ($this->error) {
102 fclose($this->error);
103 }
e7f3c31 @gwoo going lithium
gwoo authored
104 }
0a65d07 @gwoo refactoring \console\Command
gwoo authored
105
fc0c326 Refactoring to move filter out to Response.
psychic authored
106 /**
107 * Handles styling output.
108 *
0a65d07 @gwoo refactoring \console\Command
gwoo authored
109 * @param array $styles
fc0c326 Refactoring to move filter out to Response.
psychic authored
110 * @return array
111 */
112 public function styles($styles = array()) {
113 $defaults = array(
af55801 @davidpersson Reordering styles in console response.
davidpersson authored
114 'end' => "\033[0m",
fc0c326 Refactoring to move filter out to Response.
psychic authored
115 'black' => "\033[0;30m",
116 'red' => "\033[0;31m",
117 'green' => "\033[0;32m",
118 'yellow' => "\033[0;33m",
119 'blue' => "\033[0;34m",
120 'purple' => "\033[0;35m",
121 'cyan' => "\033[0;36m",
122 'white' => "\033[0;37m",
af55801 @davidpersson Reordering styles in console response.
davidpersson authored
123 'heading' => "\033[1;36m",
124 'option' => "\033[0;35m",
125 'command' => "\033[0;35m",
126 'error' => "\033[0;31m",
ed45e5c @dshafik Add {:bold} style, show all docblock in help, typo
dshafik authored
127 'success' => "\033[0;32m",
128 'bold' => "\033[1m",
0a65d07 @gwoo refactoring \console\Command
gwoo authored
129 );
917e4e5 @gwoo some more work on refactor of \console\Command
gwoo authored
130 if ($styles === false) {
131 return array_combine(array_keys($defaults), array_pad(array(), count($defaults), null));
132 }
fc0c326 Refactoring to move filter out to Response.
psychic authored
133 $styles += $defaults;
0a65d07 @gwoo refactoring \console\Command
gwoo authored
134
fc0c326 Refactoring to move filter out to Response.
psychic authored
135 if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
917e4e5 @gwoo some more work on refactor of \console\Command
gwoo authored
136 return $this->styles(false);
fc0c326 Refactoring to move filter out to Response.
psychic authored
137 }
138 return $styles;
139 }
e7f3c31 @gwoo going lithium
gwoo authored
140 }
4b13798 @alkemann hooks fixes for lithium/console
alkemann authored
141
e7f3c31 @gwoo going lithium
gwoo authored
142 ?>
Something went wrong with that request. Please try again.