bug #20831 [Twig] Fix deprecations with Twig 1.29 (nicolas-grekas)

This PR was merged into the 2.7 branch.

Discussion
----------

[Twig] Fix deprecations with Twig 1.29

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #20829
| License       | MIT
| Doc PR        | -

Commits
-------

9218cac [Twig] Fix deprecations with Twig 1.29
This commit is contained in:
Nicolas Grekas 2016-12-08 15:27:27 +01:00
commit be5a5e43e7
5 changed files with 44 additions and 19 deletions

View File

@ -72,8 +72,7 @@ class TwigExtractorTest extends \PHPUnit_Framework_TestCase
}
/**
* @expectedException \Twig_Error
* @expectedExceptionMessageRegExp /Unclosed "block" in ".*extractor(\/|\\)syntax_error\.twig" at line 1/
* @expectedException \Twig_Error
* @dataProvider resourcesWithSyntaxErrorsProvider
*/
public function testExtractSyntaxError($resources)
@ -82,7 +81,19 @@ class TwigExtractorTest extends \PHPUnit_Framework_TestCase
$twig->addExtension(new TranslationExtension($this->getMock('Symfony\Component\Translation\TranslatorInterface')));
$extractor = new TwigExtractor($twig);
$extractor->extract($resources, new MessageCatalogue('en'));
try {
$extractor->extract($resources, new MessageCatalogue('en'));
} catch (\Twig_Error $e) {
if (method_exists($e, 'getSourceContext')) {
$this->assertSame(dirname(__DIR__).strtr('/Fixtures/extractor/syntax_error.twig', '/', DIRECTORY_SEPARATOR), $e->getFile());
$this->assertSame(1, $e->getLine());
$this->assertSame('Unclosed "block".', $e->getMessage());
} else {
$this->expectExceptionMessageRegExp('/Unclosed "block" in ".*extractor(\\/|\\\\)syntax_error\\.twig" at line 1/');
}
throw $e;
}
}
/**

View File

@ -61,10 +61,14 @@ class TwigExtractor extends AbstractFileExtractor implements ExtractorInterface
try {
$this->extractTemplate(file_get_contents($file->getPathname()), $catalogue);
} catch (\Twig_Error $e) {
if ($file instanceof SplFileInfo) {
$e->setTemplateName($file->getRelativePathname());
} elseif ($file instanceof \SplFileInfo) {
$e->setTemplateName($file->getRealPath() ?: $file->getPathname());
if ($file instanceof \SplFileInfo) {
$path = $file->getRealPath() ?: $file->getPathname();
$name = $file instanceof SplFileInfo ? $file->getRelativePathname() : $path;
if (method_exists($e, 'setSourceContext')) {
$e->setSourceContext(new \Twig_Source('', $name, $path));
} else {
$e->setTemplateName($name);
}
}
throw $e;

View File

@ -74,7 +74,13 @@ class TwigEngine extends BaseEngine implements EngineInterface
if ($name instanceof TemplateReference) {
try {
// try to get the real name of the template where the error occurred
$e->setTemplateName(sprintf('%s', $this->locator->locate($this->parser->parse($e->getTemplateName()))));
$name = $e->getTemplateName();
$path = (string) $this->locator->locate($this->parser->parse($name));
if (method_exists($e, 'setSourceContext')) {
$e->setSourceContext(new \Twig_Source('', $name, $path));
} else {
$e->setTemplateName($path);
}
} catch (\Exception $e2) {
}
}

View File

@ -37,9 +37,11 @@
<ul id="menu-profiler">
{% for name, template in templates %}
{% set menu -%}
{% with { 'collector': profile.getcollector(name) } %}
{{- block('menu', template) -}}
{% endwith %}
{% if block('menu', template) is defined %}
{% with { 'collector': profile.getcollector(name) } %}
{{- block('menu', template) -}}
{% endwith %}
{% endif %}
{%- endset %}
{% if menu != '' %}
<li class="{{ name }}{% if name == panel %} selected{% endif %}">

View File

@ -26,14 +26,16 @@
<div id="sfToolbarMainContent-{{ token }}" class="sf-toolbarreset clear-fix" data-no-turbolink>
{% for name, template in templates %}
{% with {
'collector': profile.getcollector(name),
'profiler_url': profiler_url,
'token': profile.token,
'name': name
} %}
{{ block('toolbar', template) }}
{% endwith %}
{% if block('toolbar', template) is defined %}
{% with {
'collector': profile.getcollector(name),
'profiler_url': profiler_url,
'token': profile.token,
'name': name
} %}
{{ block('toolbar', template) }}
{% endwith %}
{% endif %}
{% endfor %}
{% if 'normal' != position %}