Пример #1
0
 /**
  * Validates the config, and returns the result.
  *
  * @param string $file The path to the file
  *
  * @return array a triple containing the errors, publishable errors, and warnings
  */
 public function validate($file)
 {
     $errors = array();
     $publishErrors = array();
     $warnings = array();
     // validate json schema
     $laxValid = false;
     $valid = false;
     try {
         $json = new JsonFile($file, new RemoteFilesystem($this->io));
         $manifest = $json->read();
         $json->validateSchema(JsonFile::LAX_SCHEMA);
         $laxValid = true;
         $json->validateSchema();
         $valid = true;
     } catch (JsonValidationException $e) {
         foreach ($e->getErrors() as $message) {
             if ($laxValid) {
                 $publishErrors[] = $message;
             } else {
                 $errors[] = $message;
             }
         }
     } catch (\Exception $e) {
         $errors[] = $e->getMessage();
         return array($errors, $publishErrors, $warnings);
     }
     // validate actual data
     if (!empty($manifest['license'])) {
         $licenseValidator = new SpdxLicenseIdentifier();
         if (!$licenseValidator->validate($manifest['license'])) {
             $warnings[] = sprintf('License %s is not a valid SPDX license identifier, see http://www.spdx.org/licenses/ if you use an open license', json_encode($manifest['license']));
         }
     } else {
         $warnings[] = 'No license specified, it is recommended to do so';
     }
     if (!empty($manifest['name']) && preg_match('{[A-Z]}', $manifest['name'])) {
         $suggestName = preg_replace('{(?:([a-z])([A-Z])|([A-Z])([A-Z][a-z]))}', '\\1\\3-\\2\\4', $manifest['name']);
         $suggestName = strtolower($suggestName);
         $warnings[] = sprintf('Name "%s" does not match the best practice (e.g. lower-cased/with-dashes). We suggest using "%s" instead. As such you will not be able to submit it to Packagist.', $manifest['name'], $suggestName);
     }
     try {
         $loader = new ValidatingArrayLoader(new ArrayLoader());
         if (!isset($manifest['version'])) {
             $manifest['version'] = '1.0.0';
         }
         if (!isset($manifest['name'])) {
             $manifest['name'] = 'dummy/dummy';
         }
         $loader->load($manifest);
     } catch (InvalidPackageException $e) {
         $errors = array_merge($errors, $e->getErrors());
     }
     $warnings = array_merge($warnings, $loader->getWarnings());
     return array($errors, $publishErrors, $warnings);
 }
