-
Notifications
You must be signed in to change notification settings - Fork 31
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is unhandled case when |
||
} | ||
else if(is_callable($this->commandExecuter)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please keep |
||
$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', | ||
|
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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
function parseArgs($args) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please wrap this into a class or just move it back to |
||
{ | ||
$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; | ||
} |
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']); | ||
} | ||
} |
There was a problem hiding this comment.
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?