/**
     * {@inheritDoc}
     */
    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        /** @var $options \JMSSerializerModule\Options\Handlers */
        $options = $this->getOptions($serviceLocator, 'handlers');
        $handlerRegistry = new HandlerRegistry();
        foreach ($options->getSubscribers() as $subscriberName) {
            $subscriber = $subscriberName;
            if (is_string($subscriber)) {
                if ($serviceLocator->has($subscriber)) {
                    $subscriber = $serviceLocator->get($subscriber);
                } elseif (class_exists($subscriber)) {
                    $subscriber = new $subscriber();
                }
            }
            if ($subscriber instanceof SubscribingHandlerInterface) {
                $handlerRegistry->registerSubscribingHandler($subscriber);
                continue;
            }
            throw new InvalidArgumentException(sprintf('Invalid subscriber "%s" given, must be a service name, ' . 'class name or an instance implementing JMS\\Serializer\\Handler\\SubscribingHandlerInterface;
', is_object($subscriberName) ? get_class($subscriberName) : (is_string($subscriberName) ? $subscriberName : gettype($subscriber))));
        }
        return $handlerRegistry;
    }
Exemplo n.º 2
0
 public function process(ContainerBuilder $container)
 {
     $handlers = array();
     foreach ($container->findTaggedServiceIds('jms_serializer.handler') as $id => $tags) {
         foreach ($tags as $attrs) {
             if (!isset($attrs['type'], $attrs['format'])) {
                 throw new \RuntimeException(sprintf('Each tag named "jms_serializer.handler" of service "%s" must have at least two attributes: "type" and "format".', $id));
             }
             $directions = array(GraphNavigator::DIRECTION_DESERIALIZATION, GraphNavigator::DIRECTION_SERIALIZATION);
             if (isset($attrs['direction'])) {
                 if (!defined($directionConstant = 'JMS\\Serializer\\GraphNavigator::DIRECTION_' . strtoupper($attrs['direction']))) {
                     throw new \RuntimeException(sprintf('The direction "%s" of tag "jms_serializer.handler" of service "%s" does not exist.', $attrs['direction'], $id));
                 }
                 $directions = array(constant($directionConstant));
             }
             foreach ($directions as $direction) {
                 $method = isset($attrs['method']) ? $attrs['method'] : HandlerRegistry::getDefaultMethod($direction, $attrs['type'], $attrs['format']);
                 $handlers[$direction][$attrs['type']][$attrs['format']] = array($id, $method);
             }
         }
     }
     foreach ($container->findTaggedServiceIds('jms_serializer.subscribing_handler') as $id => $tags) {
         $class = $container->getDefinition($id)->getClass();
         $ref = new \ReflectionClass($class);
         if (!$ref->implementsInterface('JMS\\Serializer\\Handler\\SubscribingHandlerInterface')) {
             throw new \RuntimeException(sprintf('The service "%s" must implement the SubscribingHandlerInterface.', $id));
         }
         foreach (call_user_func(array($class, 'getSubscribingMethods')) as $methodData) {
             if (!isset($methodData['format'], $methodData['type'])) {
                 throw new \RuntimeException(sprintf('Each method returned from getSubscribingMethods of service "%s" must have a "type", and "format" attribute.', $id));
             }
             $directions = array(GraphNavigator::DIRECTION_DESERIALIZATION, GraphNavigator::DIRECTION_SERIALIZATION);
             if (isset($methodData['direction'])) {
                 $directions = array($methodData['direction']);
             }
             foreach ($directions as $direction) {
                 $method = isset($methodData['method']) ? $methodData['method'] : HandlerRegistry::getDefaultMethod($direction, $methodData['type'], $methodData['format']);
                 $handlers[$direction][$methodData['type']][$methodData['format']] = array($id, $method);
             }
         }
     }
     $container->getDefinition('jms_serializer.handler_registry')->addArgument($handlers);
 }
 /**
  * @dataProvider getDateTime
  * @group datetime
  */
 public function testDateTimeNoCData($key, $value, $type)
 {
     $handlerRegistry = new HandlerRegistry();
     $handlerRegistry->registerSubscribingHandler(new DateHandler(\DateTime::ISO8601, 'UTC', false));
     $objectConstructor = new UnserializeObjectConstructor();
     $serializer = new Serializer($this->factory, $handlerRegistry, $objectConstructor, $this->serializationVisitors, $this->deserializationVisitors);
     $this->assertEquals($this->getContent($key . '_no_cdata'), $serializer->serialize($value, $this->getFormat()));
 }
 protected function registerHandler($type)
 {
     $this->handlerRegistry->registerHandler(GraphNavigator::DIRECTION_SERIALIZATION, $type, $this->getFormat(), array($this->handler, 'serializeEnumTo' . ucfirst($this->getFormat())));
 }
 public function registerHandler($direction, $typeName, $format, $handler)
 {
     parent::registerHandler($direction, $typeName, $format, $handler);
     unset($this->initializedHandlers[$direction][$typeName][$format]);
 }