Skip to content

Commit

Permalink
Merge pull request #1341 from schmittjoh/twig
Browse files Browse the repository at this point in the history
Allow to add prefix to twig helpers
  • Loading branch information
goetas authored Oct 13, 2021
2 parents 4a36e4b + 4994e43 commit d17d8fc
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 30 deletions.
30 changes: 30 additions & 0 deletions src/Twig/SerializerBaseExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace JMS\Serializer\Twig;

use Twig\Extension\AbstractExtension;

abstract class SerializerBaseExtension extends AbstractExtension
{
/**
* @var string
*/
protected $serializationFunctionsPrefix;

/**
* @return string
*
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingReturnTypeHint
*/
public function getName()
{
return 'jms_serializer';
}

public function __construct(string $serializationFunctionsPrefix = '')
{
$this->serializationFunctionsPrefix = $serializationFunctionsPrefix;
}
}
20 changes: 5 additions & 15 deletions src/Twig/SerializerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

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

Expand All @@ -15,26 +14,17 @@
*
* Basically provides access to JMSSerializer from Twig
*/
class SerializerExtension extends AbstractExtension
class SerializerExtension extends SerializerBaseExtension
{
/**
* @var SerializerInterface
*/
protected $serializer;

/**
* @return string
*
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingReturnTypeHint
*/
public function getName()
{
return 'jms_serializer';
}

public function __construct(SerializerInterface $serializer)
public function __construct(SerializerInterface $serializer, string $serializationFunctionsPrefix = '')
{
$this->serializer = $serializer;
parent::__construct($serializationFunctionsPrefix);
}

/**
Expand All @@ -45,7 +35,7 @@ public function __construct(SerializerInterface $serializer)
public function getFilters()
{
return [
new TwigFilter('serialize', [$this, 'serialize']),
new TwigFilter($this->serializationFunctionsPrefix . 'serialize', [$this, 'serialize']),
];
}

Expand All @@ -57,7 +47,7 @@ public function getFilters()
public function getFunctions()
{
return [
new TwigFunction('serialization_context', '\JMS\Serializer\SerializationContext::create'),
new TwigFunction($this->serializationFunctionsPrefix . 'serialization_context', '\JMS\Serializer\SerializationContext::create'),
];
}

Expand Down
17 changes: 3 additions & 14 deletions src/Twig/SerializerRuntimeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +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 AbstractExtension
final class SerializerRuntimeExtension extends SerializerBaseExtension
{
/**
* @return string
*
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingReturnTypeHint
*/
public function getName()
{
return 'jms_serializer';
}

/**
* @return TwigFilter[]
*
Expand All @@ -31,7 +20,7 @@ public function getName()
public function getFilters()
{
return [
new TwigFilter('serialize', [SerializerRuntimeHelper::class, 'serialize']),
new TwigFilter($this->serializationFunctionsPrefix . 'serialize', [SerializerRuntimeHelper::class, 'serialize']),
];
}

Expand All @@ -43,7 +32,7 @@ public function getFilters()
public function getFunctions()
{
return [
new TwigFunction('serialization_context', '\JMS\Serializer\SerializationContext::create'),
new TwigFunction($this->serializationFunctionsPrefix . 'serialization_context', '\JMS\Serializer\SerializationContext::create'),
];
}
}
25 changes: 24 additions & 1 deletion tests/Twig/SerializerExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,37 @@ public function testSerialize()

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

self::assertEquals(
[new TwigFunction('serialization_context', '\JMS\Serializer\SerializationContext::create')],
$serializerExtension->getFunctions()
);
}

public function testSerializeWithPrefix()
{
$mockSerializer = $this->getMockBuilder('JMS\Serializer\SerializerInterface')->getMock();
$obj = new \stdClass();
$mockSerializer
->expects($this->once())
->method('serialize')
->with($this->equalTo($obj), $this->equalTo('json'));
$serializerExtension = new SerializerExtension($mockSerializer, 'foo_');
$serializerExtension->serialize($obj);

self::assertEquals('jms_serializer', $serializerExtension->getName());

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

self::assertEquals(
[new TwigFunction('foo_serialization_context', '\JMS\Serializer\SerializationContext::create')],
$serializerExtension->getFunctions()
);
}

public function testRuntimeSerializerHelper()
{
$obj = new \stdClass();
Expand Down

0 comments on commit d17d8fc

Please sign in to comment.