CLI   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 183
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 2
cbo 2
dl 0
loc 183
ccs 39
cts 39
cp 1
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setStdIn() 0 5 1
A setStdOut() 0 5 1
A setStdErr() 0 5 1
A readLine() 0 6 1
A writeStdout() 0 6 1
A writeStdoutLine() 0 5 1
A writeStderrLine() 0 5 1
A writeStderr() 0 6 1
A setPrompt() 0 5 1
A __construct() 0 13 2
1
<?php
2
namespace Nkey\Caribu\Console;
3
4
/**
5
 * This class is part of Caribu command line interface framework
6
 *
7
 * @author Maik Greubel <[email protected]>
8
 */
9
class CLI
10
{
11
12
    /**
13
     * The command line prompt
14
     *
15
     * @var string
16
     */
17
    private $prompt;
18
19
    /**
20
     * Handle for stdin channel
21
     *
22
     * @var resource
23
     */
24
    private $stdin;
25
26
    /**
27
     * Handle for stdout channel
28
     *
29
     * @var resource
30
     */
31
    private $stdout;
32
33
    /**
34
     * Handle for stderr channel
35
     *
36
     * @var resource
37
     */
38
    private $stderr;
39
40
    /**
41
     * Command line parser
42
     *
43
     * @var Parser
44
     */
45
    private $parser;
46
47
    /**
48
     * Create a new CLI instance
49
     *
50
     * @param string $prompt
51
     *            Optional command line prompt (default 'cli')
52
     * @param \Nkey\Caribu\Console\Parser $parser
53
     *            Optional command line parser (default instance of \Nkey\Caribu\Console\DefaultParser)
54
     */
55 2
    public function __construct(string $prompt = "cli", Parser $parser = null)
56
    {
57 2
        $this->setPrompt($prompt);
58
        
59 2
        $this->parser = $parser;
60 2
        if (null === $this->parser) {
61 2
            $this->parser = new DefaultParser();
62
        }
63
        
64 2
        $this->stdin = fopen("php://stdin", "r");
65 2
        $this->stdout = fopen("php://stdout", "w");
66 2
        $this->stderr = fopen("php://stderr", "w");
67 2
    }
68
69
    /**
70
     * Set a custom stdin channel
71
     *
72
     * @param resource $stdin
73
     *            The channel
74
     * @return \Nkey\Caribu\Console\CLI
75
     */
76 2
    public function setStdIn($stdin): CLI
77
    {
78 2
        $this->stdin = $stdin;
79 2
        return $this;
80
    }
81
82
    /**
83
     * Set a custom stdout channel
84
     *
85
     * @param resource $stdout
86
     *            The channel
87
     * @return \Nkey\Caribu\Console\CLI
88
     */
89 2
    public function setStdOut($stdout): CLI
90
    {
91 2
        $this->stdout = $stdout;
92 2
        return $this;
93
    }
94
95
    /**
96
     * Set a custom stderr channel
97
     *
98
     * @param resource $stderr
99
     *            The channel
100
     * @return \Nkey\Caribu\Console\CLI
101
     */
102 2
    public function setStdErr($stderr): CLI
103
    {
104 2
        $this->stderr = $stderr;
105 2
        return $this;
106
    }
107
108
    /**
109
     * Read full line from stdin
110
     *
111
     * @return ParsedCommand The parsed command
112
     */
113 2
    public function readLine(): ParsedCommand
114
    {
115 2
        $this->writeStdout(sprintf("%s > ", $this->prompt));
116 2
        $input = stream_get_line($this->stdin, 4096, PHP_EOL);
117 2
        return $this->parser->parse(trim($input));
118
    }
119
120
    /**
121
     * Write output to stdout channel
122
     *
123
     * @param string $output
124
     *            The output to write
125
     *
126
     * @return \Nkey\Caribu\Console\CLI Fluent interface instance
127
     */
128 2
    public function writeStdout(string $output): CLI
129
    {
130 2
        fprintf($this->stdout, "%s", $output);
131 2
        fflush($this->stdout);
132 2
        return $this;
133
    }
134
135
    /**
136
     * Write a line to stdout channel
137
     *
138
     * @param string $line
139
     *            The line to write, line break will be added
140
     *
141
     * @return \Nkey\Caribu\Console\CLI
142
     */
143 1
    public function writeStdoutLine(string $line): CLI
144
    {
145 1
        $this->writeStdout(sprintf("%s%s", $line, PHP_EOL));
146 1
        return $this;
147
    }
148
149
    /**
150
     * Write a line to stderr channel
151
     *
152
     * @param string $line
153
     *            The line to write, line break will be added
154
     *
155
     * @return \Nkey\Caribu\Console\CLI
156
     */
157 1
    public function writeStderrLine(string $line): CLI
158
    {
159 1
        $this->writeStderr(sprintf("%s%s", $line, PHP_EOL));
160 1
        return $this;
161
    }
162
163
    /**
164
     * Write output to stderr channel
165
     *
166
     * @param string $output
167
     *            The output to write
168
     *
169
     * @return \Nkey\Caribu\Console\CLI Fluent interface instance
170
     */
171 1
    public function writeStderr(string $output): CLI
172
    {
173 1
        fprintf($this->stderr, "%s", $output);
174 1
        fflush($this->stderr);
175 1
        return $this;
176
    }
177
178
    /**
179
     * Set the prompt
180
     *
181
     * @param string $prompt
182
     *            The prompt to set
183
     *
184
     * @return \Nkey\Caribu\Console\CLI Fluent interface instance
185
     */
186 2
    public function setPrompt(string $prompt): CLI
187
    {
188 2
        $this->prompt = $prompt;
189 2
        return $this;
190
    }
191
}
192