Skip to content

Commit 68c4b38

Browse files
fix: error handling in StringTemplateRenderer for 6.5 to 6.7 compatibility (#82)
1 parent c70e38c commit 68c4b38

File tree

9 files changed

+170
-170
lines changed

9 files changed

+170
-170
lines changed

.github/workflows/code-style.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
strategy:
2525
fail-fast: false
2626
matrix:
27-
version-selection: [ 'lowest', 'highest']
27+
version-selection: [ 'highest' ]
2828
steps:
2929
- name: Checkout
3030
uses: actions/checkout@v4

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"shopware/core": "~6.5.0 || ~6.6.0 || ~6.7.0"
4949
},
5050
"scripts": {
51-
"format": "docker run --rm -v $(pwd):/ext ghcr.io/shopwarelabs/extension-verifier:latest format /ext",
52-
"check": "docker run --rm -v $(pwd):/ext ghcr.io/shopwarelabs/extension-verifier:latest check /ext"
51+
"format": "docker run --rm -v $(pwd):/ext shopware/shopware-cli:latest extension format /ext",
52+
"check": "docker run --rm -v $(pwd):/ext shopware/shopware-cli:latest extension validate --full /ext"
5353
}
5454
}

src/Command/ExportCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public function execute(InputInterface $input, OutputInterface $output): int
6363
$targetFile = \sprintf('%s/%s.twig', $templateDir, $name);
6464
}
6565

66+
// @phpstan-ignore-next-line
6667
$fs->dumpFile($targetFile, $record[$field]);
6768
}
6869
}
Lines changed: 93 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,100 @@
11
{% block sw_mail_template_list_grid %}
2-
<sw-card
3-
:title="$tc('sw-mail-template.list.titleMailTemplateList')"
4-
position-identifier="sw-mail-template-list"
5-
>
6-
<template>
7-
{% block sw_mail_template_list_grid_empty_state %}
8-
<sw-empty-state
9-
v-if="!isLoading && !showListing"
10-
:title="$tc('sw-mail-template.list.emptyStateTitle')"
11-
:subline="$tc('sw-mail-template.list.emptyStateSubTitle')"
12-
:absolute="false"
13-
>
14-
{% block sw_mail_template_list_grid_empty_state_icon %}
15-
<template #icon>
16-
<img
17-
:src="'/administration/static/img/empty-states/settings-empty-state.svg' | asset"
18-
alt
19-
/>
20-
</template>
21-
{% endblock %}
22-
</sw-empty-state>
2+
<sw-card
3+
:title="$tc('sw-mail-template.list.titleMailTemplateList')"
4+
position-identifier="sw-mail-template-list"
5+
>
6+
<template>
7+
{% block sw_mail_template_list_grid_empty_state %}
8+
<sw-empty-state
9+
v-if="!isLoading && !showListing"
10+
:title="$tc('sw-mail-template.list.emptyStateTitle')"
11+
:subline="$tc('sw-mail-template.list.emptyStateSubTitle')"
12+
:absolute="false"
13+
>
14+
{% block sw_mail_template_list_grid_empty_state_icon %}
15+
<template #icon>
16+
<img
17+
:src="'/administration/static/img/empty-states/settings-empty-state.svg' | asset"
18+
alt
19+
/>
20+
</template>
2321
{% endblock %}
24-
</template>
22+
</sw-empty-state>
23+
{% endblock %}
24+
</template>
2525

