Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use Twig 2.7 namespaces #1061

Merged
merged 3 commits into from
Apr 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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