Skip to content

Commit

Permalink
#55 Support Twig 2.x (#56)
Browse files Browse the repository at this point in the history
* Update twig dependancy version to ~2.0

* Allow for BC twig > 1.23

* Clean unused attribute

* Install gettext in CI

* Remove hhvm (travis)
  • Loading branch information
Aluok authored and umpirsky committed Aug 28, 2017
1 parent 603ac24 commit 15dc422
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 48 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ php:
- 5.5
- 5.6
- 7.0
- hhvm

before_script:
- sudo apt-get -qq update
- sudo apt-get install -y gettext
- composer install --dev

script:
Expand Down
11 changes: 1 addition & 10 deletions Twig/Gettext/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@ class Extractor
*/
protected $environment;

/**
* Template cached file names.
*
* @var string[]
*/
protected $templates;

/**
* Gettext parameters.
*
Expand All @@ -47,14 +40,12 @@ public function __construct(\Twig_Environment $environment)

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

public function addTemplate($path)
{
$this->environment->loadTemplate($path);
$this->templates[] = $this->environment->getCacheFilename($path);
}

public function addGettextParameter($parameter)
Expand All @@ -71,7 +62,7 @@ public function extract()
{
$command = 'xgettext';
$command .= ' '.implode(' ', $this->parameters);
$command .= ' '.implode(' ', $this->templates);
$command .= ' ' . $this->environment->getCache() . '/*/*.php';

$error = 0;
$output = system($command, $error);
Expand Down
34 changes: 5 additions & 29 deletions Twig/Gettext/Loader/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,12 @@ class Filesystem extends \Twig_Loader_Filesystem
*
* @param string $name template name or absolute path
*/
protected function findTemplate($name)
protected function findTemplate($name, $throw = null)
{
// normalize name
$name = preg_replace('#/{2,}#', '/', strtr($name, '\\', '/'));

if (isset($this->cache[$name])) {
return $this->cache[$name];
}

$this->validateName($name);

$namespace = '__main__';
if (isset($name[0]) && '@' == $name[0]) {
if (false === $pos = strpos($name, '/')) {
throw new \InvalidArgumentException(sprintf('Malformed namespaced template name "%s" (expecting "@namespace/template_name").', $name));
}

$namespace = substr($name, 1, $pos - 1);

$name = substr($name, $pos + 1);
$result = parent::findTemplate($name, false);
if ($result === false) {
return __DIR__.'/../Test/Fixtures/twig/empty.twig';
}

if (!isset($this->paths[$namespace])) {
throw new \Twig_Error_Loader(sprintf('There are no registered paths for namespace "%s".', $namespace));
}

if (is_file($name)) {
return $this->cache[$name] = $name;
}

return __DIR__.'/../Test/Fixtures/twig/empty.twig';
return $result;
}
}
16 changes: 9 additions & 7 deletions Twig/Gettext/Test/ExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
/**
* @author Saša Stamenković <[email protected]>
*/
class ExtractorTest extends \PHPUnit_Framework_TestCase
class ExtractorTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Twig_Environment
Expand All @@ -32,7 +32,9 @@ class ExtractorTest extends \PHPUnit_Framework_TestCase

protected function setUp()
{
$this->twig = new \Twig_Environment(new Filesystem('/'), array(
$filesystem = new Filesystem('/', __DIR__.'/Fixtures/twig');
$filesystem->prependPath(__DIR__.'/Fixtures/twig');
$this->twig = new \Twig_Environment($filesystem, array(
'cache' => '/tmp/cache/'.uniqid(),
'auto_reload' => true,
));
Expand All @@ -42,7 +44,7 @@ protected function setUp()
}

/**
* @dataProvider testExtractDataProvider
* @dataProvider extractDataProvider
*/
public function testExtract(array $templates, array $parameters, array $messages)
{
Expand All @@ -67,13 +69,13 @@ public function testExtract(array $templates, array $parameters, array $messages
}
}

public function testExtractDataProvider()
public function extractDataProvider()
{
return array(
array(
array(
__DIR__.'/Fixtures/twig/singular.twig',
__DIR__.'/Fixtures/twig/plural.twig',
'/singular.twig',
'/plural.twig',
),
$this->getGettextParameters(),
array(
Expand All @@ -90,7 +92,7 @@ public function testExtractNoTranslations()
{
$extractor = new Extractor($this->twig);

$extractor->addTemplate(__DIR__.'/Fixtures/twig/empty.twig');
$extractor->addTemplate('/empty.twig');
$extractor->setGettextParameters($this->getGettextParameters());

$extractor->extract();
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
],
"require": {
"php": ">=5.5.9",
"twig/twig": "~1.2",
"twig/twig": "~1.23|~2.0",
"twig/extensions": "~1.0",
"symfony/twig-bridge": "~3.0",
"symfony/routing": "~3.0",
Expand Down

0 comments on commit 15dc422

Please sign in to comment.