浏览代码

Merge pull request #62 from jonotron/twig-filter

Twig filter access to serializer
Johannes 13 年之前
父节点
当前提交
bf10c3ede2
共有 4 个文件被更改,包括 110 次插入1 次删除
  1. 9 1
      Resources/config/services.xml
  2. 7 0
      Resources/doc/index.rst
  3. 41 0
      Tests/Twig/SerializerExtensionTest.php
  4. 53 0
      Twig/SerializerExtension.php

+ 9 - 1
Resources/config/services.xml

@@ -25,6 +25,8 @@
         <parameter key="jms_serializer.version_exclusion_strategy.class">JMS\SerializerBundle\Serializer\Exclusion\VersionExclusionStrategy</parameter>
 
         <parameter key="jms_serializer.serializer.class">JMS\SerializerBundle\Serializer\LazyLoadingSerializer</parameter>
+
+        <parameter key="jms_serializer.twig_extension.class">JMS\SerializerBundle\Twig\SerializerExtension</parameter>
         
         <parameter key="jms_serializer.json_serialization_visitor.class">JMS\SerializerBundle\Serializer\JsonSerializationVisitor</parameter>
         <parameter key="jms_serializer.json_deserialization_visitor.class">JMS\SerializerBundle\Serializer\JsonDeserializationVisitor</parameter>
@@ -114,7 +116,13 @@
             <tag name="jms_serializer.serializer" />
         </service>
         <service id="serializer" alias="jms_serializer.serializer" />
-        
+
+        <!-- Twig Extension -->
+        <service id="jms_serializer.twig_extension.serializer" class="%jms_serializer.twig_extension.class%" public="false">
+            <argument type="service" id="serializer" />
+            <tag name="twig.extension" />
+        </service>
+
         <!-- Visitors -->
         <service id="jms_serializer.json_serialization_visitor" class="%jms_serializer.json_serialization_visitor.class%" public="false">
             <argument type="service" id="jms_serializer.naming_strategy" />

+ 7 - 0
Resources/doc/index.rst

@@ -116,6 +116,13 @@ De-/Serializing Objects
 The serializer supports JSON, and XML out-of-the-box, and can also handle
 many custom XML features (see below).
 
+The serializer can also be accessed via a Twig filter and will default to
+"json".
+
+::
+    {{ myObject | serialize | raw }}
+    {{ myObject | serialize('xml') | raw }}
+
 Versioning
 ~~~~~~~~~~
 

+ 41 - 0
Tests/Twig/SerializerExtensionTest.php

@@ -0,0 +1,41 @@
+<?php
+
+/*
+ * Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+namespace JMS\SerializerBundle\Tests\Twig;
+
+use JMS\SerializerBundle\Serializer\SerializerInterface;
+use JMS\SerializerBundle\Twig\SerializerExtension;
+
+class SerializerExtensionTest extends \PHPUnit_Framework_TestCase
+{
+    public function setUp()
+    {
+        $this->mockSerializer = $this->getMock('JMS\SerializerBundle\Serializer\SerializerInterface');
+    }
+
+    public function testSerialize()
+    {
+        $obj = new \stdClass();
+        $this->mockSerializer
+            ->expects($this->once())
+            ->method('serialize')
+            ->with($this->equalTo($obj), $this->equalTo('json'));
+        $serializerExtension = new SerializerExtension($this->mockSerializer);
+        $serializerExtension->serialize($obj);
+    }
+}

+ 53 - 0
Twig/SerializerExtension.php

@@ -0,0 +1,53 @@
+<?php
+/*
+ * Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+namespace JMS\SerializerBundle\Twig;
+
+use JMS\SerializerBundle\Serializer\SerializerInterface;
+
+
+/**
+ * Serializer helper twig extension
+ * 
+ * Basically provides access to JMSSerializer from Twig
+ */
+class SerializerExtension extends \Twig_Extension 
+{
+    protected $serializer;
+
+    public function getName() 
+    {
+        return 'jms_serializer';
+    }
+
+    public function __construct(SerializerInterface $serializer)
+    {
+        $this->serializer = $serializer;
+    }
+
+    public function getFilters() 
+    {
+        return array(
+            'serialize'      => new \Twig_Filter_Method($this, 'serialize'),
+        );
+    }
+
+    public function serialize($object, $type = 'json') 
+    {
+        return $this->serializer->serialize($object, $type);
+    }
+}