1
|
|
|
<?php namespace Tarsana\Command\Console; |
2
|
|
|
|
3
|
|
|
use Tarsana\Command\Console\OutTransformer; |
4
|
|
|
use Tarsana\Command\Interfaces\Console\ConsoleInterface; |
5
|
|
|
use Tarsana\Command\Interfaces\Console\TransformerInterface; |
6
|
|
|
use Tarsana\IO\Interfaces\Resource\Reader as ReaderInterface; |
7
|
|
|
use Tarsana\IO\Interfaces\Resource\Writer as WriterInterface; |
8
|
|
|
use Tarsana\IO\Resource\Reader; |
9
|
|
|
use Tarsana\IO\Resource\Writer; |
10
|
|
|
use Tarsana\Syntax\Syntax; |
11
|
|
|
|
12
|
|
|
class Console implements ConsoleInterface { |
13
|
|
|
|
14
|
|
|
protected $out; |
15
|
|
|
protected $in; |
16
|
|
|
protected $err; |
17
|
|
|
protected $outTransformer; |
18
|
|
|
|
19
|
|
|
public function __construct() |
20
|
|
|
{ |
21
|
|
|
$this->out = new Writer; |
22
|
|
|
$this->err = new Writer('php://stderr'); |
23
|
|
|
$this->in = new Reader; |
24
|
|
|
$this->outTransformer = new OutTransformer; |
25
|
|
|
$aliases = [ |
26
|
|
|
'<info>' => '<color:33>', |
27
|
|
|
'</info>' => '<reset>', |
28
|
|
|
'<warn>' => '<color:220>', |
29
|
|
|
'</warn>' => '<reset>', |
30
|
|
|
'<success>' => '<color:46>', |
31
|
|
|
'</success>' => '<reset>', |
32
|
|
|
'<error>' => '<bold><color:15><background:124>', |
33
|
|
|
'</error>' => '<reset>', |
34
|
|
|
'<tab>' => ' ', |
35
|
|
|
'<br>' => PHP_EOL |
36
|
|
|
]; |
37
|
|
|
foreach ($aliases as $name => $value) { |
38
|
|
|
$this->outTransformer()->alias($name, $value); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function stdin(ReaderInterface $in = null) |
43
|
|
|
{ |
44
|
|
|
if (null === $in) |
45
|
|
|
return $this->in; |
46
|
|
|
$this->in = $in; |
47
|
|
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function stdout(WriterInterface $out = null) |
51
|
|
|
{ |
52
|
|
|
if (null === $out) |
53
|
|
|
return $this->out; |
54
|
|
|
$this->out = $out; |
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function stderr(WriterInterface $err = null) |
59
|
|
|
{ |
60
|
|
|
if (null === $err) |
61
|
|
|
return $this->err; |
62
|
|
|
$this->err = $err; |
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function outTransformer(TransformerInterface $value = null) |
67
|
|
|
{ |
68
|
|
|
if (null === $value) { |
69
|
|
|
return $this->outTransformer; |
70
|
|
|
} |
71
|
|
|
$this->outTransformer = $value; |
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function out(string $text) : ConsoleInterface |
76
|
|
|
{ |
77
|
|
|
$this->out->write($this->outTransformer()->transform($text)); |
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function line(string $text) : ConsoleInterface |
82
|
|
|
{ |
83
|
|
|
return $this->out($text . '<br>'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function error(string $text) : ConsoleInterface |
87
|
|
|
{ |
88
|
|
|
$text = "<error>{$text}</error><br>"; |
89
|
|
|
$this->err->write($this->outTransformer()->transform($text)); |
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function read() : string |
94
|
|
|
{ |
95
|
|
|
return $this->in->read(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function readLine() : string |
99
|
|
|
{ |
100
|
|
|
return $this->in->readLine(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function char() : string |
104
|
|
|
{ |
105
|
|
|
readline_callback_handler_install('', function() {}); |
106
|
|
|
$c = $this->in->read(1); |
107
|
|
|
readline_callback_handler_remove(); |
108
|
|
|
return $c; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function alias(string $name, string $value) : ConsoleInterface |
112
|
|
|
{ |
113
|
|
|
$this->outTransformer->alias($name, $value); |
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|