Update to SQL Parser 4.1.9
[php-sqllint.git] / src / phpsqllint / Cli.php
index 129a9257d736826bc43bfec9599c376b1e99f211..eecc9b789617be9826915d1e7db5e21547eda76c 100644 (file)
@@ -11,7 +11,7 @@
  * @link     http://cweiske.de/php-sqllint.htm
  */
 namespace phpsqllint;
-use SqlParser\Parser;
+use PhpMyAdmin\SqlParser\Parser;
 
 require_once 'Console/CommandLine.php';
 
@@ -29,7 +29,14 @@ class Cli
     protected $renderer;
 
     protected $format = false;
-    protected $colors = false;
+
+    /**
+     * What syntax highlighting mode should be used
+     *
+     * none, ansi, html
+     */
+    protected $highlight = 'none';
+
 
     /**
      * Start processing.
@@ -77,7 +84,7 @@ class Cli
             return false;
         }
 
-        $parser = new \SqlParser\Parser($sql);
+        $parser = new \PhpMyAdmin\SqlParser\Parser($sql);
         if (count($parser->errors) == 0) {
             $this->renderer->finishOk();
             return true;
@@ -91,7 +98,7 @@ class Cli
         }
 
         foreach ($parser->errors as $error) {
-            /* @var SqlParser\Exceptions\ParserException $error) */
+            /* @var PhpMyAdmin\SqlParser\Exceptions\ParserException $error) */
             reset($lines);
             $line = 1;
             while (next($lines) && $error->token->position >= current($lines)) {
@@ -122,11 +129,15 @@ class Cli
             return false;
         }
 
+        $typeMap = array(
+            'none' => 'text',
+            'ansi' => 'cli',
+            'html' => 'html',
+        );
         $options = array(
-            //FIXME: automatically detect if the shell/tool supports colors
-            'type' => $this->colors ? 'cli' : 'text'
+            'type' => $typeMap[$this->highlight],
         );
-        echo \SqlParser\Utils\Formatter::format($sql, $options) . "\n";
+        echo \PhpMyAdmin\SqlParser\Utils\Formatter::format($sql, $options) . "\n";
     }
 
     protected function loadSql($filename)
@@ -152,9 +163,14 @@ class Cli
     {
         $parser = new \Console_CommandLine();
         $parser->description = 'php-sqllint';
-        $parser->version = '0.0.2';
+        $parser->version = 'dev';
         $parser->avoid_reading_stdin = true;
 
+        $versionFile = __DIR__ . '/../../VERSION';
+        if (file_exists($versionFile)) {
+            $parser->version = trim(file_get_contents($versionFile));
+        }
+
         $parser->addOption(
             'format',
             array(
@@ -166,13 +182,20 @@ class Cli
             )
         );
         $parser->addOption(
-            'colors',
+            'highlight',
             array(
-                'short_name'  => '-c',
-                'long_name'   => '--colors',
-                'description' => 'Use colors in formatting output',
-                'action'      => 'StoreTrue',
-                'default'     => false,
+                'short_name'  => '-h',
+                'long_name'   => '--highlight',
+                'description' => 'Highlighting mode (when using --format)',
+                'action'      => 'StoreString',
+                'choices'     => array(
+                    'none',
+                    'ansi',
+                    'html',
+                    'auto',
+                ),
+                'default' => 'auto',
+                'add_list_option' => true,
             )
         );
         $parser->addOption(
@@ -219,7 +242,23 @@ class Cli
             $this->renderer = new $rendClass();
 
             $this->format = $result->options['format'];
-            $this->colors = $result->options['colors'];
+
+            $this->highlight = $result->options['highlight'];
+            if ($this->highlight == 'auto') {
+                if (php_sapi_name() == 'cli') {
+                    //default coloring to enabled, except
+                    // when piping | to another tool
+                    $this->highlight = 'ansi';
+                    if (function_exists('posix_isatty')
+                        && !posix_isatty(STDOUT)
+                    ) {
+                        $this->highlight = 'none';
+                    }
+                } else {
+                    //no idea where we are, so do not highlight
+                    $this->highlight = 'none';
+                }
+            }
 
             foreach ($result->args['sql_files'] as $filename) {
                 if ($filename == '-') {