* Jordi Boggiano * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Composer\Command; use Composer\Factory; use Composer\Package\Loader\ValidatingArrayLoader; use Composer\Util\ConfigValidator; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; /** * ValidateCommand * * @author Robert Schönthal * @author Jordi Boggiano */ class ValidateCommand extends Command { /** * configure */ protected function configure() { $this ->setName('validate') ->setDescription('Validates a composer.json and composer.lock') ->setDefinition(array( new InputOption('no-check-all', null, InputOption::VALUE_NONE, 'Do not make a complete validation'), new InputOption('no-check-lock', null, InputOption::VALUE_NONE, 'Do not check if lock file is up to date'), new InputOption('no-check-publish', null, InputOption::VALUE_NONE, 'Do not check for publish errors'), new InputArgument('file', InputArgument::OPTIONAL, 'path to composer.json file', './composer.json') )) ->setHelp(<<getArgument('file'); $io = $this->getIO(); if (!file_exists($file)) { $io->writeError('' . $file . ' not found.'); return 1; } if (!is_readable($file)) { $io->writeError('' . $file . ' is not readable.'); return 1; } $validator = new ConfigValidator($io); $checkAll = $input->getOption('no-check-all') ? 0 : ValidatingArrayLoader::CHECK_ALL; $checkPublish = !$input->getOption('no-check-publish'); list($errors, $publishErrors, $warnings) = $validator->validate($file, $checkAll); $checkLock = !$input->getOption('no-check-lock'); $lockErrors = array(); $composer = Factory::create($io, $file); $locker = $composer->getLocker(); if ($locker->isLocked() && !$locker->isFresh()) { $lockErrors[] = 'The lock file is not up to date with the latest changes in composer.json.'; } // output errors/warnings if (!$errors && !$publishErrors && !$warnings) { $io->write('' . $file . ' is valid'); } elseif (!$errors && !$publishErrors) { $io->writeError('' . $file . ' is valid, but with a few warnings'); $io->writeError('See https://getcomposer.org/doc/04-schema.md for details on the schema'); } elseif (!$errors) { $io->writeError('' . $file . ' is valid for simple usage with composer but has'); $io->writeError('strict errors that make it unable to be published as a package:'); $io->writeError('See https://getcomposer.org/doc/04-schema.md for details on the schema'); } else { $io->writeError('' . $file . ' is invalid, the following errors/warnings were found:'); } $messages = array( 'error' => $errors, 'warning' => $warnings, ); // If checking publish errors, display them as errors, otherwise just show them as warnings if ($checkPublish) { $messages['error'] = array_merge($messages['error'], $publishErrors); } else { $messages['warning'] = array_merge($messages['warning'], $publishErrors); } // If checking lock errors, display them as errors, otherwise just show them as warnings if ($checkLock) { $messages['error'] = array_merge($messages['error'], $lockErrors); } else { $messages['warning'] = array_merge($messages['warning'], $lockErrors); } foreach ($messages as $style => $msgs) { foreach ($msgs as $msg) { $io->writeError('<' . $style . '>' . $msg . ''); } } return $errors || ($publishErrors && $checkPublish) || ($lockErrors && $checkLock) ? 1 : 0; } }