Skip to content

Commit

Permalink
Merge pull request #1061 from IonBazan/twig-2.7
Browse files Browse the repository at this point in the history
use Twig 2.7 namespaces
  • Loading branch information
goetas authored Apr 21, 2019
2 parents c6b2342 + d1283b0 commit 8026e13
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
},
"require-dev": {
"ext-pdo_sqlite": "*",
"twig/twig": "^1.12|^2.0",
"twig/twig": "~1.34|~2.4",
"doctrine/orm": "~2.1",
"jackalope/jackalope-doctrine-dbal": "^1.1.5",
"doctrine/phpcr-odm": "^1.3|^2.0",
Expand Down
13 changes: 8 additions & 5 deletions src/Twig/SerializerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@

use JMS\Serializer\SerializationContext;
use JMS\Serializer\SerializerInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;

/**
* Serializer helper twig extension
*
* Basically provides access to JMSSerializer from Twig
*/
class SerializerExtension extends \Twig_Extension
class SerializerExtension extends AbstractExtension
{
/**
* @var SerializerInterface
Expand All @@ -35,26 +38,26 @@ public function __construct(SerializerInterface $serializer)
}

/**
* @return \Twig_Filter[]
* @return TwigFilter[]
*
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingReturnTypeHint
*/
public function getFilters()
{
return [
new \Twig_SimpleFilter('serialize', [$this, 'serialize']),
new TwigFilter('serialize', [$this, 'serialize']),
];
}

/**
* @return \Twig_Function[]
* @return TwigFunction[]
*
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingReturnTypeHint
*/
public function getFunctions()
{
return [
new \Twig_SimpleFunction('serialization_context', '\JMS\Serializer\SerializationContext::create'),
new TwigFunction('serialization_context', '\JMS\Serializer\SerializationContext::create'),
];
}

Expand Down
14 changes: 9 additions & 5 deletions src/Twig/SerializerRuntimeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@

namespace JMS\Serializer\Twig;

use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;

/**
* @author Asmir Mustafic <[email protected]>
*/
final class SerializerRuntimeExtension extends \Twig_Extension
final class SerializerRuntimeExtension extends AbstractExtension
{
/**
* @return string
Expand All @@ -20,26 +24,26 @@ public function getName()
}

/**
* @return \Twig_Filter[]
* @return TwigFilter[]
*
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingReturnTypeHint
*/
public function getFilters()
{
return [
new \Twig_SimpleFilter('serialize', [SerializerRuntimeHelper::class, 'serialize']),
new TwigFilter('serialize', [SerializerRuntimeHelper::class, 'serialize']),
];
}

/**
* @return \Twig_Function[]
* @return TwigFunction[]
*
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingReturnTypeHint
*/
public function getFunctions()
{
return [
new \Twig_SimpleFunction('serialization_context', '\JMS\Serializer\SerializationContext::create'),
new TwigFunction('serialization_context', '\JMS\Serializer\SerializationContext::create'),
];
}
}
10 changes: 6 additions & 4 deletions tests/Twig/SerializerExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use JMS\Serializer\Twig\SerializerRuntimeExtension;
use JMS\Serializer\Twig\SerializerRuntimeHelper;
use PHPUnit\Framework\TestCase;
use Twig\TwigFilter;
use Twig\TwigFunction;

class SerializerExtensionTest extends TestCase
{
Expand All @@ -25,11 +27,11 @@ public function testSerialize()
self::assertEquals('jms_serializer', $serializerExtension->getName());

$filters = $serializerExtension->getFilters();
self::assertInstanceOf('Twig_SimpleFilter', $filters[0]);
self::assertInstanceOf(TwigFilter::class, $filters[0]);
self::assertSame([$serializerExtension, 'serialize'], $filters[0]->getCallable());

self::assertEquals(
[new \Twig_SimpleFunction('serialization_context', '\JMS\Serializer\SerializationContext::create')],
[new TwigFunction('serialization_context', '\JMS\Serializer\SerializationContext::create')],
$serializerExtension->getFunctions()
);
}
Expand All @@ -54,11 +56,11 @@ public function testRuntimeSerializerExtension()

self::assertEquals('jms_serializer', $serializerExtension->getName());
self::assertEquals(
[new \Twig_SimpleFilter('serialize', [SerializerRuntimeHelper::class, 'serialize'])],
[new TwigFilter('serialize', [SerializerRuntimeHelper::class, 'serialize'])],
$serializerExtension->getFilters()
);
self::assertEquals(
[new \Twig_SimpleFunction('serialization_context', '\JMS\Serializer\SerializationContext::create')],
[new TwigFunction('serialization_context', '\JMS\Serializer\SerializationContext::create')],
$serializerExtension->getFunctions()
);
}
Expand Down

0 comments on commit 8026e13

Please sign in to comment.