Skip to content

Commit f5c57b2

Browse files
committed
Removing unused connection parameter from Twig environment factory
1 parent 6474d5c commit f5c57b2

File tree

3 files changed

+13
-31
lines changed

3 files changed

+13
-31
lines changed

src/Mouf/Database/MagicQuery.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ private function getConnectionUniqueId() {
244244
*/
245245
private function getTwigEnvironment() {
246246
if ($this->twigEnvironment === null) {
247-
$this->twigEnvironment = SqlTwigEnvironmentFactory::getTwigEnvironment($this->connection);
247+
$this->twigEnvironment = SqlTwigEnvironmentFactory::getTwigEnvironment();
248248
}
249249
return $this->twigEnvironment;
250250
}

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@
88
*/
99
class SqlTwigEnvironmentFactory
1010
{
11-
public static function getTwigEnvironment(Connection $connection = null) {
11+
private static $twig;
12+
13+
public static function getTwigEnvironment() {
14+
if (self::$twig) {
15+
return self::$twig;
16+
}
17+
1218
$stringLoader = new StringLoader();
1319

1420
$options = array(
@@ -21,13 +27,15 @@ public static function getTwigEnvironment(Connection $connection = null) {
2127

2228
// Default escaper will throw an exception. This is because we want to use SQL parameters instead of Twig.
2329
// This ahs a number of advantages, especially in terms of caching.
24-
$twig->getExtension('core')->setEscaper('sql', function(\Twig_Environment $env, $string, $charset) use ($connection) {
30+
$twig->getExtension('core')->setEscaper('sql', function() {
2531
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 .... %}"');
2632
});
2733

2834
// Default autoescape mode: sql
2935
$twig->addExtension(new \Twig_Extension_Escaper('sql'));
3036

37+
self::$twig = $twig;
38+
3139
return $twig;
3240
}
3341

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

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,12 @@
99

1010
class SqlTwigEnvironmentFactoryTest extends \PHPUnit_Framework_TestCase
1111
{
12-
public function getTwigWithConnection()
13-
{
14-
global $db_url;
15-
$config = new \Doctrine\DBAL\Configuration();
16-
// TODO: put this in conf variable
17-
$connectionParams = array(
18-
'url' => $db_url,
19-
);
20-
$conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);
21-
22-
$twigEnvironment = SqlTwigEnvironmentFactory::getTwigEnvironment($conn);
23-
return $twigEnvironment;
24-
}
25-
26-
/**
27-
* We need to run this test in a separate process because Twig seems to be enable to register several escapers
28-
* with the same name.
29-
*
30-
* @runInSeparateProcess
31-
*/
32-
/*public function testWithoutConnection()
33-
{
34-
$twigEnvironment = SqlTwigEnvironmentFactory::getTwigEnvironment();
35-
$this->doTestTwig($twigEnvironment);
36-
}*/
37-
3812
/**
3913
*
4014
*/
4115
public function testIf() {
4216

43-
$twig = $this->getTwigWithConnection();
17+
$twig = SqlTwigEnvironmentFactory::getTwigEnvironment();
4418
$sql = $twig->render("SELECT * FROM toto {% if id %}WHERE id = :id{% endif %}", ["id"=>12]);
4519
$this->assertEquals("SELECT * FROM toto WHERE id = :id", $sql);
4620
}
@@ -50,7 +24,7 @@ public function testIf() {
5024
*/
5125
public function testException() {
5226

53-
$twig = $this->getTwigWithConnection();
27+
$twig = SqlTwigEnvironmentFactory::getTwigEnvironment();
5428
$twig->render("SELECT * FROM toto WHERE id = {{ id }}", ["id"=>"hello"]);
5529
}
5630
}

0 commit comments

Comments
 (0)