summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/Utils/CLI.php23
-rw-r--r--tests/Utils/CLITest.php26
2 files changed, 49 insertions, 0 deletions
diff --git a/src/Utils/CLI.php b/src/Utils/CLI.php
index 31d09ce..79e88dc 100644
--- a/src/Utils/CLI.php
+++ b/src/Utils/CLI.php
@@ -77,6 +77,11 @@ class CLI
return 0;
}
+ if (!isset($params['q'])) {
+ if ($stdIn = $this->readStdin()) {
+ $params['q'] = $stdIn;
+ }
+ }
if (isset($params['q'])) {
echo Formatter::format(
$params['q'],
@@ -127,6 +132,11 @@ class CLI
if (isset($params['c'])) {
Context::load($params['c']);
}
+ if (!isset($params['q'])) {
+ if ($stdIn = $this->readStdin()) {
+ $params['q'] = $stdIn;
+ }
+ }
if (isset($params['q'])) {
$lexer = new Lexer($params['q'], false);
$parser = new Parser($lexer->list);
@@ -177,6 +187,11 @@ class CLI
return 0;
}
+ if (!isset($params['q'])) {
+ if ($stdIn = $this->readStdin()) {
+ $params['q'] = $stdIn;
+ }
+ }
if (isset($params['q'])) {
$lexer = new Lexer($params['q'], false);
foreach ($lexer->list->tokens as $idx => $token) {
@@ -199,4 +214,12 @@ class CLI
return 1;
}
+
+ private function readStdin() {
+ stream_set_blocking(STDIN, false);
+ $stdin = stream_get_contents(STDIN);
+ // restore-default block-mode setting
+ stream_set_blocking(STDIN, true);
+ return $stdin;
+ }
}
diff --git a/tests/Utils/CLITest.php b/tests/Utils/CLITest.php
index bacf5fb..d9eadae 100644
--- a/tests/Utils/CLITest.php
+++ b/tests/Utils/CLITest.php
@@ -205,4 +205,30 @@ class CLITest extends TestCase
],
];
}
+
+ /**
+ * @dataProvider stdinParams
+ *
+ * @param string $cmd
+ * @param int $result
+ */
+ public function testStdinPipe($cmd, $result)
+ {
+ exec ($cmd, $out, $ret);
+ $this->assertSame($result, $ret);
+ }
+
+ public function stdinParams()
+ {
+ $binPath = PHP_BINARY .' '. dirname(__DIR__,2 ). '/bin/';
+
+ return [
+ ['echo "SELECT 1" | '. $binPath .'highlight-query', 0],
+ ['echo "invalid query" | '. $binPath .'highlight-query', 0],
+ ['echo "SELECT 1" | '. $binPath .'lint-query', 0],
+ ['echo "invalid query" | '. $binPath .'lint-query', 10],
+ ['echo "SELECT 1" | '. $binPath .'tokenize-query', 0],
+ ['echo "invalid query" | '. $binPath .'tokenize-query', 0],
+ ];
+ }
}