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\Translation\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;
21 * Validates XLIFF files syntax and outputs encountered errors.
23 * @author Grégoire Pineau <lyrixx@lyrixx.info>
24 * @author Robin Chalas <robin.chalas@gmail.com>
25 * @author Javier Eguiluz <javier.eguiluz@gmail.com>
27 class XliffLintCommand extends Command
29 protected static $defaultName = 'lint:xliff';
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 ->setDescription('Lints a XLIFF file and outputs encountered errors')
51 ->addArgument('filename', null, 'A file or a directory or STDIN')
52 ->addOption('format', null, InputOption::VALUE_REQUIRED, 'The output format', 'txt')
54 The <info>%command.name%</info> command lints a XLIFF file and outputs to STDOUT
55 the first encountered syntax error.
57 You can validates XLIFF contents passed from STDIN:
59 <info>cat filename | php %command.full_name%</info>
61 You can also validate the syntax of a file:
63 <info>php %command.full_name% filename</info>
65 Or of a whole directory:
67 <info>php %command.full_name% dirname</info>
68 <info>php %command.full_name% dirname --format=json</info>
75 protected function execute(InputInterface $input, OutputInterface $output)
77 $io = new SymfonyStyle($input, $output);
78 $filename = $input->getArgument('filename');
79 $this->format = $input->getOption('format');
80 $this->displayCorrectFiles = $output->isVerbose();
83 if (!$stdin = $this->getStdin()) {
84 throw new \RuntimeException('Please provide a filename or pipe file content to STDIN.');
87 return $this->display($io, array($this->validate($stdin)));
90 if (!$this->isReadable($filename)) {
91 throw new \RuntimeException(sprintf('File or directory "%s" is not readable.', $filename));
95 foreach ($this->getFiles($filename) as $file) {
96 $filesInfo[] = $this->validate(file_get_contents($file), $file);
99 return $this->display($io, $filesInfo);
102 private function validate($content, $file = null)
104 // Avoid: Warning DOMDocument::loadXML(): Empty string supplied as input
105 if ('' === trim($content)) {
106 return array('file' => $file, 'valid' => true);
109 libxml_use_internal_errors(true);
111 $document = new \DOMDocument();
112 $document->loadXML($content);
113 if ($document->schemaValidate(__DIR__.'/../Resources/schemas/xliff-core-1.2-strict.xsd')) {
114 return array('file' => $file, 'valid' => true);
117 $errorMessages = array_map(function ($error) {
119 'line' => $error->line,
120 'column' => $error->column,
121 'message' => trim($error->message),
123 }, libxml_get_errors());
125 libxml_clear_errors();
126 libxml_use_internal_errors(false);
128 return array('file' => $file, 'valid' => false, 'messages' => $errorMessages);
131 private function display(SymfonyStyle $io, array $files)
133 switch ($this->format) {
135 return $this->displayTxt($io, $files);
137 return $this->displayJson($io, $files);
139 throw new \InvalidArgumentException(sprintf('The format "%s" is not supported.', $this->format));
143 private function displayTxt(SymfonyStyle $io, array $filesInfo)
145 $countFiles = count($filesInfo);
148 foreach ($filesInfo as $info) {
149 if ($info['valid'] && $this->displayCorrectFiles) {
150 $io->comment('<info>OK</info>'.($info['file'] ? sprintf(' in %s', $info['file']) : ''));
151 } elseif (!$info['valid']) {
153 $io->text('<error> ERROR </error>'.($info['file'] ? sprintf(' in %s', $info['file']) : ''));
154 $io->listing(array_map(function ($error) {
155 // general document errors have a '-1' line number
156 return -1 === $error['line'] ? $error['message'] : sprintf('Line %d, Column %d: %s', $error['line'], $error['column'], $error['message']);
157 }, $info['messages']));
161 if (0 === $erroredFiles) {
162 $io->success(sprintf('All %d XLIFF files contain valid syntax.', $countFiles));
164 $io->warning(sprintf('%d XLIFF files have valid syntax and %d contain errors.', $countFiles - $erroredFiles, $erroredFiles));
167 return min($erroredFiles, 1);
170 private function displayJson(SymfonyStyle $io, array $filesInfo)
174 array_walk($filesInfo, function (&$v) use (&$errors) {
175 $v['file'] = (string) $v['file'];
181 $io->writeln(json_encode($filesInfo, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
183 return min($errors, 1);
186 private function getFiles($fileOrDirectory)
188 if (is_file($fileOrDirectory)) {
189 yield new \SplFileInfo($fileOrDirectory);
194 foreach ($this->getDirectoryIterator($fileOrDirectory) as $file) {
195 if (!in_array($file->getExtension(), array('xlf', 'xliff'))) {
203 private function getStdin()
205 if (0 !== ftell(STDIN)) {
210 while (!feof(STDIN)) {
211 $inputs .= fread(STDIN, 1024);
217 private function getDirectoryIterator($directory)
219 $default = function ($directory) {
220 return new \RecursiveIteratorIterator(
221 new \RecursiveDirectoryIterator($directory, \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS),
222 \RecursiveIteratorIterator::LEAVES_ONLY
226 if (null !== $this->directoryIteratorProvider) {
227 return call_user_func($this->directoryIteratorProvider, $directory, $default);
230 return $default($directory);
233 private function isReadable($fileOrDirectory)
235 $default = function ($fileOrDirectory) {
236 return is_readable($fileOrDirectory);
239 if (null !== $this->isReadableProvider) {
240 return call_user_func($this->isReadableProvider, $fileOrDirectory, $default);
243 return $default($fileOrDirectory);