From 336e44a963d8af81734c791bd4fd2e15ef3229c0 Mon Sep 17 00:00:00 2001 From: Laurent Laville Date: Thu, 1 Aug 2024 06:03:07 +0000 Subject: [PATCH] upgrade examples --- examples/README.md | 2 +- examples/phar-manifest.php | 39 ++++++++++++++++++++++++++------------ 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/examples/README.md b/examples/README.md index ac4ccc1..5af8855 100644 --- a/examples/README.md +++ b/examples/README.md @@ -4,4 +4,4 @@ Table Of Contents : 1. A `phar-manifest.php` script that demonstrate how to use programmatically the API to generate manifests in different format. -1. A basic `app-fixtures` application that will be support of a tutorial. +2. A basic `app-fixtures` application that will be support of a tutorial. diff --git a/examples/phar-manifest.php b/examples/phar-manifest.php index 96094be..9a81434 100644 --- a/examples/phar-manifest.php +++ b/examples/phar-manifest.php @@ -34,6 +34,16 @@ public function log($level, string|Stringable $message, array $context = []): vo } }; +if (count($argv) < 2) { + // Print Usage. + $logger->log('notice', 'If you want to build a manifest in specific format, please use one of these syntaxes'); + $logger->log('notice', sprintf('Usage: php %s %s :: %s', __FILE__, 'sbom', 'SBOM XML format')); + $logger->log('notice', sprintf('Usage: php %s %s :: %s', __FILE__, 'plain', 'Plain text (key: value) format')); + $logger->log('notice', sprintf('Usage: php %s %s :: %s', __FILE__, 'ansi', 'Console Line format')); + $logger->log('notice', sprintf('Usage: php %s %s :: %s', __FILE__, 'console', 'Console Table format')); + exit(1); +} + (new PhpSettingsHandler($logger))->check(); $configLoader = new ConfigurationLoader(); @@ -43,26 +53,31 @@ public function log($level, string|Stringable $message, array $context = []): vo $factory = new ManifestFactory($config, true, (new BoxHelper())->getBoxVersion(), (new Application())->getVersion()); try { - // 1. - $format = 'xml'; // Allowed values are: xml, json - $specVersion = '1.4'; // Allowed values are: 1.1, 1.2, 1.3, 1.4 - $result = $factory->toSbom($format, $specVersion); - - // 2. - //$result = $factory->toText(); - - // 3. - //$result = $factory->toHighlight(); + if ($argv[1] == 'sbom') { + // 1. + $format = 'xml'; // Allowed values are: xml, json + $specVersion = '1.4'; // Allowed values are: 1.1, 1.2, 1.3, 1.4, 1.5, 1.6 + $result = $factory->toSbom($format, $specVersion); + } elseif ($argv[1] == 'plain') { + // 2. + $result = $factory->toText(); + } elseif ($argv[1] == 'ansi') { + // 3. + $result = $factory->toHighlight(); + } elseif ($argv[1] == 'console') { + // 4. + $result = $factory->toConsole(); + } } catch (Throwable $e) { echo $e->getMessage(), PHP_EOL; } finally { $logger->log( 'info', - 'Using composer.json : ' . $config->getComposerJson() + 'Using composer.json : ' . $config->getComposerJson()?->path ); $logger->log( 'info', - 'Using composer.lock : ' . $config->getComposerLock() + 'Using composer.lock : ' . $config->getComposerLock()?->path ); echo $result, PHP_EOL; }