Skip to content

Commit 2814641

Browse files
authored
Merge pull request #64 from moufmouf/twig3
Migrating to Twig 3
2 parents dd1a312 + 392048a commit 2814641

File tree

5 files changed

+21
-16
lines changed

5 files changed

+21
-16
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"mouf/utils.common.paginable-interface": "~1.0",
2020
"mouf/utils.common.sortable-interface": "~1.0",
2121
"mouf/schema-analyzer": "~1.0",
22-
"twig/twig": "^1.34.4 || ^2",
22+
"twig/twig": "^2.11 || ^3",
2323
"greenlion/php-sql-parser": "^4.1.2",
2424
"doctrine/cache": "^1.5"
2525
},

src/Mouf/Database/MagicQuery.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Doctrine\DBAL\Connection;
66
use Doctrine\DBAL\Platforms\AbstractPlatform;
77
use Doctrine\DBAL\Platforms\MySqlPlatform;
8+
use Twig\Environment;
89
use function array_filter;
910
use function array_keys;
1011
use Doctrine\Common\Cache\VoidCache;
@@ -40,7 +41,7 @@ class MagicQuery
4041
*/
4142
private $platform;
4243
/**
43-
* @var \Twig_Environment
44+
* @var Environment
4445
*/
4546
private $twigEnvironment;
4647
private $enableTwig = false;
@@ -321,7 +322,7 @@ private function getConnectionUniqueId(Connection $connection)
321322
}
322323

323324
/**
324-
* @return \Twig_Environment
325+
* @return Environment
325326
*/
326327
private function getTwigEnvironment()
327328
{

src/Mouf/Database/MagicQuery/Twig/SqlTwigEnvironmentFactory.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Mouf\Database\MagicQuery\Twig;
44

5+
use Twig\Environment;
6+
use Twig\Extension\CoreExtension;
7+
use Twig\Extension\EscaperExtension;
58
use Twig_Extension_Core;
69

710
/**
@@ -26,14 +29,14 @@ public static function getTwigEnvironment()
2629
'autoescape' => 'sql' // Default autoescape mode: sql
2730
);
2831

29-
$twig = new \Twig_Environment($stringLoader, $options);
32+
$twig = new Environment($stringLoader, $options);
3033

3134
// Default escaper will throw an exception. This is because we want to use SQL parameters instead of Twig.
3235
// This has a number of advantages, especially in terms of caching.
3336

34-
/** @var Twig_Extension_Core $twigExtensionCore */
35-
$twigExtensionCore = $twig->getExtension('Twig_Extension_Core');
36-
$twigExtensionCore->setEscaper('sql', function () {
37+
/** @var EscaperExtension $twigExtensionEscaper */
38+
$twigExtensionEscaper = $twig->getExtension(EscaperExtension::class);
39+
$twigExtensionEscaper->setEscaper('sql', function () {
3740
throw new ForbiddenTwigParameterInSqlException('You cannot use Twig expressions (like "{{ id }}"). Instead, you should use SQL parameters (like ":id"). Twig integration is limited to Twig statements (like "{% for .... %}"');
3841
});
3942

src/Mouf/Database/MagicQuery/Twig/StringLoader.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<?php
22

33
namespace Mouf\Database\MagicQuery\Twig;
4+
use Twig\Error\LoaderError;
5+
use Twig\Loader\LoaderInterface;
6+
use Twig\Source;
47
use Twig_Error_Loader;
58
use Twig_Source;
69

@@ -12,7 +15,7 @@
1215
* We enable it back in our case because there won't be a million of different cache keys.
1316
* And yes, we know what we are doing :)
1417
*/
15-
class StringLoader implements \Twig_LoaderInterface
18+
class StringLoader implements LoaderInterface
1619
{
1720
/**
1821
* {@inheritdoc}
@@ -24,14 +27,14 @@ public function getSource($name)
2427
/**
2528
* {@inheritdoc}
2629
*/
27-
public function getCacheKey($name)
30+
public function getCacheKey($name): string
2831
{
2932
return $name;
3033
}
3134
/**
3235
* {@inheritdoc}
3336
*/
34-
public function isFresh($name, $time)
37+
public function isFresh($name, $time): bool
3538
{
3639
return true;
3740
}
@@ -41,13 +44,11 @@ public function isFresh($name, $time)
4144
*
4245
* @param string $name The template logical name
4346
*
44-
* @return Twig_Source
45-
*
46-
* @throws Twig_Error_Loader When $name is not found
47+
* @return Source
4748
*/
48-
public function getSourceContext($name)
49+
public function getSourceContext($name): Source
4950
{
50-
return new Twig_Source($name, $name);
51+
return new Source($name, $name);
5152
}
5253

5354
/**

tests/Mouf/Database/MagicQuery/Twig/SqlTwigEnvironmentFactoryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function testIf()
1818
}
1919

2020
/**
21-
* @expectedException Twig_Error_Runtime
21+
* @expectedException \Twig\Error\RuntimeError
2222
*/
2323
public function testException()
2424
{

0 commit comments

Comments
 (0)