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->setDescription("Check database schema files for correctness"); |
||
52 | |||
53 | $this->addArgument( |
||
54 | self::ARGUMENT_PATH, |
||
55 | InputArgument::OPTIONAL | InputArgument::IS_ARRAY, |
||
56 | '', |
||
57 | ['php://stdin'] |
||
58 | ); |
||
59 | |||
60 | $this->addOption(self::OPTION_NO_VERBOSE); |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * @param InputInterface $input |
||
65 | * @param OutputInterface $output |
||
66 | * @return int |
||
67 | */ |
||
68 | protected function execute(InputInterface $input, OutputInterface $output) |
||
69 | { |
||
70 | $this->paths = $input->getArgument(self::ARGUMENT_PATH); |
||
71 | |||
72 | if ($input->getOption(self::OPTION_VERBOSE)) { |
||
73 | $this->verbose = true; |
||
74 | } |
||
75 | |||
76 | $engine = null; |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
77 | $collation = null; |
||
0 ignored issues
–
show
|
|||
78 | |||
79 | $errorFiles = []; |
||
80 | |||
81 | foreach ($this->paths as $path) { |
||
82 | $dump = new MysqlDump; |
||
83 | |||
84 | $files = []; |
||
85 | if (is_dir($path)) { |
||
86 | if ($this->verbose) { |
||
87 | $output->writeln("$path"); |
||
88 | } |
||
89 | foreach (new GlobIterator("$path/*.sql") as $fileInfo) { |
||
90 | $files[] = $fileInfo->getPathname(); |
||
91 | } |
||
92 | } else { |
||
93 | $files[] = $path; |
||
94 | } |
||
95 | |||
96 | foreach ($files as $file) { |
||
97 | $stream = TokenStream::newFromFile($file); |
||
98 | try { |
||
99 | $dump->parse($stream); |
||
100 | if ($this->verbose) { |
||
101 | $output->writeln("OK $file"); |
||
102 | } |
||
103 | } catch (RuntimeException $e) { |
||
104 | $errorFiles[] = $file; |
||
105 | $message = $stream->contextualise($e->getMessage()); |
||
106 | $output->writeln("<error>ERROR $message</error>"); |
||
107 | } |
||
108 | } |
||
109 | } |
||
110 | |||
111 | return count($errorFiles); |
||
112 | } |
||
113 | } |
||
114 |