26-
<template #grid>
27-
<sw-entity-listing
28-
v-if="isLoading || showListing"
29-
id="mailTemplateGrid"
30-
class="sw-mail-templates-list-grid"
31-
detail-route="sw.mail.template.detail"
32-
identifier="sw-mail-template-list"
33-
:items="mailTemplates"
34-
:columns="getListColumns()"
35-
:repository="mailTemplateRepository"
36-
:full-page="false"
37-
:is-loading="isLoading"
38-
:allow-view="acl.can('mail_templates.viewer')"
39-
:allow-edit="acl.can('mail_templates.editor')"
40-
:allow-delete="acl.can('mail_templates.deleter')"
41-
:show-selection="acl.can('mail_templates.deleter')"
42-
:skeleton-item-amount="skeletonItemAmount"
43-
@update-records="updateRecords"
44-
>
45-
{% block sw_mail_template_list_grid_columns_frosh_template_mail %}
46-
<template #column-froshTemplateMail="{ item }">
47-
<sw-label
48-
appearance="circle"
49-
:variant="item.extensions?.froshTemplateMail.subject ? 'success' : 'warning'"
50-
v-tooltip="getFroshTooltip('subject', item.extensions.froshTemplateMail.subject)"
51-
>
52-
<sw-icon
53-
name="regular-exclamation-s"
54-
small
55-
/>
56-
</sw-label>
57-
<sw-label
58-
appearance="circle"
59-
:variant="item.extensions?.froshTemplateMail.plain ? 'success' : 'warning'"
60-
v-tooltip="getFroshTooltip('plain', item.extensions.froshTemplateMail.plain)"
61-
>
62-
<sw-icon
63-
name="regular-align-left"
64-
small
65-
/>
66-
</sw-label>
67-
<sw-label
68-
appearance="circle"
69-
:variant="item.extensions?.froshTemplateMail.html ? 'success' : 'warning'"
70-
v-tooltip="getFroshTooltip('html', item.extensions.froshTemplateMail.html)"
71-
>
72-
<sw-icon
73-
name="regular-code"
74-
small
75-
/>
76-
</sw-label>
77-
</template>
78-
{% endblock %}
26+
<template #grid>
27+
<sw-entity-listing
28+
v-if="isLoading || showListing"
29+
id="mailTemplateGrid"
30+
class="sw-mail-templates-list-grid"
31+
detail-route="sw.mail.template.detail"
32+
identifier="sw-mail-template-list"
33+
:items="mailTemplates"
34+
:columns="getListColumns()"
35+
:repository="mailTemplateRepository"
36+
:full-page="false"
37+
:is-loading="isLoading"
38+
:allow-view="acl.can('mail_templates.viewer')"
39+
:allow-edit="acl.can('mail_templates.editor')"
40+
:allow-delete="acl.can('mail_templates.deleter')"
41+
:show-selection="acl.can('mail_templates.deleter')"
42+
:skeleton-item-amount="skeletonItemAmount"
43+
@update-records="updateRecords"
44+
>
45+
{% block sw_mail_template_list_grid_columns_frosh_template_mail %}
46+
<template #column-froshTemplateMail="{ item }">
47+
<sw-label
48+
appearance="circle"
49+
:variant="item.extensions?.froshTemplateMail.subject ? 'success' : 'warning'"
50+
v-tooltip="getFroshTooltip('subject', item.extensions.froshTemplateMail.subject)"
51+
>
52+
<sw-icon
53+
name="regular-exclamation-s"
54+
small
55+
/>
56+
</sw-label>
57+
<sw-label
58+
appearance="circle"
59+
:variant="item.extensions?.froshTemplateMail.plain ? 'success' : 'warning'"
60+
v-tooltip="getFroshTooltip('plain', item.extensions.froshTemplateMail.plain)"
61+
>
62+
<sw-icon
63+
name="regular-align-left"
64+
small
65+
/>
66+
</sw-label>
67+
<sw-label
68+
appearance="circle"
69+
:variant="item.extensions?.froshTemplateMail.html ? 'success' : 'warning'"
70+
v-tooltip="getFroshTooltip('html', item.extensions.froshTemplateMail.html)"
71+
>
72+
<sw-icon
73+
name="regular-code"
74+
small
75+
/>
76+
</sw-label>
77+
</template>
78+
{% endblock %}
7979

8080
{% block sw_mail_template_list_grid_columns_technical_name %}
81-
<template #column-technicalName="{ item }">
82-
{{ item.extensions.froshTemplateMail.technicalName }}
83-
</template>
81+
<template #column-technicalName="{ item }">
82+
{{ item.extensions.froshTemplateMail.technicalName }}
83+
</template>
84+
{% endblock %}
85+
86+
<template #more-actions="{ item }">
87+
{% block sw_mail_template_list_grid_columns_actions_duplicate %}
88+
<sw-context-menu-item
89+
class="sw-mail-template-list-grid__duplicate-action"
90+
:disabled="!acl.can('mail_templates.creator')"
91+
@click="onDuplicate(item.id)"
92+
>
93+
{{ $tc('sw-mail-template.list.contextMenuDuplicate') }}
94+
</sw-context-menu-item>
8495
{% endblock %}
85-
<template #more-actions="{ item }">
86-
{% block sw_mail_template_list_grid_columns_actions_duplicate %}
87-
<sw-context-menu-item
88-
class="sw-mail-template-list-grid__duplicate-action"
89-
:disabled="!acl.can('mail_templates.creator')"
90-
@click="onDuplicate(item.id)"
91-
>
92-
{{ $tc('sw-mail-template.list.contextMenuDuplicate') }}
93-
</sw-context-menu-item>
94-
{% endblock %}
95-
</template>
96-
</sw-entity-listing>
97-
</template>
98-
</sw-card>
96+
</template>
97+
</sw-entity-listing>
98+
</template>
99+
</sw-card>
99100
{% endblock %}
Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,47 @@
11
{% block sw_mail_template_options_form_subject_field %}
2-
{% parent() %}
2+
{% parent() %}
33

4-
<sw-label
5-
v-if="mailTemplate.extensions.froshTemplateMail"
6-
appearance="circle"
7-
:variant="mailTemplate.extensions.froshTemplateMail.subject ? 'success' : 'warning'"
8-
v-tooltip="getFroshTooltip('subject', mailTemplate.extensions.froshTemplateMail.subject)"
9-
>
10-
<sw-icon
11-
name="regular-exclamation-s"
12-
small
13-
/>
14-
</sw-label>
4+
<sw-label
5+
v-if="mailTemplate.extensions.froshTemplateMail"
6+
appearance="circle"
7+
:variant="mailTemplate.extensions.froshTemplateMail.subject ? 'success' : 'warning'"
8+
v-tooltip="getFroshTooltip('subject', mailTemplate.extensions.froshTemplateMail.subject)"
9+
>
10+
<sw-icon
11+
name="regular-exclamation-s"
12+
small
13+
/>
14+
</sw-label>
1515
{% endblock %}
16+
1617
{% block sw_mail_template_mail_text_form_content_plain_field %}
17-
{% parent() %}
18+
{% parent() %}
1819

