Skip to content

Commit 31efe42

Browse files
committed
reorganize cli and prompt script
Signed-off-by: Derek Smart <derek@grindaga.com>
1 parent 0222333 commit 31efe42

File tree

4 files changed

+65
-23
lines changed

4 files changed

+65
-23
lines changed

phpred

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@ if ($mysqli->connect_errno) {
4141
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
4242
}
4343

44-
$cli = new \PHPRed\CLI($mysqli);
44+
$cli = new \PHPRed\CLI\Prompt($mysqli);
4545
$cli->run();
4646
exit(0);

src/CLI.php

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/cli/CLI.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
namespace PHPRed\CLI;
3+
4+
abstract class CLI extends \PHPRed\PHPRed
5+
{
6+
protected $mysqli;
7+
private $stdout;
8+
private $stderr;
9+
private $stdin;
10+
11+
public function __construct(\mysqli $mysqli, $stdout = '', $stderr = '', $stdin = '')
12+
{
13+
parent::__construct($mysqli);
14+
15+
$this->stdout = $stdout ? $stdout : STDOUT;
16+
$this->stderr = $stderr ? $stderr : STDERR;
17+
$this->stdin = $stdin ? $stdin : STDIN;
18+
}
19+
20+
protected function shutDown()
21+
{
22+
fclose($this->stdout);
23+
fclose($this->stderr);
24+
fclose($this->stdin);
25+
}
26+
27+
protected function output(string $statement)
28+
{
29+
fwrite($this->stdout, $statement . "\n");
30+
}
31+
32+
protected function prompt(string $question)
33+
{
34+
$this->output($question);
35+
return trim(fgets($this->stdin));
36+
}
37+
}

src/cli/Prompt.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
namespace PHPREd\CLI;
3+
4+
class Prompt extends CLI
5+
{
6+
public function __construct(\mysqli $mysqli)
7+
{
8+
parent::__construct($mysqli);
9+
}
10+
11+
private function welcome()
12+
{
13+
$this->output('Welcome');
14+
$response = $this->prompt('What do you want to do? Type "yes".');
15+
if ($response != 'yes') {
16+
$this->output("ABORTING!");
17+
exit;
18+
}
19+
$this->output("Thank you, continuing");
20+
}
21+
22+
public function run()
23+
{
24+
$this->welcome();
25+
$this->shutDown();
26+
}
27+
}

0 commit comments

Comments
 (0)