Пример #2
0
 /**
  * Validates the config, and returns the result.
  *
  * @param string $file The path to the file
  *
  * @return array a triple containing the errors, publishable errors, and warnings
  */
 public function validate($file)
 {
     $errors = array();
     $publishErrors = array();
     $warnings = array();
     // validate json schema
     $laxValid = false;
     try {
         $json = new JsonFile($file, new RemoteFilesystem($this->io));
         $manifest = $json->read();
         $json->validateSchema(JsonFile::LAX_SCHEMA);
         $laxValid = true;
         $json->validateSchema();
     } catch (JsonValidationException $e) {
         foreach ($e->getErrors() as $message) {
             if ($laxValid) {
                 $publishErrors[] = $message;
             } else {
                 $errors[] = $message;
             }
         }
     } catch (\Exception $e) {
         $errors[] = $e->getMessage();
         return array($errors, $publishErrors, $warnings);
     }
     // validate actual data
     if (!empty($manifest['license'])) {
         // strip proprietary since it's not a valid SPDX identifier, but is accepted by composer
         if (is_array($manifest['license'])) {
             foreach ($manifest['license'] as $key => $license) {
                 if ('proprietary' === $license) {
                     unset($manifest['license'][$key]);
                 }
             }
         }
         $licenseValidator = new SpdxLicenseIdentifier();
         if ('proprietary' !== $manifest['license'] && array() !== $manifest['license'] && !$licenseValidator->validate($manifest['license'])) {
             $warnings[] = sprintf('License %s is not a valid SPDX license identifier, see http://www.spdx.org/licenses/ if you use an open license.' . "\nIf the software is closed-source, you may use \"proprietary\" as license.", json_encode($manifest['license']));
         }
     } else {
         $warnings[] = 'No license specified, it is recommended to do so. For closed-source software you may use "proprietary" as license.';
     }
     if (!empty($manifest['name']) && preg_match('{[A-Z]}', $manifest['name'])) {
         $suggestName = preg_replace('{(?:([a-z])([A-Z])|([A-Z])([A-Z][a-z]))}', '\\1\\3-\\2\\4', $manifest['name']);
         $suggestName = strtolower($suggestName);
         $warnings[] = sprintf('Name "%s" does not match the best practice (e.g. lower-cased/with-dashes). We suggest using "%s" instead. As such you will not be able to submit it to Packagist.', $manifest['name'], $suggestName);
     }
     if (!empty($manifest['type']) && $manifest['type'] == 'composer-installer') {
         $warnings[] = "The package type 'composer-installer' is deprecated. Please distribute your custom installers as plugins from now on. See http://getcomposer.org/doc/articles/plugins.md for plugin documentation.";
     }
     // check for require-dev overrides
     if (isset($manifest['require']) && isset($manifest['require-dev'])) {
         $requireOverrides = array_intersect_key($manifest['require'], $manifest['require-dev']);
         if (!empty($requireOverrides)) {
             $plural = count($requireOverrides) > 1 ? 'are' : 'is';
             $warnings[] = implode(', ', array_keys($requireOverrides)) . " {$plural} required both in require and require-dev, this can lead to unexpected behavior";
         }
     }
     try {
         $loader = new ValidatingArrayLoader(new ArrayLoader());
         if (!isset($manifest['version'])) {
             $manifest['version'] = '1.0.0';
         }
         if (!isset($manifest['name'])) {
             $manifest['name'] = 'dummy/dummy';
         }
         $loader->load($manifest);
     } catch (InvalidPackageException $e) {
         $errors = array_merge($errors, $e->getErrors());
     }
     $warnings = array_merge($warnings, $loader->getWarnings());
     return array($errors, $publishErrors, $warnings);
 }
 /**
  * @dataProvider provideInvalidArgument
  * @expectedException InvalidArgumentException
  */
 public function testInvalidArgument($invalidArgument)
 {
     $validator = new SpdxLicenseIdentifier();
     $validator->validate($invalidArgument);
 }
