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

Update twig loader to support Twig 2.x #105

Open
wants to merge 6 commits 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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ php:
- 5.5
- 5.6
- 7
- 7.1
- nightly
- hhvm
matrix:
allow_failures:
- php: hhvm
- php: nightly
- php: 7
before_script:
- composer install --no-interaction
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"cebe/markdown": "~1",
"kzykhys/ciconia": "~1",
"fluxbb/commonmark": "~1@dev",
"gregwar/rst": "~1"
"gregwar/rst": "1.0.0"
},
"suggest": {
"twig/twig": "If you want to use Twig templating engine",
Expand Down
2 changes: 1 addition & 1 deletion lib/MtHaml/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ private function parseTagAttributeHtml(Buffer $buf)
return new TagAttributeInterpolation($expr->getPosition(), $expr);
}

if ($buf->match('/[\w+:-]+/A', $match)) {
if ($buf->match('/[@\.\w+:-]+/A', $match)) {
$name = new Text($match['pos'][0], $match[0]);

if (!$buf->match('/\s*=\s*/A')) {
Expand Down
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;
}
}
}
15 changes: 14 additions & 1 deletion test/MtHaml/Tests/EnvironmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ public function testEnvironment($file)
file_put_contents($file . '.php', $parts['FILE']);
file_put_contents($file . '.exp', $parts['EXPECT']);

if (isset($parts['SKIPIF'])) {
file_put_contents($file . '.skip.php', $parts['SKIPIF']);
}

if (isset($parts['SKIPIF'])) {
ob_start();
require $file . '.skip.php';
$out = ob_get_clean();
if (false !== strpos($out, 'skip')) {
return $this->markTestSkipped();
}
}

try {
ob_start();
require $file . '.php';
Expand Down Expand Up @@ -54,6 +67,6 @@ public function getEnvironmentTests()

return array_map(function ($file) {
return array($file);
}, $files);
}, array_combine($files, $files));
}
}
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');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ $filter = new MtHaml\Filter\CoffeeScript(new CoffeeScript\Compiler);
$env = new MtHaml\Environment('twig', array('enable_escaper' => false), array('coffee' => $filter));
echo $env->compileString($parts['HAML'], "$file.haml");

--SKIPIF--
<?php
PHP_MAJOR_VERSION >= 7 && print("skip");

--HAML--
:coffee
square = (x) -> x * x
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ $filter = new MtHaml\Filter\Markdown\FluxBBMarkdown(new FluxBB\CommonMark\Docume
$env = new MtHaml\Environment('twig', array('enable_escaper' => false), array('markdown' => $filter));
echo $env->compileString($parts['HAML'], "$file.haml");

--SKIPIF--
<?php
PHP_MAJOR_VERSION >= 7 && print("skip");

--HAML--
:markdown
## Header
Expand Down