* 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 Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Output\OutputInterface; use Composer\Json\JsonFile; use Composer\Json\JsonValidationException; use Composer\Util\RemoteFilesystem; use Composer\Util\SPDXLicenseIdentifier; /** * ValidateCommand * * @author Robert Schönthal * @author Jordi Boggiano */ class ValidateCommand extends Command { /** * configure */ protected function configure() { $this ->setName('validate') ->setDescription('Validates a composer.json') ->setDefinition(array( new InputArgument('file', InputArgument::OPTIONAL, 'path to composer.json file', './composer.json') )) ->setHelp(<<getArgument('file'); if (!file_exists($file)) { $output->writeln('' . $file . ' not found.'); return 1; } if (!is_readable($file)) { $output->writeln('' . $file . ' is not readable.'); return 1; } $laxValid = false; try { $json = new JsonFile($file, new RemoteFilesystem($this->getIO())); $manifest = $json->read(); $json->validateSchema(JsonFile::LAX_SCHEMA); $laxValid = true; $json->validateSchema(); } catch (JsonValidationException $e) { if ($laxValid) { $output->writeln('' . $file . ' is valid for simple usage with composer but has'); $output->writeln('strict errors that make it unable to be published as a package:'); } else { $output->writeln('' . $file . ' is invalid, the following errors were found:'); } foreach ($e->getErrors() as $message) { $output->writeln('' . $message . ''); } return 1; } catch (\Exception $e) { $output->writeln('' . $file . ' contains a JSON Syntax Error:'); $output->writeln('' . $e->getMessage() . ''); return 1; } if (isset($manifest['license'])) { try { $identifier = new SPDXLicenseIdentifier($manifest['license']); } catch (\InvalidArgumentException $e) { $output->writeln(sprintf( 'License "%s" is not a SPDX license identifier.', print_r($manifest['license'], true) )); } } else { $output->writeln('No license specified.'); } $output->writeln('' . $file . ' is valid'); return 0; } }