Skip to content

Commit

Permalink
Update twig loader to support twig 2.x
Browse files Browse the repository at this point in the history
getSource() was deprecated in twig 1.27
getSourceContext() replaced it, which returns a Twig_Source object, rather than just the code
https://twig.symfony.com/doc/1.x/deprecated.html#loaders
  • Loading branch information
TechWilk committed Jan 22, 2018
1 parent 60b4247 commit 265a7f1
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 21 deletions.
56 changes: 47 additions & 9 deletions 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,20 +29,46 @@ public function __construct(Environment $env, \Twig_LoaderInterface $loader)
}

/**
* Deprecated in Twig 1.27
* Removed in Twig 2.x
* {@inheritdoc}
*/
public function getSource($name)
{
$source = $this->loader->getSource($name);
$code = $this->loader->getSource($name);

$code->renderHaml($name, $code);

return $code;
}

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

$code = $source->getCode();
$code = $this->renderHaml($name, $code);

$source = new \Twig_Source($code, $source->getName(), $source->getPath());

return $source;
}

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

return $source;
return $code;
}

/**
Expand All @@ -68,14 +94,26 @@ public function exists($name)
{
if ($this->loader instanceof \Twig_ExistsLoaderInterface) {
return $this->loader->exists($name);
} else {
}

// for Twig 2.x
if ($this->loader instanceof \Twig_SourceContextLoaderInterface) {
try {
$this->loader->getSource($name);
$this->loader->getSourceContext($name);

return true;
} catch (\Twig_Error_Loader $e) {
return false;
}
}

// for Twig 1.x
try {
$this->loader->getSource($name);

return true;
} catch (\Twig_Error_Loader $e) {
return false;
}
}
}
40 changes: 28 additions & 12 deletions test/MtHaml/Tests/Support/Twig/LoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@ public function testLoadSimpleTwigTemplate()
$env->expects($this->never())
->method('compileString');

$loader = $this->getMock('Twig_LoaderInterface');
$source = new \Twig_Source('<h1>{{ title }}</h1>', 'template.haml', '/somewhere/template.haml');

$loader = $this->getMockBuilder('Twig_LoaderInterface')
->setMethods(['getSourceContext'])
->getMock();
$loader->expects($this->once())
->method('getSource')
->will($this->returnValue('<h1>{{ title }}</h1>'));
->method('getSourceContext')
->with('template.haml')
->will($this->returnValue($source));

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

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

$loader = $this->getMock('Twig_LoaderInterface');
$source = new \Twig_Source('%h1= title', 'template.haml', '/somewhere/template.haml');

$loader = $this->getMockBuilder('Twig_LoaderInterface')
->setMethods(['getSourceContext'])
->getMock();
$loader->expects($this->once())
->method('getSource')
->will($this->returnValue('%h1= title'));
->method('getSourceContext')
->with('template.haml')
->will($this->returnValue($source));


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

public function testLoadTwigWithHamlTemplate()
Expand All @@ -54,12 +65,17 @@ public function testLoadTwigWithHamlTemplate()
->with(' %h1= title')
->will($this->returnValue('<h1>{{ title }}</h1>'));

$loader = $this->getMock('Twig_LoaderInterface');
$source = new \Twig_Source('{% haml %} %h1= title', 'template.haml', '/somewhere/template.haml');

$loader = $this->getMockBuilder('Twig_LoaderInterface')
->setMethods(['getSourceContext'])
->getMock();
$loader->expects($this->once())
->method('getSource')
->will($this->returnValue('{% haml %} %h1= title'));
->method('getSourceContext')
->with('template.haml')
->will($this->returnValue($source));

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

0 comments on commit 265a7f1

Please sign in to comment.