Skip to content

Commit

Permalink
Merge pull request #64 from moufmouf/twig3
Browse files Browse the repository at this point in the history
Migrating to Twig 3
  • Loading branch information
moufmouf authored Dec 14, 2019
2 parents dd1a312 + 392048a commit 2814641
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 16 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"mouf/utils.common.paginable-interface": "~1.0",
"mouf/utils.common.sortable-interface": "~1.0",
"mouf/schema-analyzer": "~1.0",
"twig/twig": "^1.34.4 || ^2",
"twig/twig": "^2.11 || ^3",
"greenlion/php-sql-parser": "^4.1.2",
"doctrine/cache": "^1.5"
},
Expand Down
5 changes: 3 additions & 2 deletions src/Mouf/Database/MagicQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Twig\Environment;
use function array_filter;
use function array_keys;
use Doctrine\Common\Cache\VoidCache;
Expand Down Expand Up @@ -40,7 +41,7 @@ class MagicQuery
*/
private $platform;
/**
* @var \Twig_Environment
* @var Environment
*/
private $twigEnvironment;
private $enableTwig = false;
Expand Down Expand Up @@ -321,7 +322,7 @@ private function getConnectionUniqueId(Connection $connection)
}

/**
* @return \Twig_Environment
* @return Environment
*/
private function getTwigEnvironment()
{
Expand Down
11 changes: 7 additions & 4 deletions src/Mouf/Database/MagicQuery/Twig/SqlTwigEnvironmentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Mouf\Database\MagicQuery\Twig;

use Twig\Environment;
use Twig\Extension\CoreExtension;
use Twig\Extension\EscaperExtension;
use Twig_Extension_Core;

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

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

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

/** @var Twig_Extension_Core $twigExtensionCore */
$twigExtensionCore = $twig->getExtension('Twig_Extension_Core');
$twigExtensionCore->setEscaper('sql', function () {
/** @var EscaperExtension $twigExtensionEscaper */
$twigExtensionEscaper = $twig->getExtension(EscaperExtension::class);
$twigExtensionEscaper->setEscaper('sql', function () {
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 .... %}"');
});

Expand Down
17 changes: 9 additions & 8 deletions src/Mouf/Database/MagicQuery/Twig/StringLoader.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php

namespace Mouf\Database\MagicQuery\Twig;
use Twig\Error\LoaderError;
use Twig\Loader\LoaderInterface;
use Twig\Source;
use Twig_Error_Loader;
use Twig_Source;

Expand All @@ -12,7 +15,7 @@
* We enable it back in our case because there won't be a million of different cache keys.
* And yes, we know what we are doing :)
*/
class StringLoader implements \Twig_LoaderInterface
class StringLoader implements LoaderInterface
{
/**
* {@inheritdoc}
Expand All @@ -24,14 +27,14 @@ public function getSource($name)
/**
* {@inheritdoc}
*/
public function getCacheKey($name)
public function getCacheKey($name): string
{
return $name;
}
/**
* {@inheritdoc}
*/
public function isFresh($name, $time)
public function isFresh($name, $time): bool
{
return true;
}
Expand All @@ -41,13 +44,11 @@ public function isFresh($name, $time)
*
* @param string $name The template logical name
*
* @return Twig_Source
*
* @throws Twig_Error_Loader When $name is not found
* @return Source
*/
public function getSourceContext($name)
public function getSourceContext($name): Source
{
return new Twig_Source($name, $name);
return new Source($name, $name);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function testIf()
}

/**
* @expectedException Twig_Error_Runtime
* @expectedException \Twig\Error\RuntimeError
*/
public function testException()
{
Expand Down

0 comments on commit 2814641

Please sign in to comment.