Skip to content

Commit

Permalink
Merge pull request #35 from php-gettext/bin-shebang
Browse files Browse the repository at this point in the history
Fix export-plural-rules file
  • Loading branch information
mlocati authored Jul 13, 2021
2 parents 8fb5879 + fd3830c commit 5473976
Showing 1 changed file with 51 additions and 50 deletions.
101 changes: 51 additions & 50 deletions bin/export-plural-rules
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env php
<?php

use Gettext\Languages\Exporter\Exporter;
Expand All @@ -7,55 +8,12 @@ use Gettext\Languages\Language;
// This is a really life-saving approach.
error_reporting(E_ALL);
set_error_handler(function ($errno, $errstr, $errfile, $errline) {
fprintf(STDERR, "{$errstr}\nFile: {$errfile}\nLine: {$errline}\nCode: {$errno}\n");
fwrite(STDERR, "{$errstr}\nFile: {$errfile}\nLine: {$errline}\nCode: {$errno}\n");
exit(5);
});

require_once dirname(__DIR__) . '/src/autoloader.php';

// Parse the command line options
Enviro::initialize();

try {
if (isset(Enviro::$languages)) {
$languages = array();
foreach (Enviro::$languages as $languageId) {
$language = Language::getById($languageId);
if (!isset($language)) {
throw new Exception("Unable to find the language with id '{$languageId}'");
}
$languages[] = $language;
}
} else {
$languages = Language::getAll();
}
if (Enviro::$reduce) {
$languages = Enviro::reduce($languages);
}
if (Enviro::$noExtraParenthesis) {
$languages = array_map(
function (Language $language) {
$language->formula = $language->buildFormula(true);

return $language;
},
$languages
);
}
if (isset(Enviro::$outputFilename)) {
echo call_user_func(array(Exporter::getExporterClassName(Enviro::$outputFormat), 'toFile'), $languages, Enviro::$outputFilename, array('us-ascii' => Enviro::$outputUSAscii));
} else {
echo call_user_func(array(Exporter::getExporterClassName(Enviro::$outputFormat), 'toString'), $languages, array('us-ascii' => Enviro::$outputUSAscii));
}
} catch (Exception $x) {
fprintf(STDERR, $x->getMessage() . "\n");
fprintf(STDERR, "Trace:\n");
fprintf(STDERR, $x->getTraceAsString() . "\n");
exit(4);
}

exit(0);

/**
* Helper class to handle command line options.
*/
Expand Down Expand Up @@ -146,7 +104,7 @@ class Enviro
default:
if (preg_match('/^--output=.+$/', $argLC)) {
if (isset(self::$outputFilename)) {
fprintf(STDERR, "The output file name has been specified more than once!\n");
fwrite(STDERR, "The output file name has been specified more than once!\n");
self::showSyntax();
exit(3);
}
Expand All @@ -162,13 +120,13 @@ class Enviro
}
} elseif (isset($exporters[$argLC])) {
if (isset(self::$outputFormat)) {
fprintf(STDERR, "The output format has been specified more than once!\n");
fwrite(STDERR, "The output format has been specified more than once!\n");
self::showSyntax();
exit(3);
}
self::$outputFormat = $argLC;
} else {
fprintf(STDERR, "Unknown option: {$arg}\n");
fwrite(STDERR, "Unknown option: {$arg}\n");
self::showSyntax();
exit(2);
}
Expand Down Expand Up @@ -197,7 +155,7 @@ class Enviro
$basename = basename(__FILE__);
$exporters = array_keys(Exporter::getExporters(true));
$exporterList = implode('|', $exporters);
fprintf(
fwrite(
STDERR,
<<<EOT
Syntax:
Expand Down Expand Up @@ -238,9 +196,9 @@ EOT
);
$len = max(array_map('strlen', $exporters));
foreach ($exporters as $exporter) {
fprintf(STDERR, ' ' . str_pad($exporter, $len) . ': ' . Exporter::getExporterDescription($exporter) . "\n");
fwrite(STDERR, ' ' . str_pad($exporter, $len) . ': ' . Exporter::getExporterDescription($exporter) . "\n");
}
fprintf(STDERR, "\n");
fwrite(STDERR, "\n");
}

/**
Expand Down Expand Up @@ -287,3 +245,46 @@ EOT
return $languages;
}
}

// Parse the command line options
Enviro::initialize();

try {
if (isset(Enviro::$languages)) {
$languages = array();
foreach (Enviro::$languages as $languageId) {
$language = Language::getById($languageId);
if (!isset($language)) {
throw new Exception("Unable to find the language with id '{$languageId}'");
}
$languages[] = $language;
}
} else {
$languages = Language::getAll();
}
if (Enviro::$reduce) {
$languages = Enviro::reduce($languages);
}
if (Enviro::$noExtraParenthesis) {
$languages = array_map(
function (Language $language) {
$language->formula = $language->buildFormula(true);

return $language;
},
$languages
);
}
if (isset(Enviro::$outputFilename)) {
echo call_user_func(array(Exporter::getExporterClassName(Enviro::$outputFormat), 'toFile'), $languages, Enviro::$outputFilename, array('us-ascii' => Enviro::$outputUSAscii));
} else {
echo call_user_func(array(Exporter::getExporterClassName(Enviro::$outputFormat), 'toString'), $languages, array('us-ascii' => Enviro::$outputUSAscii));
}
} catch (Exception $x) {
fwrite(STDERR, $x->getMessage() . "\n");
fwrite(STDERR, "Trace:\n");
fwrite(STDERR, $x->getTraceAsString() . "\n");
exit(4);
}

exit(0);

0 comments on commit 5473976

Please sign in to comment.