From c50e14035abd0a4d28034be10e5d0a726e40cd0b Mon Sep 17 00:00:00 2001 From: Oboxo Srl Date: Thu, 25 Sep 2014 20:00:52 +0200 Subject: [PATCH] Added a --binpath param specify which gettext binaries to use. Especially useful for OsX Users. --- README.md | 22 ++++++++++++++++ Twig/Gettext/Extractor.php | 36 ++++++++++++++++++++++++--- Twig/Gettext/Test/ExtractorTest.php | 14 +++++++++++ Twig/Gettext/Test/Mock/SystemMock.php | 13 ++++++++++ cli/argParser.php | 27 ++++++++++++++++++++ cli/argParserTest.php | 18 ++++++++++++++ phpunit.xml.dist | 1 + twig-gettext-extractor | 18 +++++++------- 8 files changed, 137 insertions(+), 12 deletions(-) create mode 100644 Twig/Gettext/Test/Mock/SystemMock.php create mode 100644 cli/argParser.php create mode 100644 cli/argParserTest.php diff --git a/README.md b/README.md index 50249fd..4a0cc7a 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,28 @@ $twig->addFunction(new \Twig_SimpleFunction('myCustomExtension', true)); $twig->addFunction(new \Twig_SimpleFunction('myCustomExtension2', true)); ``` +## For OsX users + +Install gettext with [homebrew](http://brew.sh/) + +``` +$ brew install gettext +``` + +Get the path of the installed library by running + +``` +brew info gettext +``` + +It will be something like `/usr/local/Cellar/gettext/{{version}}` +edit the Parser command adding the binpath parameter this way: + +- Invocation: + - Parser command: `/vendor/bin/twig-gettext-extractor --binpath=/usr/local/Cellar/gettext/{{version}}/bin/ --sort-output --force-po -o %o %C %K -L PHP --files %F` + +Mind the trailing slash. + ## Tests To run the test suite, you need [composer](http://getcomposer.org) and diff --git a/Twig/Gettext/Extractor.php b/Twig/Gettext/Extractor.php index e7fa1af..9600ed4 100644 --- a/Twig/Gettext/Extractor.php +++ b/Twig/Gettext/Extractor.php @@ -39,9 +39,24 @@ class Extractor */ protected $parameters; - public function __construct(\Twig_Environment $environment) + /** + * The path where the gettext binaries are located + * + * @var string[] + */ + protected $binPath; + + /** + * A callable which will send the command to the system + * + * @var string[] + */ + protected $commandExecuter; + + public function __construct(\Twig_Environment $environment, $commandExecuter = 'system') { $this->environment = $environment; + $this->commandExecuter = $commandExecuter; $this->reset(); } @@ -49,6 +64,7 @@ protected function reset() { $this->templates = array(); $this->parameters = array(); + $this->binPath = ''; } public function addTemplate($path) @@ -67,14 +83,28 @@ public function setGettextParameters(array $parameters) $this->parameters = $parameters; } + public function setBinPath($binPath) + { + $this->binPath = $binPath; + } + public function extract() { - $command = 'xgettext'; + $command = $this->binPath.'xgettext'; $command .= ' '.join(' ', $this->parameters); $command .= ' '.join(' ', $this->templates); $error = 0; - $output = system($command, $error); + $output = null; + + if(is_string($this->commandExecuter)) { + $function = $this->commandExecuter; + $output = $function($command, $error); + } + else if(is_callable($this->commandExecuter)) { + $output = call_user_func_array($this->commandExecuter, array($command, $error)); + } + if (0 !== $error) { throw new \RuntimeException(sprintf( 'Gettext command "%s" failed with error code %s and output: %s', diff --git a/Twig/Gettext/Test/ExtractorTest.php b/Twig/Gettext/Test/ExtractorTest.php index d467835..eef0fad 100644 --- a/Twig/Gettext/Test/ExtractorTest.php +++ b/Twig/Gettext/Test/ExtractorTest.php @@ -100,6 +100,20 @@ public function testExtractNoTranslations() $this->assertEmpty($catalog->all('messages')); } + public function testSetBinPath() + { + $systemMock = new Mock\SystemMock(); + $extractor = new Extractor($this->twig, array($systemMock, 'execute')); + + $extractor->addTemplate(__DIR__.'/Fixtures/twig/empty.twig'); + $extractor->setGettextParameters($this->getGettextParameters()); + $extractor->setBinPath('/my/custom/bin/'); + + $extractor->extract(); + + $this->assertContains('/my/custom/bin/', $systemMock->command); + } + private function getPotFile() { return __DIR__.'/Fixtures/messages.pot'; diff --git a/Twig/Gettext/Test/Mock/SystemMock.php b/Twig/Gettext/Test/Mock/SystemMock.php new file mode 100644 index 0000000..1b7955e --- /dev/null +++ b/Twig/Gettext/Test/Mock/SystemMock.php @@ -0,0 +1,13 @@ +command = $command; + } +} diff --git a/cli/argParser.php b/cli/argParser.php new file mode 100644 index 0000000..caa566e --- /dev/null +++ b/cli/argParser.php @@ -0,0 +1,27 @@ + '', + 'templates' => array(), + 'params' => array() + ); + + $addTemplate = false; + + foreach ($args as $arg) { + if(strpos($arg, '--binpath') === 0) { + $parts = explode('=', $arg); + $parsed['binpath'] = $parts[1]; + } else if ('--files' == $arg) { + $addTemplate = true; + } else if ($addTemplate) { + $parsed['templates'][] = $arg; + } else { + $parsed['params'][] = $arg; + } + } + + return $parsed; +} diff --git a/cli/argParserTest.php b/cli/argParserTest.php new file mode 100644 index 0000000..e4a0fdf --- /dev/null +++ b/cli/argParserTest.php @@ -0,0 +1,18 @@ +assertEquals('/my/custom/bin/', $parsed['binpath']); + $this->assertEquals(array('"Views/test1.twig"', '"Views/test2.twig"'), $parsed['templates']); + $this->assertEquals(array('--sort-output', '--force-po', '-o', + '"/tmp/0extracted.pot"', '-k_', '-kgettext', '-kgettext_noop', '-L', 'PHP'), + $parsed['params']); + } +} diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 56fdc6b..5258bf0 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -9,6 +9,7 @@ ./Twig/Gettext/Test/ + ./cli/argParserTest.php diff --git a/twig-gettext-extractor b/twig-gettext-extractor index 6cc97c1..171012c 100755 --- a/twig-gettext-extractor +++ b/twig-gettext-extractor @@ -22,6 +22,8 @@ if (file_exists($a = __DIR__.'/../../autoload.php')) { require_once __DIR__.'/vendor/autoload.php'; } +require __DIR__.'/cli/argParser.php'; + $twig = new Twig_Environment(new Twig\Gettext\Loader\Filesystem('/'), array( 'cache' => '/tmp/cache/'.uniqid(), 'auto_reload' => true @@ -41,18 +43,16 @@ $twig->addExtension(new Symfony\Bridge\Twig\Extension\FormExtension( // You can add more extensions here. array_shift($_SERVER['argv']); -$addTemplate = false; $extractor = new Twig\Gettext\Extractor($twig); -foreach ($_SERVER['argv'] as $arg) { - if ('--files' == $arg) { - $addTemplate = true; - } else if ($addTemplate) { - $extractor->addTemplate(getcwd().DIRECTORY_SEPARATOR.$arg); - } else { - $extractor->addGettextParameter($arg); - } +$settings = parseArgs($_SERVER['argv']); +$extractor->setBinPath($settings['binpath']); +foreach ($settings['templates'] as $template) { + $extractor->addTemplate(getcwd().DIRECTORY_SEPARATOR.$template); +} +foreach ($settings['params'] as $param) { + $extractor->addGettextParameter($param); } $extractor->extract();