19-
<sw-label
20-
v-if="mailTemplate.extensions.froshTemplateMail"
21-
appearance="circle"
22-
:variant="mailTemplate.extensions.froshTemplateMail.plain ? 'success' : 'warning'"
23-
v-tooltip="getFroshTooltip('plain', mailTemplate.extensions.froshTemplateMail.plain)"
24-
>
25-
<sw-icon
26-
name="regular-align-left"
27-
small
28-
/>
29-
</sw-label>
20+
<sw-label
21+
v-if="mailTemplate.extensions.froshTemplateMail"
22+
appearance="circle"
23+
:variant="mailTemplate.extensions.froshTemplateMail.plain ? 'success' : 'warning'"
24+
v-tooltip="getFroshTooltip('plain', mailTemplate.extensions.froshTemplateMail.plain)"
25+
>
26+
<sw-icon
27+
name="regular-align-left"
28+
small
29+
/>
30+
</sw-label>
3031
{% endblock %}
32+
3133
{% block sw_mail_template_mail_text_form_content_html_field %}
32-
{% parent() %}
34+
{% parent() %}
3335

34-
<sw-label
35-
v-if="mailTemplate.extensions.froshTemplateMail"
36-
appearance="circle"
37-
:variant="mailTemplate.extensions.froshTemplateMail.html ? 'success' : 'warning'"
38-
v-tooltip="getFroshTooltip('html', mailTemplate.extensions.froshTemplateMail.html)"
39-
>
40-
<sw-icon
41-
name="regular-code"
42-
small
43-
/>
44-
</sw-label>
36+
<sw-label
37+
v-if="mailTemplate.extensions.froshTemplateMail"
38+
appearance="circle"
39+
:variant="mailTemplate.extensions.froshTemplateMail.html ? 'success' : 'warning'"
40+
v-tooltip="getFroshTooltip('html', mailTemplate.extensions.froshTemplateMail.html)"
41+
>
42+
<sw-icon
43+
name="regular-code"
44+
small
45+
/>
46+
</sw-label>
4547
{% endblock %}

src/Services/StringTemplateRenderer.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44

55
namespace Frosh\TemplateMail\Services;
66

7-
use Shopware\Core\Framework\Adapter\Twig\Exception\StringTemplateRenderingException;
87
use Shopware\Core\Framework\Context;
98
use Symfony\Component\DependencyInjection\Attribute\AsDecorator;
109
use Twig\Environment;
1110
use Twig\Error\Error;
11+
use Twig\Error\SyntaxError;
1212
use Twig\Extension\CoreExtension;
13+
use Twig\Extension\EscaperExtension;
1314
use Twig\Loader\ArrayLoader;
1415
use Twig\Loader\ChainLoader;
1516

@@ -68,11 +69,35 @@ public function render(string $templateSource, array $data, Context $context, bo
6869

6970
$this->twig->addGlobal('context', $context);
7071

72+
if ($this->twig->hasExtension(EscaperExtension::class)) {
73+
/** @var EscaperExtension $escaperExtension */
74+
$escaperExtension = $this->twig->getExtension(EscaperExtension::class);
75+
$escaperExtension->setDefaultStrategy($htmlEscape ? 'html' : false);
76+
}
77+
78+
if ($this->twig->hasExtension(CoreExtension::class) && \array_key_exists('timezone', $data) && $data['timezone'] !== null) {
79+
$coreExtension = $this->twig->getExtension(CoreExtension::class);
80+
$timezone = $data['timezone'];
81+
82+
if (\is_string($timezone) || $timezone instanceof \DateTimeZone) {
83+
$coreExtension->setTimezone($timezone);
84+
}
85+
}
86+
7187
try {
7288
return $this->twig->render($name, $data);
7389
} catch (Error $error) {
7490
// @phpstan-ignore-next-line
75-
throw new StringTemplateRenderingException($error->getMessage());
91+
if (class_exists(\Shopware\Core\Framework\Adapter\AdapterException::class) && method_exists(\Shopware\Core\Framework\Adapter\AdapterException::class, 'invalidTemplateSyntax')) {
92+
if ($error instanceof SyntaxError) {
93+
throw \Shopware\Core\Framework\Adapter\AdapterException::invalidTemplateSyntax($error->getMessage());
94+
}
95+
96+
throw \Shopware\Core\Framework\Adapter\AdapterException::renderingTemplateFailed($error->getMessage());
97+
} else {
98+
// @phpstan-ignore-next-line
99+
throw new \Shopware\Core\Framework\Adapter\Twig\Exception\StringTemplateRenderingException($error->getMessage());
100+
}
76101
}
77102
}
78103

src/Services/StringTemplateRenderingException.php

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)