Skip to content
This repository was archived by the owner on Sep 16, 2021. It is now read-only.

Commit ed06b85

Browse files
committed
Refactored collection handling
1 parent 88364ee commit ed06b85

File tree

6 files changed

+151
-8
lines changed

6 files changed

+151
-8
lines changed

Resources/config/serializer.xml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
66

77
<parameters>
8-
<parameter key="cmf_resource_rest.serializer.handler.collection.class">Symfony\Cmf\Bundle\ResourceRestBundle\Serializer\Handler\CollectionHandler</parameter>
8+
<parameter key="cmf_resource_rest.serializer.handler.resource_collection.class">Symfony\Cmf\Bundle\ResourceRestBundle\Serializer\Handler\ResourceCollectionHandler</parameter>
99
<parameter key="cmf_resource_rest.serializer.handler.phpcr_node.class">Symfony\Cmf\Bundle\ResourceRestBundle\Serializer\Handler\PhpcrNodeHandler</parameter>
1010
<parameter key="cmf_resource_rest.serializer.subscriber.phpcr_node.class">Symfony\Cmf\Bundle\ResourceRestBundle\Serializer\EventSubscriber\PhpcrNodeSubscriber</parameter>
11+
<parameter key="cmf_resource_rest.serializer.subscriber.resource_collection.class">Symfony\Cmf\Bundle\ResourceRestBundle\Serializer\EventSubscriber\ResourceCollectionSubscriber</parameter>
1112
</parameters>
1213

1314
<services>
1415

15-
<service id="cmf_resource_rest.serializer.handler.collection" class="%cmf_resource_rest.serializer.handler.collection.class%">
16+
<service id="cmf_resource_rest.serializer.handler.resource_collection" class="%cmf_resource_rest.serializer.handler.resource_collection.class%">
1617
<tag name="jms_serializer.subscribing_handler"/>
1718
</service>
1819

@@ -23,5 +24,9 @@
2324
<service id="cmf_resource_rest.serializer.subscriber.phpcr_node" class="%cmf_resource_rest.serializer.subscriber.phpcr_node.class%">
2425
<tag name="jms_serializer.event_subscriber" />
2526
</service>
27+
28+
<service id="cmf_resource_rest.serializer.subscriber.resource_collection" class="%cmf_resource_rest.serializer.subscriber.resource_collection.class%">
29+
<tag name="jms_serializer.event_subscriber" />
30+
</service>
2631
</services>
2732
</container>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony CMF package.
5+
*
6+
* (c) 2011-2014 Symfony CMF
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Cmf\Bundle\ResourceRestBundle\Serializer\EventSubscriber;
13+
14+
use JMS\Serializer\EventDispatcher\EventSubscriberInterface;
15+
use PHPCR\NodeInterface;
16+
use JMS\Serializer\EventDispatcher\Events;
17+
use JMS\Serializer\EventDispatcher\PreSerializeEvent;
18+
use Puli\Repository\Api\ResourceCollection;
19+
20+
/**
21+
* Force instaces of ResourceCollection to type "ResourceCollection"
22+
*
23+
* @author Daniel Leech <[email protected]>
24+
*/
25+
class ResourceCollectionSubscriber implements EventSubscriberInterface
26+
{
27+
public static function getSubscribedEvents()
28+
{
29+
return array(
30+
array(
31+
'event' => Events::PRE_SERIALIZE,
32+
'method' => 'onPreSerialize',
33+
),
34+
);
35+
}
36+
37+
public function onPreSerialize(PreSerializeEvent $event)
38+
{
39+
$object = $event->getObject();
40+
41+
if ($object instanceof ResourceCollection) {
42+
$event->setType('Puli\Repository\Api\ResourceCollection');
43+
}
44+
}
45+
}

Serializer/Handler/PhpcrNodeHandler.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ public static function getSubscribingMethods()
3636
);
3737
}
3838

