Skip to content

Commit 9218cac

Browse files
[Twig] Fix deprecations with Twig 1.29
1 parent 7a07c1b commit 9218cac

File tree

5 files changed

+44
-19
lines changed

5 files changed

+44
-19
lines changed

src/Symfony/Bridge/Twig/Tests/Translation/TwigExtractorTest.php

+14-3
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ public function getExtractData()
7272
}
7373

7474
/**
75-
* @expectedException \Twig_Error
76-
* @expectedExceptionMessageRegExp /Unclosed "block" in ".*extractor(\/|\\)syntax_error\.twig" at line 1/
75+
* @expectedException \Twig_Error
7776
* @dataProvider resourcesWithSyntaxErrorsProvider
7877
*/
7978
public function testExtractSyntaxError($resources)
@@ -82,7 +81,19 @@ public function testExtractSyntaxError($resources)
8281
$twig->addExtension(new TranslationExtension($this->getMock('Symfony\Component\Translation\TranslatorInterface')));
8382

8483
$extractor = new TwigExtractor($twig);
85-
$extractor->extract($resources, new MessageCatalogue('en'));
84+
85+
try {
86+
$extractor->extract($resources, new MessageCatalogue('en'));
87+
} catch (\Twig_Error $e) {
88+
if (method_exists($e, 'getSourceContext')) {
89+
$this->assertSame(dirname(__DIR__).strtr('/Fixtures/extractor/syntax_error.twig', '/', DIRECTORY_SEPARATOR), $e->getFile());
90+
$this->assertSame(1, $e->getLine());
91+
$this->assertSame('Unclosed "block".', $e->getMessage());
92+
} else {
93+
$this->expectExceptionMessageRegExp('/Unclosed "block" in ".*extractor(\\/|\\\\)syntax_error\\.twig" at line 1/');
94+
}
95+
throw $e;
96+
}
8697
}
8798

8899
/**

src/Symfony/Bridge/Twig/Translation/TwigExtractor.php

+8-4
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,14 @@ public function extract($resource, MessageCatalogue $catalogue)
6161
try {
6262
$this->extractTemplate(file_get_contents($file->getPathname()), $catalogue);
6363
} catch (\Twig_Error $e) {
64-
if ($file instanceof SplFileInfo) {
65-
$e->setTemplateName($file->getRelativePathname());
66-
} elseif ($file instanceof \SplFileInfo) {
67-
$e->setTemplateName($file->getRealPath() ?: $file->getPathname());
64+
if ($file instanceof \SplFileInfo) {
65+
$path = $file->getRealPath() ?: $file->getPathname();
66+
$name = $file instanceof SplFileInfo ? $file->getRelativePathname() : $path;
67+
if (method_exists($e, 'setSourceContext')) {
68+
$e->setSourceContext(new \Twig_Source('', $name, $path));
69+
} else {
70+
$e->setTemplateName($name);
71+
}
6872
}
6973

7074
throw $e;

src/Symfony/Bundle/TwigBundle/TwigEngine.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,13 @@ public function render($name, array $parameters = array())
7474
if ($name instanceof TemplateReference) {
7575
try {
7676
// try to get the real name of the template where the error occurred
77-
$e->setTemplateName(sprintf('%s', $this->locator->locate($this->parser->parse($e->getTemplateName()))));
77+
$name = $e->getTemplateName();
78+
$path = (string) $this->locator->locate($this->parser->parse($name));
79+
if (method_exists($e, 'setSourceContext')) {
80+
$e->setSourceContext(new \Twig_Source('', $name, $path));
81+
} else {
82+
$e->setTemplateName($path);
83+
}
7884
} catch (\Exception $e2) {
7985
}
8086
}

src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/layout.html.twig

+5-3
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@
3737
<ul id="menu-profiler">
3838
{% for name, template in templates %}
3939
{% set menu -%}
40-
{% with { 'collector': profile.getcollector(name) } %}
41-
{{- block('menu', template) -}}
42-
{% endwith %}
40+
{% if block('menu', template) is defined %}
41+
{% with { 'collector': profile.getcollector(name) } %}
42+
{{- block('menu', template) -}}
43+
{% endwith %}
44+
{% endif %}
4345
{%- endset %}
4446
{% if menu != '' %}
4547
<li class="{{ name }}{% if name == panel %} selected{% endif %}">

src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/toolbar.html.twig

+10-8
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,16 @@
2626

2727
<div id="sfToolbarMainContent-{{ token }}" class="sf-toolbarreset clear-fix" data-no-turbolink>
2828
{% for name, template in templates %}
29-
{% with {
30-
'collector': profile.getcollector(name),
31-
'profiler_url': profiler_url,
32-
'token': profile.token,
33-
'name': name
34-
} %}
35-
{{ block('toolbar', template) }}
36-
{% endwith %}
29+
{% if block('toolbar', template) is defined %}
30+
{% with {
31+
'collector': profile.getcollector(name),
32+
'profiler_url': profiler_url,
33+
'token': profile.token,
34+
'name': name
35+
} %}
36+
{{ block('toolbar', template) }}
37+
{% endwith %}
38+
{% endif %}
3739
{% endfor %}
3840

3941
{% if 'normal' != position %}

0 commit comments

Comments
 (0)