Skip to content

Commit 980cb8c

Browse files
authored
Merge pull request #39 from moufmouf/twig2
Adding Twig 2 compatibility
2 parents 4bc9dda + 32ceac0 commit 980cb8c

File tree

4 files changed

+40
-8
lines changed

4 files changed

+40
-8
lines changed

.travis.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@ php:
55
- 7.0
66
- 7.1
77

8+
env:
9+
matrix:
10+
- PREFER_LOWEST=""
11+
- PREFER_LOWEST="--prefer-lowest"
12+
813
before_script:
9-
- composer install --no-interaction
14+
- composer update --no-interaction $PREFER_LOWEST
1015

1116
script:
1217
- vendor/bin/phpunit

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
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.14",
23-
"greenlion/php-sql-parser": "^4.0"
22+
"twig/twig": "^1.34.4 || ^2",
23+
"greenlion/php-sql-parser": "^4.0",
24+
"doctrine/cache": "^1.5"
2425
},
2526
"require-dev": {
2627
"phpunit/phpunit": "~5.0",

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,17 @@ public static function getTwigEnvironment()
2121
// The cache directory is in the temporary directory and reproduces the path to the directory (to avoid cache conflict between apps).
2222
'cache' => self::getCacheDirectory(),
2323
'strict_variables' => true,
24+
'autoescape' => 'sql' // Default autoescape mode: sql
2425
);
2526

2627
$twig = new \Twig_Environment($stringLoader, $options);
2728

2829
// Default escaper will throw an exception. This is because we want to use SQL parameters instead of Twig.
29-
// This ahs a number of advantages, especially in terms of caching.
30-
$twig->getExtension('core')->setEscaper('sql', function () {
30+
// This has a number of advantages, especially in terms of caching.
31+
$twig->getExtension('Twig_Extension_Core')->setEscaper('sql', function () {
3132
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 .... %}"');
3233
});
3334

34-
// Default autoescape mode: sql
35-
$twig->addExtension(new \Twig_Extension_Escaper('sql'));
36-
3735
self::$twig = $twig;
3836

3937
return $twig;

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

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

33
namespace Mouf\Database\MagicQuery\Twig;
4+
use Twig_Error_Loader;
5+
use Twig_Source;
46

57
/**
68
* This loader completely bypasses the loader mechanism, by directly passing the key as a template.
@@ -33,4 +35,30 @@ public function isFresh($name, $time)
3335
{
3436
return true;
3537
}
38+
39+
/**
40+
* Returns the source context for a given template logical name.
41+
*
42+
* @param string $name The template logical name
43+
*
44+
* @return Twig_Source
45+
*
46+
* @throws Twig_Error_Loader When $name is not found
47+
*/
48+
public function getSourceContext($name)
49+
{
50+
return new Twig_Source($name, $name);
51+
}
52+
53+
/**
54+
* Check if we have the source code of a template, given its name.
55+
*
56+
* @param string $name The name of the template to check if we can load
57+
*
58+
* @return bool If the template source code is handled by this loader or not
59+
*/
60+
public function exists($name)
61+
{
62+
return true;
63+
}
3664
}

0 commit comments

Comments
 (0)