Skip to content

Commit

Permalink
Twig 2.x compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul King authored and arnaud-lb committed Jun 20, 2019
1 parent b3f7910 commit 86ebfbe
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 14 deletions.
24 changes: 23 additions & 1 deletion lib/MtHaml/Support/Twig/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* $twig->setLoader($mthaml, new \MtHaml\Support\Twig\Loader($origLoader));
* </code>
*/
class Loader implements \Twig_LoaderInterface, \Twig_ExistsLoaderInterface
class Loader implements \Twig_LoaderInterface, \Twig_ExistsLoaderInterface, \Twig_SourceContextLoaderInterface
{
protected $env;
protected $loader;
Expand All @@ -29,6 +29,8 @@ public function __construct(Environment $env, \Twig_LoaderInterface $loader)
}

/**
* Deprecated in Twig 1.27
* Removed in Twig 2.x
* {@inheritdoc}
*/
public function getSource($name)
Expand All @@ -45,6 +47,26 @@ public function getSource($name)
return $source;
}

/**
* Support Twig 2.x
* {@inheritdoc}
*/
public function getSourceContext($name)
{
$context = $this->loader->getSourceContext($name);
$source = $context->getCode();

if ('haml' === pathinfo($name, PATHINFO_EXTENSION)) {
$source = $this->env->compileString($source, $name);
} elseif (preg_match('#^\s*{%\s*haml\s*%}#', $source, $match)) {
$padding = str_repeat(' ', strlen($match[0]));
$source = $padding . substr($source, strlen($match[0]));
$source = $this->env->compileString($source, $name);
}

return new \Twig\Source($source, $context->getName(), $context->getPath());
}

/**
* {@inheritdoc}
*/
Expand Down
46 changes: 33 additions & 13 deletions test/MtHaml/Tests/Support/Twig/LoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

class LoaderTest extends \PHPUnit_Framework_TestCase
{
protected $getSourceMethod;

public function testLoadSimpleTwigTemplate()
{
$env = $this->getMockBuilder('MtHaml\Environment')
Expand All @@ -14,13 +16,14 @@ public function testLoadSimpleTwigTemplate()
$env->expects($this->never())
->method('compileString');

$loader = $this->getMock('Twig_LoaderInterface');
$loader = $this->createMock('Twig_LoaderInterface');

$loader->expects($this->once())
->method('getSource')
->will($this->returnValue('<h1>{{ title }}</h1>'));
->method($this->getSourceMethod)
->will($this->returnValue($this->getSource('<h1>{{ title }}</h1>', 'template.twig')));

$hamlLoader = new Loader($env, $loader);
$hamlLoader->getSource('template.twig');
$hamlLoader->{$this->getSourceMethod}('template.twig');
}

public function testLoadHamlTemplate()
Expand All @@ -34,13 +37,14 @@ public function testLoadHamlTemplate()
->with('%h1= title')
->will($this->returnValue('<h1>{{ title }}</h1>'));

$loader = $this->getMock('Twig_LoaderInterface');
$loader = $this->createMock('Twig_LoaderInterface');

$loader->expects($this->once())
->method('getSource')
->will($this->returnValue('%h1= title'));
->method($this->getSourceMethod)
->will($this->returnValue($this->getSource('%h1= title', 'template.haml')));

$hamlLoader = new Loader($env, $loader);
$hamlLoader->getSource('template.haml');
$hamlLoader->{$this->getSourceMethod}('template.haml');
}

public function testLoadTwigWithHamlTemplate()
Expand All @@ -52,14 +56,30 @@ public function testLoadTwigWithHamlTemplate()
$env->expects($this->once())
->method('compileString')
->with(' %h1= title')
->will($this->returnValue('<h1>{{ title }}</h1>'));
->will($this->returnValue('<h1>{{ title }}</h1>', 'template.twig'));

$loader = $this->createMock('Twig_LoaderInterface');

$loader = $this->getMock('Twig_LoaderInterface');
$loader->expects($this->once())
->method('getSource')
->will($this->returnValue('{% haml %} %h1= title'));
->method($this->getSourceMethod)
->will($this->returnValue($this->getSource('{% haml %} %h1= title', 'template.twig')));

$hamlLoader = new Loader($env, $loader);
$hamlLoader->getSource('template.twig');
$hamlLoader->{$this->getSourceMethod}('template.twig');
}

protected function getSource($source, $name)
{
if ($this->getSourceMethod === 'getSourceContext') {
return new \Twig\Source($source, $name);
}

return $source;
}

protected function setUp()
{
$loader = $this->createMock('Twig_LoaderInterface');
$this->getSourceMethod = method_exists($loader, 'getSourceContext') ? 'getSourceContext' : 'getSource';
}
}

0 comments on commit 86ebfbe

Please sign in to comment.