Skip to content

Commit

Permalink
Misc updates
Browse files Browse the repository at this point in the history
* Switched to using Composer\InstalledVersions for yaml component version (fixes #47)
* Code tidy (removed redundant IDE hints, breaks, namespace qualifiers)
* Clarified comments
  • Loading branch information
j13k committed Mar 22, 2022
1 parent 8776d9b commit 9e75d5d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Install as a project component with Composer (executable from the project's
composer require j13k/yaml-lint
```

Typically a binary edition (`yaml-lint.phar`) is also available for download
Typically, a binary edition (`yaml-lint.phar`) is also available for download
with [each release](https://github.com/j13k/yaml-lint/releases). This embeds
the latest stable version of the Symfony Yaml component that is current at
the time of the release.
Expand Down Expand Up @@ -62,4 +62,3 @@ The MIT License (MIT). Please see [LICENCE](LICENSE) for more information.
[link-downloads]: https://packagist.org/packages/j13k/yaml-lint/stats
[link-dependencies]: https://www.versioneye.com/user/projects/58324238eaa74b004633a7c1
[link-author]: https://github.com/j13k
[link-contributors]: ../../contributors
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
}
],
"require": {
"composer-runtime-api": "^2",
"symfony/yaml": "^2|^3|^4|^5|^6"
},
"autoload": {
Expand Down
6 changes: 4 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 16 additions & 16 deletions src/yaml-lint.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* file that was distributed with this source code.
*/

use Composer\InstalledVersions;
use J13k\YamlLint\UsageException;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Yaml;
Expand All @@ -28,7 +29,7 @@
define('YAML_PARSE_PARAM_NAME_FLAGS', 'flags');

// Init app name and args
$appStr = APP_NAME . ' ' . APP_VERSION;
$appStr = APP_NAME;
$argQuiet = false;
$argPaths = [];

Expand All @@ -38,23 +39,24 @@
$pathToTry = null;
foreach (array('/../../../', '/../vendor/') as $pathToTry) {
if (is_readable(__DIR__ . $pathToTry . 'autoload.php')) {
/** @noinspection PhpIncludeInspection */
require __DIR__ . $pathToTry . 'autoload.php';
break;
}
}
if (!class_exists('\Composer\Autoload\ClassLoader')) {
throw new \Exception(_msg('composer'));
throw new Exception(_msg('composer'));
}

// Extract YAML component metadata
$componentsManifest = __DIR__ . $pathToTry . 'composer/installed.json';
$components = json_decode(file_get_contents($componentsManifest), true);
foreach ($components as $component) {
if (isset($component['name']) && $component['name'] == 'symfony/yaml') {
$appStr .= ', symfony/yaml ' . $component['version'];
break;
// Build app version string
if (class_exists('\Composer\InstalledVersions')) {
if (InstalledVersions::isInstalled('j13k/yaml-lint')) {
$appStr .= ' ' . InstalledVersions::getPrettyVersion('j13k/yaml-lint');
}
if (InstalledVersions::isInstalled('symfony/yaml')) {
$appStr .= ', symfony/yaml ' . InstalledVersions::getPrettyVersion('symfony/yaml');
}
} else {
$appStr .= ' ' . APP_VERSION;
}

// Process and check args
Expand All @@ -78,7 +80,7 @@
}
}

// Currently only one input file or STDIN supported
// Currently, only one input file or STDIN supported
if (count($argPaths) < 1) {
throw new UsageException('no input specified', EXIT_ERROR);
}
Expand All @@ -94,11 +96,11 @@
$yamlParseParams = $yamlParseMethod->getParameters();
switch ($yamlParseParams[1]->name) {
case YAML_PARSE_PARAM_NAME_EXCEPTION_ON_INVALID_TYPE:
// Maintains original behaviour in ^2
// Maintains original behaviour in v2
Yaml::parse($content, true);
break;
case YAML_PARSE_PARAM_NAME_FLAGS:
// Implements same behaviour in ^3 and ^4
// Implements same behaviour in v3+
Yaml::parse($content, Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE);
break;
default:
Expand Down Expand Up @@ -156,7 +158,7 @@
fwrite(STDERR, "\n" . $e->getMessage() . "\n\n");
exit(EXIT_ERROR);

} catch (\Exception $e) {
} catch (Exception $e) {

// The rest
fwrite(STDERR, $appStr);
Expand Down Expand Up @@ -196,7 +198,6 @@ function _msg($str)
Composer dependencies cannot be loaded; install Composer to remedy:
https://getcomposer.org/download/
EOD;
break;
case 'usage':
return <<<EOD
usage: yaml-lint [options] [input source]
Expand All @@ -207,7 +208,6 @@ function _msg($str)
-h, --help Display this help
-V, --version Display application version
EOD;
break;
default:
}

Expand Down

0 comments on commit 9e75d5d

Please sign in to comment.