|
11 | 11 |
|
12 | 12 | namespace Symfony\Bundle\FrameworkBundle\Command; |
13 | 13 |
|
14 | | -use Symfony\Component\Console\Command\Command; |
15 | | -use Symfony\Component\Console\Input\InputInterface; |
16 | | -use Symfony\Component\Console\Input\InputOption; |
17 | | -use Symfony\Component\Console\Output\OutputInterface; |
18 | | -use Symfony\Component\Console\Style\SymfonyStyle; |
19 | | -use Symfony\Component\Finder\Finder; |
20 | | -use Symfony\Component\Yaml\Exception\ParseException; |
21 | | -use Symfony\Component\Yaml\Parser; |
| 14 | +use Symfony\Component\Yaml\Command\LintCommand as BaseLintCommand; |
22 | 15 |
|
23 | 16 | /** |
24 | 17 | * Validates YAML files syntax and outputs encountered errors. |
25 | 18 | * |
26 | 19 | * @author Grégoire Pineau <lyrixx@lyrixx.info> |
| 20 | + * @author Robin Chalas <robin.chalas@gmail.com> |
27 | 21 | */ |
28 | | -class YamlLintCommand extends Command |
| 22 | +class YamlLintCommand extends BaseLintCommand |
29 | 23 | { |
| 24 | + /** |
| 25 | + * {@inheritdoc} |
| 26 | + */ |
30 | 27 | protected function configure() |
31 | 28 | { |
32 | | - $this |
33 | | - ->setName('lint:yaml') |
34 | | - ->setDescription('Lints a file and outputs encountered errors') |
35 | | - ->addArgument('filename', null, 'A file or a directory or STDIN') |
36 | | - ->addOption('format', null, InputOption::VALUE_REQUIRED, 'The output format', 'txt') |
37 | | - ->setHelp(<<<EOF |
38 | | -The <info>%command.name%</info> command lints a YAML file and outputs to STDOUT |
39 | | -the first encountered syntax error. |
| 29 | + parent::configure(); |
40 | 30 |
|
41 | | -You can validate the syntax of a file: |
| 31 | + $this->setHelp( |
| 32 | + $this->getHelp().<<<EOF |
42 | 33 |
|
43 | | - <info>php %command.full_name% filename</info> |
44 | | -
|
45 | | -Or of a whole directory: |
46 | | -
|
47 | | - <info>php %command.full_name% dirname</info> |
48 | | - <info>php %command.full_name% dirname --format=json</info> |
49 | | -
|
50 | | -Or all YAML files in a bundle: |
| 34 | +Or find all files in a bundle: |
51 | 35 |
|
52 | 36 | <info>php %command.full_name% @AcmeDemoBundle</info> |
53 | 37 |
|
54 | | -You can also pass the YAML contents from STDIN: |
55 | | -
|
56 | | - <info>cat filename | php %command.full_name%</info> |
57 | | -
|
58 | 38 | EOF |
59 | | - ) |
60 | | - ; |
61 | | - } |
62 | | - |
63 | | - protected function execute(InputInterface $input, OutputInterface $output) |
64 | | - { |
65 | | - $io = new SymfonyStyle($input, $output); |
66 | | - $filename = $input->getArgument('filename'); |
67 | | - |
68 | | - if (!$filename) { |
69 | | - if (0 !== ftell(STDIN)) { |
70 | | - throw new \RuntimeException('Please provide a filename or pipe file content to STDIN.'); |
71 | | - } |
72 | | - |
73 | | - $content = ''; |
74 | | - while (!feof(STDIN)) { |
75 | | - $content .= fread(STDIN, 1024); |
76 | | - } |
77 | | - |
78 | | - return $this->display($input, $output, $io, array($this->validate($content))); |
79 | | - } |
80 | | - |
81 | | - if (0 !== strpos($filename, '@') && !is_readable($filename)) { |
82 | | - throw new \RuntimeException(sprintf('File or directory "%s" is not readable', $filename)); |
83 | | - } |
84 | | - |
85 | | - $files = array(); |
86 | | - if (is_file($filename)) { |
87 | | - $files = array($filename); |
88 | | - } elseif (is_dir($filename)) { |
89 | | - $files = Finder::create()->files()->in($filename)->name('*.yml'); |
90 | | - } else { |
91 | | - $dir = $this->getApplication()->getKernel()->locateResource($filename); |
92 | | - $files = Finder::create()->files()->in($dir)->name('*.yml'); |
93 | | - } |
94 | | - |
95 | | - $filesInfo = array(); |
96 | | - foreach ($files as $file) { |
97 | | - $filesInfo[] = $this->validate(file_get_contents($file), $file); |
98 | | - } |
99 | | - |
100 | | - return $this->display($input, $output, $io, $filesInfo); |
| 39 | + ); |
101 | 40 | } |
102 | 41 |
|
103 | | - private function validate($content, $file = null) |
| 42 | + protected function getDirectoryIterator($directory) |
104 | 43 | { |
105 | | - $parser = new Parser(); |
106 | | - try { |
107 | | - $parser->parse($content); |
108 | | - } catch (ParseException $e) { |
109 | | - return array('file' => $file, 'valid' => false, 'message' => $e->getMessage()); |
| 44 | + if (!is_dir($directory)) { |
| 45 | + $directory = $this->getApplication()->getKernel()->locateResource($directory); |
110 | 46 | } |
111 | 47 |
|
112 | | - return array('file' => $file, 'valid' => true); |
| 48 | + return parent::getDirectoryIterator($directory); |
113 | 49 | } |
114 | 50 |
|
115 | | - private function display(InputInterface $input, OutputInterface $output, SymfonyStyle $io, $files) |
| 51 | + protected function isReadable($fileOrDirectory) |
116 | 52 | { |
117 | | - switch ($input->getOption('format')) { |
118 | | - case 'txt': |
119 | | - return $this->displayTxt($output, $io, $files); |
120 | | - case 'json': |
121 | | - return $this->displayJson($io, $files); |
122 | | - default: |
123 | | - throw new \InvalidArgumentException(sprintf('The format "%s" is not supported.', $input->getOption('format'))); |
124 | | - } |
125 | | - } |
126 | | - |
127 | | - private function displayTxt(OutputInterface $output, SymfonyStyle $io, $filesInfo) |
128 | | - { |
129 | | - $errors = 0; |
130 | | - |
131 | | - foreach ($filesInfo as $info) { |
132 | | - if ($info['valid'] && $output->isVerbose()) { |
133 | | - $io->comment('<info>OK</info>'.($info['file'] ? sprintf(' in %s', $info['file']) : '')); |
134 | | - } elseif (!$info['valid']) { |
135 | | - ++$errors; |
136 | | - $io->text(sprintf('<error> ERROR </error> in %s', $info['file'])); |
137 | | - $io->text(sprintf('<error> >> %s</error>', $info['message'])); |
138 | | - } |
139 | | - } |
140 | | - |
141 | | - if ($errors === 0) { |
142 | | - $io->success(sprintf('All %d YAML files contain valid syntax.', count($filesInfo))); |
143 | | - } else { |
144 | | - $io->warning(sprintf('%d YAML files have valid syntax and %d contain errors.', count($filesInfo) - $errors, $errors)); |
145 | | - } |
146 | | - |
147 | | - return min($errors, 1); |
148 | | - } |
149 | | - |
150 | | - private function displayJson(OutputInterface $output, $filesInfo) |
151 | | - { |
152 | | - $errors = 0; |
153 | | - |
154 | | - array_walk($filesInfo, function (&$v) use (&$errors) { |
155 | | - $v['file'] = (string) $v['file']; |
156 | | - if (!$v['valid']) { |
157 | | - ++$errors; |
158 | | - } |
159 | | - }); |
160 | | - |
161 | | - $output->writeln(json_encode($filesInfo, J
8B92
SON_PRETTY_PRINT)); |
162 | | - |
163 | | - return min($errors, 1); |
| 53 | + return 0 === strpos($fileOrDirectory, '@') || parent::isReadable($fileOrDirectory); |
164 | 54 | } |
165 | 55 | } |
0 commit comments