39+
/**
40+
* @param JsonSerializationVisitor $visitor
41+
* @param NodeInterface $nodeInterface
42+
* @param array $type
43+
* @param Context $context
44+
*/
3945
public function serializePhpcrNode(
4046
JsonSerializationVisitor $visitor,
4147
NodeInterface $node,

Serializer/Handler/CollectionHandler.php renamed to Serializer/Handler/ResourceCollectionHandler.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,40 @@
1919
use JMS\Serializer\Context;
2020

2121
/**
22-
* Normalize Collection objects to flat arrays
22+
* Serialize ResourceCollection instances into flat arrays
2323
*
2424
* @author Daniel Leech <[email protected]>
2525
*/
26-
class CollectionHandler implements SubscribingHandlerInterface
26+
class ResourceCollectionHandler implements SubscribingHandlerInterface
2727
{
2828
public static function getSubscribingMethods()
2929
{
3030
return array(
3131
array(
3232
'event' => GraphNavigator::DIRECTION_SERIALIZATION,
3333
'format' => 'json',
34-
'type' => 'Puli\Repository\Resource\Collection\ArrayResourceCollection',
35-
'method' => 'normalizeCollection',
34+
'type' => 'Puli\Repository\Api\ResourceCollection',
35+
'method' => 'serializeCollection',
3636
),
3737
);
3838
}
3939

40-
public function normalizeCollection(
40+
/**
41+
* @param JsonSerializationVisitor $visitor
42+
* @param ResourceCollection $resourceCollection
43+
* @param array $type
44+
* @param Context $context
45+
*/
46+
public function serializeCollection(
4147
JsonSerializationVisitor $visitor,
4248
ResourceCollection $collection,
4349
array $type,
4450
Context $context
4551
) {
46-
$res = $visitor->visitarray($collection->toArray(), array('name' => 'Symfony\Cmf\Component\Resource\Repository\Resource\PhpcrOdmResource'), $context);
52+
$res = array();
53+
foreach ($collection as $resource) {
54+
$res[$resource->getName()] = $context->accept($resource);
55+
}
4756

4857
return $res;
4958
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony CMF package.
5+
*
6+
* (c) 2011-2014 Symfony CMF
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Cmf\Bundle\ResourceRestBundle\Tests\Serializer\EventSubscriber;
13+
14+
use Prophecy\PhpUnit\ProphecyTestCase;
15+
use Symfony\Cmf\Bundle\ResourceRestBundle\Serializer\EventSubscriber\PhpcrNodeSubscriber;
16+
17+
class PhpcrNodeSubscriberTest extends ProphecyTestCase
18+
{
19+
private $node;
20+
private $event;
21+
private $subscriber;
22+
23+
public function setUp()
24+
{
25+
parent::setUp();
26+
27+
$this->node = $this->prophesize('PHPCR\NodeInterface');
28+
$this->event = $this->prophesize('JMS\Serializer\EventDispatcher\PreSerializeEvent');
29+
$this->subscriber = new PhpcrNodeSubscriber();
30+
}
31+
32+
public function testPreSerialize()
33+
{
34+
$this->event->getObject()->willReturn($this->node->reveal());
35+
$this->event->setType('PHPCR\NodeInterface')->shouldBeCalled();
36+
$this->subscriber->onPreSerialize($this->event->reveal());
37+
}
38+
}
39+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony CMF package.
5+
*
6+
* (c) 2011-2014 Symfony CMF
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Cmf\Bundle\ResourceRestBundle\Tests\Serializer\EventSubscriber;
13+
14+
use Prophecy\PhpUnit\ProphecyTestCase;
15+
use Symfony\Cmf\Bundle\ResourceRestBundle\Serializer\EventSubscriber\ResourceCollectionSubscriber;
16+
17+
class ResourceCollectionSubscriberTest extends ProphecyTestCase
18+
{
19+
private $collection;
20+
private $event;
21+
private $subscriber;
22+
23+
public function setUp()
24+
{
25+
parent::setUp();
26+
27+
$this->collection = $this->prophesize('Puli\Repository\Resource\Collection\ArrayResourceCollection');
28+
$this->event = $this->prophesize('JMS\Serializer\EventDispatcher\PreSerializeEvent');
29+
$this->subscriber = new ResourceCollectionSubscriber();
30+
}
31+
32+
public function testPreSerialize()
33+
{
34+
$this->event->getObject()->willReturn($this->collection->reveal());
35+
$this->event->setType('Puli\Repository\Api\ResourceCollection')->shouldBeCalled();
36+
$this->subscriber->onPreSerialize($this->event->reveal());
37+
}
38+
}
39+

0 commit comments

Comments
 (0)