Пример #4
0
 /**
  * @param InputInterface  $input
  * @param OutputInterface $output
  *
  * @return int
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $file = $input->getArgument('file');
     if (!file_exists($file)) {
         $output->writeln('<error>' . $file . ' not found.</error>');
         return 1;
     }
     if (!is_readable($file)) {
         $output->writeln('<error>' . $file . ' is not readable.</error>');
         return 1;
     }
     $errors = array();
     $publishErrors = array();
     $warnings = array();
     // validate json schema
     $laxValid = false;
     $valid = false;
     try {
         $json = new JsonFile($file, new RemoteFilesystem($this->getIO()));
         $manifest = $json->read();
         $json->validateSchema(JsonFile::LAX_SCHEMA);
         $laxValid = true;
         $json->validateSchema();
         $valid = true;
     } catch (JsonValidationException $e) {
         foreach ($e->getErrors() as $message) {
             if ($laxValid) {
                 $publishErrors[] = '<error>Publish Error: ' . $message . '</error>';
             } else {
                 $errors[] = '<error>' . $message . '</error>';
             }
         }
     } catch (\Exception $e) {
         $output->writeln('<error>' . $file . ' contains a JSON Syntax Error:</error>');
         $output->writeln('<error>' . $e->getMessage() . '</error>');
         return 1;
     }
     // validate actual data
     if (!empty($manifest['license'])) {
         $licenseValidator = new SpdxLicenseIdentifier();
         if (!$licenseValidator->validate($manifest['license'])) {
             $warnings[] = sprintf('License %s is not a valid SPDX license identifier, see http://www.spdx.org/licenses/ if you use an open license', json_encode($manifest['license']));
         }
     } else {
         $warnings[] = 'No license specified, it is recommended to do so';
     }
     // output errors/warnings
     if (!$errors && !$publishErrors && !$warnings) {
         $output->writeln('<info>' . $file . ' is valid</info>');
     } elseif (!$errors && !$publishErrors) {
         $output->writeln('<info>' . $file . ' is valid, but with a few warnings</info>');
         $output->writeln('<warning>See http://getcomposer.org/doc/04-schema.md for details on the schema</warning>');
     } elseif (!$errors) {
         $output->writeln('<info>' . $file . ' is valid for simple usage with composer but has</info>');
         $output->writeln('<info>strict errors that make it unable to be published as a package:</info>');
         $output->writeln('<warning>See http://getcomposer.org/doc/04-schema.md for details on the schema</warning>');
     } else {
         $output->writeln('<error>' . $file . ' is invalid, the following errors/warnings were found:</error>');
     }
     $messages = array('error' => array_merge($errors, $publishErrors), 'warning' => $warnings);
     foreach ($messages as $style => $msgs) {
         foreach ($msgs as $msg) {
             $output->writeln('<' . $style . '>' . $msg . '</' . $style . '>');
         }
     }
     return $errors || $publishErrors ? 1 : 0;
 }
Пример #5
0
 /**
  * @param InputInterface  $input
  * @param OutputInterface $output
  *
  * @return int
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $file = $input->getArgument('file');
     if (!file_exists($file)) {
         $output->writeln('<error>' . $file . ' not found.</error>');
         return 1;
     }
     if (!is_readable($file)) {
         $output->writeln('<error>' . $file . ' is not readable.</error>');
         return 1;
     }
     $errors = array();
     $publishErrors = array();
     $warnings = array();
     // validate json schema
     $laxValid = false;
     $valid = false;
     try {
         $json = new JsonFile($file, new RemoteFilesystem($this->getIO()));
         $manifest = $json->read();
         $json->validateSchema(JsonFile::LAX_SCHEMA);
         $laxValid = true;
         $json->validateSchema();
         $valid = true;
     } catch (JsonValidationException $e) {
         foreach ($e->getErrors() as $message) {
             if ($laxValid) {
                 $publishErrors[] = '<error>Publish Error: ' . $message . '</error>';
             } else {
                 $errors[] = '<error>' . $message . '</error>';
             }
         }
     } catch (\Exception $e) {
         $output->writeln('<error>' . $e->getMessage() . '</error>');
         return 1;
     }
     // validate actual data
     if (!empty($manifest['license'])) {
         $licenseValidator = new SpdxLicenseIdentifier();
         if (!$licenseValidator->validate($manifest['license'])) {
             $warnings[] = sprintf('License %s is not a valid SPDX license identifier, see http://www.spdx.org/licenses/ if you use an open license', json_encode($manifest['license']));
         }
     } else {
         $warnings[] = 'No license specified, it is recommended to do so';
     }
     if (!empty($manifest['name']) && preg_match('{[A-Z]}', $manifest['name'])) {
         $suggestName = preg_replace('{(?:([a-z])([A-Z])|([A-Z])([A-Z][a-z]))}', '\\1\\3-\\2\\4', $manifest['name']);
         $suggestName = strtolower($suggestName);
         $warnings[] = sprintf('Name "%s" does not match the best practice (e.g. lower-cased/with-dashes). We suggest using "%s" instead. As such you will not be able to submit it to Packagist.', $manifest['name'], $suggestName);
     }
     // TODO validate package repositories' packages using the same technique as below
     try {
         $loader = new ValidatingArrayLoader(new ArrayLoader(), false);
         if (!isset($manifest['version'])) {
             $manifest['version'] = '1.0.0';
         }
         if (!isset($manifest['name'])) {
             $manifest['name'] = 'dummy/dummy';
         }
         $loader->load($manifest);
     } catch (\Exception $e) {
         $errors = array_merge($errors, explode("\n", $e->getMessage()));
     }
     // output errors/warnings
     if (!$errors && !$publishErrors && !$warnings) {
         $output->writeln('<info>' . $file . ' is valid</info>');
     } elseif (!$errors && !$publishErrors) {
         $output->writeln('<info>' . $file . ' is valid, but with a few warnings</info>');
         $output->writeln('<warning>See http://getcomposer.org/doc/04-schema.md for details on the schema</warning>');
     } elseif (!$errors) {
         $output->writeln('<info>' . $file . ' is valid for simple usage with composer but has</info>');
         $output->writeln('<info>strict errors that make it unable to be published as a package:</info>');
         $output->writeln('<warning>See http://getcomposer.org/doc/04-schema.md for details on the schema</warning>');
     } else {
         $output->writeln('<error>' . $file . ' is invalid, the following errors/warnings were found:</error>');
     }
     $messages = array('error' => array_merge($errors, $publishErrors), 'warning' => $warnings);
     foreach ($messages as $style => $msgs) {
         foreach ($msgs as $msg) {
             $output->writeln('<' . $style . '>' . $msg . '</' . $style . '>');
         }
     }
     return $errors || $publishErrors ? 1 : 0;
 }