4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Yaml\Command;
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\Yaml\Exception\ParseException;
20 use Symfony\Component\Yaml\Parser;
23 * Validates YAML files syntax and outputs encountered errors.
25 * @author Grégoire Pineau <lyrixx@lyrixx.info>
26 * @author Robin Chalas <robin.chalas@gmail.com>
28 class LintCommand extends Command
32 private $displayCorrectFiles;
33 private $directoryIteratorProvider;
34 private $isReadableProvider;
36 public function __construct($name = null, $directoryIteratorProvider = null, $isReadableProvider = null)
38 parent::__construct($name);
40 $this->directoryIteratorProvider = $directoryIteratorProvider;
41 $this->isReadableProvider = $isReadableProvider;
47 protected function configure()
50 ->setName('lint:yaml')
51 ->setDescription('Lints a file and outputs encountered errors')
52 ->addArgument('filename', null, 'A file or a directory or STDIN')
53 ->addOption('format', null, InputOption::VALUE_REQUIRED, 'The output format', 'txt')
55 The <info>%command.name%</info> command lints a YAML file and outputs to STDOUT
56 the first encountered syntax error.
58 You can validates YAML contents passed from STDIN:
60 <info>cat filename | php %command.full_name%</info>
62 You can also validate the syntax of a file:
64 <info>php %command.full_name% filename</info>
66 Or of a whole directory:
68 <info>php %command.full_name% dirname</info>
69 <info>php %command.full_name% dirname --format=json</info>
76 protected function execute(InputInterface $input, OutputInterface $output)
78 $io = new SymfonyStyle($input, $output);
79 $filename = $input->getArgument('filename');
80 $this->format = $input->getOption('format');
81 $this->displayCorrectFiles = $output->isVerbose();
84 if (!$stdin = $this->getStdin()) {
85 throw new \RuntimeException('Please provide a filename or pipe file content to STDIN.');
88 return $this->display($io, array($this->validate($stdin)));
91 if (!$this->isReadable($filename)) {
92 throw new \RuntimeException(sprintf('File or directory "%s" is not readable.', $filename));
96 foreach ($this->getFiles($filename) as $file) {
97 $filesInfo[] = $this->validate(file_get_contents($file), $file);
100 return $this->display($io, $filesInfo);
103 private function validate($content, $file = null)
106 $this->getParser()->parse($content);
107 } catch (ParseException $e) {
108 return array('file' => $file, 'valid' => false, 'message' => $e->getMessage());
111 return array('file' => $file, 'valid' => true);
114 private function display(SymfonyStyle $io, array $files)
116 switch ($this->format) {
118 return $this->displayTxt($io, $files);
120 return $this->displayJson($io, $files);
122 throw new \InvalidArgumentException(sprintf('The format "%s" is not supported.', $this->format));
126 private function displayTxt(SymfonyStyle $io, array $filesInfo)
128 $countFiles = count($filesInfo);
131 foreach ($filesInfo as $info) {
132 if ($info['valid'] && $this->displayCorrectFiles) {
133 $io->comment('<info>OK</info>'.($info['file'] ? sprintf(' in %s', $info['file']) : ''));
134 } elseif (!$info['valid']) {
136 $io->text('<error> ERROR </error>'.($info['file'] ? sprintf(' in %s', $info['file']) : ''));
137 $io->text(sprintf('<error> >> %s</error>', $info['message']));
141 if ($erroredFiles === 0) {
142 $io->success(sprintf('All %d YAML files contain valid syntax.', $countFiles));
144 $io->warning(sprintf('%d YAML files have valid syntax and %d contain errors.', $countFiles - $erroredFiles, $erroredFiles));
147 return min($erroredFiles, 1);
150 private function displayJson(SymfonyStyle $io, array $filesInfo)
154 array_walk($filesInfo, function (&$v) use (&$errors) {
155 $v['file'] = (string) $v['file'];
161 $io->writeln(json_encode($filesInfo, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
163 return min($errors, 1);
166 private function getFiles($fileOrDirectory)
168 if (is_file($fileOrDirectory)) {
169 yield new \SplFileInfo($fileOrDirectory);
174 foreach ($this->getDirectoryIterator($fileOrDirectory) as $file) {
175 if (!in_array($file->getExtension(), array('yml', 'yaml'))) {
183 private function getStdin()
185 if (0 !== ftell(STDIN)) {
190 while (!feof(STDIN)) {
191 $inputs .= fread(STDIN, 1024);
197 private function getParser()
199 if (!$this->parser) {
200 $this->parser = new Parser();
203 return $this->parser;
206 private function getDirectoryIterator($directory)
208 $default = function ($directory) {
209 return new \RecursiveIteratorIterator(
210 new \RecursiveDirectoryIterator($directory, \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS),
211 \RecursiveIteratorIterator::LEAVES_ONLY
215 if (null !== $this->directoryIteratorProvider) {
216 return call_user_func($this->directoryIteratorProvider, $directory, $default);
219 return $default($directory);
222 private function isReadable($fileOrDirectory)
224 $default = function ($fileOrDirectory) {
225 return is_readable($fileOrDirectory);
228 if (null !== $this->isReadableProvider) {
229 return call_user_func($this->isReadableProvider, $fileOrDirectory, $default);
232 return $default($fileOrDirectory);