1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Graze\Morphism\Command; |
4
|
|
|
|
5
|
|
|
use GlobIterator; |
6
|
|
|
use Graze\Morphism\Parse\MysqlDump; |
7
|
|
|
use Graze\Morphism\Parse\TokenStream; |
8
|
|
|
use RuntimeException; |
9
|
|
|
use Symfony\Component\Console\Command\Command; |
10
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
13
|
|
|
|
14
|
|
|
class Lint extends Command |
15
|
|
|
{ |
16
|
|
|
const COMMAND_NAME = 'lint'; |
17
|
|
|
|
18
|
|
|
// Command line arguments |
19
|
|
|
const ARGUMENT_PATH = 'path'; |
20
|
|
|
|
21
|
|
|
// Command line options |
22
|
|
|
const OPTION_VERBOSE = 'verbose'; |
23
|
|
|
const OPTION_NO_VERBOSE = 'no-verbose'; |
24
|
|
|
|
25
|
|
|
/** @var bool */ |
26
|
|
|
private $verbose = false; |
27
|
|
|
/** @var array */ |
28
|
|
|
private $paths = []; |
29
|
|
|
|
30
|
|
|
protected function configure() |
31
|
|
|
{ |
32
|
|
|
$this->setName(self::COMMAND_NAME); |
33
|
|
|
|
34
|
|
|
$helpText = sprintf( |
35
|
|
|
"Usage: %s [OPTIONS] [PATH ...]\n" . |
36
|
|
|
"Checks all schema files below the specified paths for correctness. If no PATH\n" . |
37
|
|
|
"is given, checks standard input. By default output is only produced if errors\n" . |
38
|
|
|
"are detected.\n" . |
39
|
|
|
"\n" . |
40
|
|
|
"OPTIONS\n" . |
41
|
|
|
" -h, -help, --help display this message, and exit\n" . |
42
|
|
|
" --[no-]verbose include valid files in output; default: no\n" . |
43
|
|
|
"\n" . |
44
|
|
|
"EXIT STATUS\n" . |
45
|
|
|
"The exit status will be 1 if any errors were detected, or 0 otherwise.\n" . |
46
|
|
|
"", |
47
|
|
|
self::COMMAND_NAME |
48
|
|
|
); |
49
|
|
|
$this->setHelp($helpText); |
50
|
|
|
|
51
|
|
|
$this->addArgument( |
52
|
|
|
self::ARGUMENT_PATH, |
53
|
|
|
InputArgument::OPTIONAL | InputArgument::IS_ARRAY, |
54
|
|
|
null, |
55
|
|
|
['php://stdin'] |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
$this->addOption(self::OPTION_NO_VERBOSE); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param InputInterface $input |
63
|
|
|
* @param OutputInterface $output |
64
|
|
|
* @return int |
65
|
|
|
*/ |
66
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
67
|
|
|
{ |
68
|
|
|
$this->paths = $input->getArgument(self::ARGUMENT_PATH); |
69
|
|
|
|
70
|
|
|
if ($input->getOption(self::OPTION_VERBOSE)) { |
71
|
|
|
$this->verbose = true; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$engine = null; |
|
|
|
|
75
|
|
|
$collation = null; |
|
|
|
|
76
|
|
|
|
77
|
|
|
$errorFiles = []; |
78
|
|
|
|
79
|
|
|
foreach ($this->paths as $path) { |
80
|
|
|
$dump = new MysqlDump; |
81
|
|
|
|
82
|
|
|
$files = []; |
83
|
|
|
if (is_dir($path)) { |
84
|
|
|
if ($this->verbose) { |
85
|
|
|
echo "$path\n"; |
86
|
|
|
} |
87
|
|
|
foreach (new GlobIterator("$path/*.sql") as $fileInfo) { |
88
|
|
|
$files[] = $fileInfo->getPathname(); |
89
|
|
|
} |
90
|
|
|
} else { |
91
|
|
|
$files[] = $path; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
foreach ($files as $file) { |
95
|
|
|
$stream = TokenStream::newFromFile($file); |
96
|
|
|
try { |
97
|
|
|
$dump->parse($stream); |
98
|
|
|
if ($this->verbose) { |
99
|
|
|
echo "OK $file\n"; |
100
|
|
|
} |
101
|
|
|
} catch (RuntimeException $e) { |
102
|
|
|
$errorFiles[] = $file; |
103
|
|
|
$message = $stream->contextualise($e->getMessage()); |
104
|
|
|
echo "ERROR $message\n"; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return count($errorFiles); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|