Created
May 28, 2013 12:22
-
-
Save docteurklein/5662375 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services: | |
ss.serializer.serializer_listener: | |
class: SerializerListener | |
arguments: | |
- '@router' # surely you want to inject liipimagine stuff | |
tags: | |
- { name: jms_serializer.event_listener, event: serializer.post_serialize, method: onPostSerialize, class: SuperObjectWithPath, format: json, direction: serialization } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Symfony\Bundle\FrameworkBundle\Routing\Router; | |
use JMS\Serializer\EventDispatcher\Event; | |
use Symfony\Component\Form\Util\PropertyPath; | |
class SerializerListener | |
{ | |
private $router; | |
private $map; | |
public function __construct(Router $router, array $map) | |
{ | |
$this->router = $router; | |
$this->map = $map; | |
} | |
public function onPostSerialize(Event $event) | |
{ | |
$object = $event->getObject(); | |
$config = $this->map[get_class($object)]; | |
$params = array_map(function($path) use ($object) { | |
$propertyPath = new PropertyPath($path); | |
return $propertyPath->getValue($object); | |
}, $config['params']); | |
$url = $this->router->generate($config['route'], $params); | |
$event->getVisitor()->addData('url', $url); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment