Skip to content

Commit b7af516

Browse files
authored
Drop Twig 1.x support / Add Twig 3.x support (#1270)
* Drop Twig 1.x support * Avoid using internal method/class usage * Support Twig 3.x * Update tests
1 parent ec9cdaf commit b7af516

File tree

11 files changed

+89
-138
lines changed

11 files changed

+89
-138
lines changed

.travis.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ matrix:
66
fast_finish: true
77
include:
88
- php: 7.2
9-
env: SYMFONY_REQUIRE="^4.4"
9+
env:
10+
- SYMFONY_REQUIRE="^4.4"
11+
- TWIG_VERSION="^2.9"
1012
- php: 7.3
1113
- php: 7.4
1214
- php: nightly
@@ -20,8 +22,8 @@ before_install:
2022
- composer global require --no-progress --no-scripts --no-plugins symfony/flex dev-master
2123

2224
install:
23-
- if [ "$SYMFONY_VERSION" != "" ]; then composer require "symfony/symfony:${SYMFONY_VERSION}" --no-update; fi;
24-
- composer update --prefer-source --no-interaction --no-scripts $COMPOSER_FLAGS
25+
- if [ "$TWIG_VERSION" != "" ]; then composer require "twig/twig:${TWIG_VERSION}" --no-update; fi;
26+
- composer update --prefer-dist --no-interaction --no-scripts $COMPOSER_FLAGS
2527

2628
script:
2729
- vendor/bin/phpunit

Form/Extension/TabbedFormTypeExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public function finishView(FormView $view, FormInterface $form, array $options)
8787

8888
$tabs[$tabIndex] = [
8989
'id' => $child->vars['id'],
90+
'name' => $child->vars['name'],
9091
'label' => $child->vars['label'],
9192
'icon' => $child->vars['icon'],
9293
'active' => false,

Resources/views/Form/fields.html.twig

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,11 @@
141141
<li{% if class|trim is not empty %} class="{{ class }}"{% endif %}>
142142
<a data-toggle="tab" href="#{{ tab.id }}">
143143
{% if tab.icon %}{{ mopa_bootstrap_icon(tab.icon) }}{% endif %}
144-
{{ tab.translation_domain is same as(false) ? tab.label : tab.label|trans({}, tab.translation_domain) }}
144+
{%- if tab.label is not empty -%}
145+
{{ tab.translation_domain is same as(false) ? tab.label : tab.label|trans({}, tab.translation_domain) }}
146+
{%- elseif tab.label is not same as(false) -%}
147+
{{ tab.translation_domain is same as(false) ? tab.name|humanize : tab.name|humanize|trans({}, tab.translation_domain) }}
148+
{%- endif -%}
145149
</a>
146150
</li>
147151
{% endfor %}

Tests/Form/AbstractDivLayoutTest.php

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,22 @@
2121
use Mopa\Bundle\BootstrapBundle\Form\Extension\WidgetCollectionFormTypeExtension;
2222
use Mopa\Bundle\BootstrapBundle\Form\Extension\WidgetFormTypeExtension;
2323
use Mopa\Bundle\BootstrapBundle\Form\Type\TabType;
24+
use Mopa\Bundle\BootstrapBundle\Tests\Stub\StubTranslator;
2425
use Mopa\Bundle\BootstrapBundle\Twig\FormExtension as TwigFormExtension;
2526
use Mopa\Bundle\BootstrapBundle\Twig\IconExtension;
2627
use Symfony\Bridge\Twig\Extension\FormExtension;
2728
use Symfony\Bridge\Twig\Extension\TranslationExtension;
28-
use Symfony\Bridge\Twig\Form\TwigRenderer;
2929
use Symfony\Bridge\Twig\Form\TwigRendererEngine;
30-
use Symfony\Bridge\Twig\Tests\Extension\Fixtures\StubTranslator;
3130
use Symfony\Component\Form\Extension\Core\Type\FormType;
3231
use Symfony\Component\Form\Extension\Core\Type\TextType;
3332
use Symfony\Component\Form\FormRenderer;
3433
use Symfony\Component\Form\Forms;
3534
use Symfony\Component\Form\FormView;
3635
use Symfony\Component\Form\PreloadedExtension;
3736
use Symfony\Component\Form\Test\FormIntegrationTestCase;
37+
use Twig\Environment;
38+
use Twig\Loader\FilesystemLoader;
39+
use Twig\RuntimeLoader\FactoryRuntimeLoader;
3840

3941
abstract class AbstractDivLayoutTest extends FormIntegrationTestCase
4042
{
@@ -43,28 +45,24 @@ abstract class AbstractDivLayoutTest extends FormIntegrationTestCase
4345
protected $environment;
4446
protected $tabFactory;
4547

46-
/**
47-
* @throws \Twig_Error_Loader
48-
*/
4948
protected function setUp(): void
5049
{
5150
// Setup factory for tabs
5251
$this->tabFactory = Forms::createFormFactory();
5352

5453
parent::setUp();
5554

56-
$reflectionClass = \class_exists('Symfony\Bridge\Twig\Form\TwigRenderer') ? 'Symfony\Bridge\Twig\Form\TwigRenderer' : 'Symfony\Bridge\Twig\Form\TwigRendererEngine';
57-
$reflection = new \ReflectionClass($reflectionClass);
55+
$reflection = new \ReflectionClass(TwigRendererEngine::class);
5856
$bridgeDirectory = \dirname($reflection->getFileName()).'/../Resources/views/Form';
5957

60-
$loader = new \Twig_Loader_Filesystem([
58+
$loader = new FilesystemLoader([
6159
$bridgeDirectory,
6260
__DIR__.'/../../Resources/views/Form',
6361
]);
6462

6563
$loader->addPath(__DIR__.'/../../Resources/views', 'MopaBootstrap');
6664

67-
$this->environment = new \Twig_Environment($loader, ['strict_variables' => true]);
65+
$this->environment = new Environment($loader, ['strict_variables' => true]);
6866
$this->environment->addExtension(new TranslationExtension(new StubTranslator()));
6967
$this->environment->addExtension(new IconExtension('fontawesome'));
7068
$this->environment->addExtension(new TwigFormExtension());
@@ -75,31 +73,15 @@ protected function setUp(): void
7573
'fields.html.twig',
7674
], $this->environment);
7775

78-
$this->setUpVersion4Plus();
79-
}
80-
81-
private function setUpVersion4Plus()
82-
{
8376
$csrfProvider = $this->getMockBuilder('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface')->getMock();
84-
$loaders = [
85-
'Symfony\Component\Form\FormRenderer' => function () use ($csrfProvider) {
86-
return new FormRenderer($this->rendererEngine, $csrfProvider);
87-
},
88-
];
89-
90-
$runtime = 'Symfony\Component\Form\FormRenderer';
91-
92-
if (\class_exists('Symfony\Bridge\Twig\Form\TwigRenderer')) {
93-
$loaders['Symfony\Bridge\Twig\Form\TwigRenderer'] = function () use ($csrfProvider) {
94-
return new TwigRenderer($this->rendererEngine, $csrfProvider);
95-
};
96-
97-
$runtime = 'Symfony\Bridge\Twig\Form\TwigRenderer';
98-
}
9977

10078
// Add runtime loader
101-
$this->environment->addRuntimeLoader(new \Twig_FactoryRuntimeLoader($loaders));
102-
$this->renderer = $this->environment->getRuntime($runtime);
79+
$this->environment->addRuntimeLoader(new FactoryRuntimeLoader([
80+
FormRenderer::class => function () use ($csrfProvider) {
81+
return new FormRenderer($this->rendererEngine, $csrfProvider);
82+
},
83+
]));
84+
$this->renderer = $this->environment->getRuntime(FormRenderer::class);
10385

10486
$this->environment->addExtension(new FormExtension());
10587
}

Tests/Stub/StubTranslator.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the MopaBootstrapBundle.
5+
*
6+
* (c) Philipp A. Mohrenweiser <phiamo@googlemail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Mopa\Bundle\BootstrapBundle\Tests\Stub;
13+
14+
use Symfony\Contracts\Translation\TranslatorInterface;
15+
16+
class StubTranslator implements TranslatorInterface
17+
{
18+
public function trans(string $id, array $parameters = [], string $domain = null, string $locale = null): string
19+
{
20+
return '[trans]'.\strtr($id, $parameters).'[/trans]';
21+
}
22+
}

Twig/FlashExtension.php

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,30 @@
1111

1212
namespace Mopa\Bundle\BootstrapBundle\Twig;
1313

14+
use Twig\Extension\AbstractExtension;
15+
use Twig\TwigFunction;
16+
1417
/**
1518
* MopaBootstrap Flash Extension.
1619
*
1720
* @author Nikolai Zujev (jaymecd) <nikolai.zujev@gmail.com>
1821
*/
19-
class FlashExtension extends \Twig_Extension
22+
class FlashExtension extends AbstractExtension
2023
{
2124
/**
2225
* @var array
2326
*/
2427
protected $mapping = [];
2528

26-
/**
27-
* Constructor.
28-
*/
2929
public function __construct(array $mapping)
3030
{
3131
$this->mapping = $mapping;
3232
}
3333

34-
/**
35-
* {@inheritdoc}
36-
*/
37-
public function getFunctions()
34+
public function getFunctions(): array
3835
{
3936
return [
40-
new \Twig_SimpleFunction('mopa_bootstrap_flash_mapping', [$this, 'getMapping'], ['is_safe' => ['html']]),
37+
new TwigFunction('mopa_bootstrap_flash_mapping', [$this, 'getMapping'], ['is_safe' => ['html']]),
4138
];
4239
}
4340

@@ -50,12 +47,4 @@ public function getMapping()
5047
{
5148
return $this->mapping;
5249
}
53-
54-
/**
55-
* {@inheritdoc}
56-
*/
57-
public function getName()
58-
{
59-
return 'mopa_bootstrap_flash';
60-
}
6150
}

Twig/FormExtension.php

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
namespace Mopa\Bundle\BootstrapBundle\Twig;
1313

14+
use Twig\Extension\AbstractExtension;
15+
use Twig\TwigFunction;
16+
1417
/**
1518
* Twig extension for form.
1619
*
@@ -19,34 +22,19 @@
1922
* @author Paweł Madej (nysander) <pawel.madej@profarmaceuta.pl>
2023
* @author Charles Sanquer <charles.sanquer@gmail.com>
2124
*/
22-
class FormExtension extends \Twig_Extension
25+
class FormExtension extends AbstractExtension
2326
{
24-
/**
25-
* Returns a list of functions to add to the existing list.
26-
*
27-
* @return array An array of functions
28-
*/
29-
public function getFunctions()
27+
public function getFunctions(): array
3028
{
3129
return [
32-
new \Twig_SimpleFunction('form_help', null, [
30+
new TwigFunction('form_help', null, [
3331
'node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode',
3432
'is_safe' => ['html'],
3533
]),
36-
new \Twig_SimpleFunction('form_tabs', null, [
34+
new TwigFunction('form_tabs', null, [
3735
'node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode',
3836
'is_safe' => ['html'],
3937
]),
4038
];
4139
}
42-
43-
/**
44-
* Returns the name of the extension.
45-
*
46-
* @return string The extension name
47-
*/
48-
public function getName()
49-
{
50-
return 'bootstrap_form';
51-
}
5240
}

Twig/IconExtension.php

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,58 +11,56 @@
1111

1212
namespace Mopa\Bundle\BootstrapBundle\Twig;
1313

14-
use Symfony\Component\HttpFoundation\Response;
14+
use Twig\Environment;
15+
use Twig\Extension\AbstractExtension;
16+
use Twig\TemplateWrapper;
17+
use Twig\TwigFunction;
1518

1619
/**
1720
* MopaBootstrap Icon Extension.
1821
*
1922
* @author Craig Blanchette (isometriks) <craig.blanchette@gmail.com>
2023
*/
21-
class IconExtension extends \Twig_Extension
24+
class IconExtension extends AbstractExtension
2225
{
2326
/**
2427
* @var string
2528
*/
2629
protected $iconSet;
2730

2831
/**
29-
* @var string
32+
* @var string|null
3033
*/
3134
protected $shortcut;
3235

3336
/**
34-
* @var \Twig_Template
37+
* @var TemplateWrapper|null
3538
*/
3639
protected $iconTemplate;
3740

3841
/**
39-
* Constructor.
40-
*
41-
* @param string $iconSet
42-
* @param string $shortcut
42+
* @param string $iconSet
43+
* @param string|null $shortcut
4344
*/
4445
public function __construct($iconSet, $shortcut = null)
4546
{
4647
$this->iconSet = $iconSet;
4748
$this->shortcut = $shortcut;
4849
}
4950

50-
/**
51-
* {@inheritdoc}
52-
*/
53-
public function getFunctions()
51+
public function getFunctions(): array
5452
{
5553
$options = [
5654
'is_safe' => ['html'],
5755
'needs_environment' => true,
5856
];
5957

6058
$functions = [
61-
new \Twig_SimpleFunction('mopa_bootstrap_icon', [$this, 'renderIcon'], $options),
59+
new TwigFunction('mopa_bootstrap_icon', [$this, 'renderIcon'], $options),
6260
];
6361

6462
if ($this->shortcut) {
65-
$functions[] = new \Twig_SimpleFunction($this->shortcut, [$this, 'renderIcon'], $options);
63+
$functions[] = new TwigFunction($this->shortcut, [$this, 'renderIcon'], $options);
6664
}
6765

6866
return $functions;
@@ -73,10 +71,8 @@ public function getFunctions()
7371
*
7472
* @param string $icon
7573
* @param bool $inverted
76-
*
77-
* @return Response
7874
*/
79-
public function renderIcon(\Twig_Environment $env, $icon, $inverted = false)
75+
public function renderIcon(Environment $env, $icon, $inverted = false): string
8076
{
8177
$template = $this->getIconTemplate($env);
8278
$context = [
@@ -87,21 +83,10 @@ public function renderIcon(\Twig_Environment $env, $icon, $inverted = false)
8783
return $template->renderBlock($this->iconSet, $context);
8884
}
8985

90-
/**
91-
* {@inheritdoc}
92-
*/
93-
public function getName()
94-
{
95-
return 'mopa_bootstrap_icon';
96-
}
97-
98-
/**
99-
* @return \Twig_Template
100-
*/
101-
protected function getIconTemplate(\Twig_Environment $env)
86+
protected function getIconTemplate(Environment $env): TemplateWrapper
10287
{
10388
if ($this->iconTemplate === null) {
104-
$this->iconTemplate = $env->loadTemplate('@MopaBootstrap/icons.html.twig');
89+
$this->iconTemplate = $env->load('@MopaBootstrap/icons.html.twig');
10590
}
10691

10792
return $this->iconTemplate;

0 commit comments

Comments
 (0)