Skip to content

Commit

Permalink
Added --title option + better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
SRWieZ committed Dec 2, 2023
1 parent 115400c commit fd495c8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 34 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Bumped https://github.com/SRWieZ/php-svg-ps-converter to 1.1.1
- Compiled binaries
- New `--title` option
- Better error handling

## [1.0.5] - 2023-11-01
More robust autoload
Expand Down
80 changes: 46 additions & 34 deletions src/svgtinyps.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php
error_reporting(0);

use Composer\InstalledVersions;
use SVGTinyPS\SVGTinyPS;

if (!class_exists('\Composer\InstalledVersions')) {
require __DIR__.'/../vendor/autoload.php';
require __DIR__ . '/../vendor/autoload.php';
}

// Argument parsing
Expand All @@ -25,65 +27,65 @@
$argv = array_values($argv);
$command = $argv[0] ?? null;

function verboseLog($message, $isVerbose)
function verboseLog($message, $isVerbose): void
{
if ($isVerbose) {
echo "[VERBOSE] $message".PHP_EOL;
echo "[VERBOSE] $message" . PHP_EOL;
}
}

function getComposerVersion($package = 'composer/composer'): ?string
{
if (class_exists('\Composer\InstalledVersions')) {
return \Composer\InstalledVersions::getPrettyVersion($package) ?? null;
return InstalledVersions::getPrettyVersion($package) ?? null;
}

return null;
}

// Function to display help information
function showHelp()
function showHelp(): void
{
$version = '@git_tag@';
echo 'Usage: svgtinyps [options] [command] [input-file] [output-file]'.PHP_EOL;
echo 'Usage: svgtinyps [options] [command] [input-file] [output-file]' . PHP_EOL;
echo PHP_EOL;
echo 'Options:'.PHP_EOL;
echo ' -v, --verbose Enable verbose mode'.PHP_EOL;
echo ' --title= Set company name (<title> tag)'.PHP_EOL;
echo 'Options:' . PHP_EOL;
echo ' -v, --verbose Enable verbose mode' . PHP_EOL;
echo ' --title= Set company name (<title> tag)' . PHP_EOL;
echo PHP_EOL;
echo 'Commands:'.PHP_EOL;
echo ' convert [input] [output] - Convert SVG file'.PHP_EOL;
echo ' issues [input] - Check for issues in SVG file'.PHP_EOL;
echo 'Commands:' . PHP_EOL;
echo ' convert [input] [output] - Convert SVG file' . PHP_EOL;
echo ' issues [input] - Check for issues in SVG file' . PHP_EOL;
// echo ' minify [input] - Minify SVG file'.PHP_EOL;
echo ' help - Show this help information'.PHP_EOL;
echo ' help - Show this help information' . PHP_EOL;
echo PHP_EOL;
echo 'Informations:'.PHP_EOL;
echo !str_starts_with($version, '@git_tag') ? ' Version: '.$version.PHP_EOL : '';
echo ' PHP version: '.phpversion().PHP_EOL;
echo 'Informations:' . PHP_EOL;
echo !str_starts_with($version, '@git_tag') ? ' Version: ' . $version . PHP_EOL : '';
echo ' PHP version: ' . phpversion() . PHP_EOL;
// echo 'PHP sapi name: '.php_sapi_name().PHP_EOL;
echo ' Based on https://github.com/srwiez/php-svg-ps-converter ('.getComposerVersion('srwiez/php-svg-ps-converter').')'.PHP_EOL;
echo ' Built with https://github.com/box-project/box'.PHP_EOL;
echo php_sapi_name() == 'micro' ? ' Compiled with https://github.com/crazywhalecc/static-php-cli'.PHP_EOL : '';
echo ' Based on https://github.com/srwiez/php-svg-ps-converter (' . getComposerVersion('srwiez/php-svg-ps-converter') . ')' . PHP_EOL;
echo ' Built with https://github.com/box-project/box' . PHP_EOL;
echo php_sapi_name() == 'micro' ? ' Compiled with https://github.com/crazywhalecc/static-php-cli' . PHP_EOL : '';
}

if (!$command || $command === 'help') {
showHelp();
exit;
}

function checkInputFile($inputFile)
function checkInputFile($inputFile): void
{
if (!$inputFile || !file_exists($inputFile)) {
echo "Error: Input file not provided or doesn't exist.".PHP_EOL;
echo "Error: Input file not provided or doesn't exist." . PHP_EOL;
exit(1);
}
}

function checkOutputFile($outputFile)
function checkOutputFile($outputFile): void
{
$outputDir = $outputFile ? dirname($outputFile) : null;
if (empty($outputFile)) {
echo 'Error: Output file not provided for conversion.'.PHP_EOL;
echo 'Error: Output file not provided for conversion.' . PHP_EOL;
exit(1);
}

Expand Down Expand Up @@ -119,33 +121,43 @@ function checkOutputFile($outputFile)
// minifySvg($inputFile, $isVerbose);
// break;
default:
echo 'Invalid command!'.PHP_EOL;
echo 'Invalid command!' . PHP_EOL;
echo PHP_EOL;
showHelp();
exit(1);
}

function convertSvg($input, $output, $title, $isVerbose)
function convertSvg($input, $output, $title, $isVerbose): void
{
verboseLog("Converting $input to $output", $isVerbose);

$svgps = new SVGTinyPS(file_get_contents($input));
try {
$svgps = new SVGTinyPS(file_get_contents($input));

if ($title) {
$svgps->setTitle($title);
if ($title) {
$svgps->setTitle($title);
}
$new_svg = $svgps->convert();
} catch (Exception $exception) {
echo "Error: " . $exception->getMessage() . PHP_EOL;
exit(1);
}
$new_svg = $svgps->convert();
file_put_contents($output, $new_svg);
}

function checkIssues($input, $isVerbose)
function checkIssues($input, $isVerbose): void
{
verboseLog("Checking $input for issues", $isVerbose);

$svgps = new SVGTinyPS(file_get_contents($input));
$issues = $svgps->identifyIssues();
foreach ($issues as $issue) {
echo "$issue".PHP_EOL;
try {
$svgps = new SVGTinyPS(file_get_contents($input));
$issues = $svgps->identifyIssues();
foreach ($issues as $issue) {
echo "$issue" . PHP_EOL;
}
} catch (Exception $exception) {
echo "Error: " . $exception->getMessage() . PHP_EOL;
exit(1);
}
}

Expand Down

0 comments on commit fd495c8

Please sign in to comment.