Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a --binpath param specify which gettext binaries to use. Especiall... #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,28 @@ $twig->addFunction(new \Twig_SimpleFunction('myCustomExtension', true));
$twig->addFunction(new \Twig_SimpleFunction('myCustomExtension2', true));
```

## For OsX users
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it OS X?


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: `<project>/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
Expand Down
36 changes: 33 additions & 3 deletions Twig/Gettext/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,32 @@ 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();
}

protected function reset()
{
$this->templates = array();
$this->parameters = array();
$this->binPath = '';
}

public function addTemplate($path)
Expand All @@ -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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is unhandled case when $function is not executable.

}
else if(is_callable($this->commandExecuter)) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep else if in the same line with } {. There are also other CS issues, I suggest running php-cs-fixer.

$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',
Expand Down
14 changes: 14 additions & 0 deletions Twig/Gettext/Test/ExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
13 changes: 13 additions & 0 deletions Twig/Gettext/Test/Mock/SystemMock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Twig\Gettext\Test\Mock;

class SystemMock
{
public $command;

public function execute($command, $error)
{
$this->command = $command;
}
}
27 changes: 27 additions & 0 deletions cli/argParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

function parseArgs($args)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please wrap this into a class or just move it back to twig-gettext-extractor. I would like to get rid or cli directory.

{
$parsed = array(
'binpath' => '',
'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;
}
18 changes: 18 additions & 0 deletions cli/argParserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

require 'argParser.php';

class CliTest extends \PHPUnit_Framework_TestCase
{
public function testArguments()
{
$argv = '--binpath=/my/custom/bin/ --sort-output --force-po -o "/tmp/0extracted.pot" -k_ -kgettext -kgettext_noop -L PHP --files "Views/test1.twig" "Views/test2.twig"';
$parsed = parseArgs(explode(' ', $argv));

$this->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']);
}
}
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<testsuites>
<testsuite name="Twig Gettext Extractor Test Suite">
<directory>./Twig/Gettext/Test/</directory>
<file>./cli/argParserTest.php</file>
</testsuite>
</testsuites>
</phpunit>
18 changes: 9 additions & 9 deletions twig-gettext-extractor
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();