/** * {@inheritdoc} */ protected function initializeListeners($eventName, $loweredClass, $format) { $listeners = parent::initializeListeners($eventName, $loweredClass, $format); foreach ($listeners as &$listener) { if (!is_array($listener) || !is_string($listener[0])) { continue; } if (!$this->container->has($listener[0])) { continue; } $listener[0] = $this->container->get($listener[0]); } return $listeners; }
/** * {@inheritDoc} */ public function createService(ServiceLocatorInterface $serviceLocator) { /** @var $options \JMSSerializerModule\Options\Handlers */ $options = $this->getOptions($serviceLocator, 'eventdispatcher'); $handlerRegistry = new EventDispatcher(); 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 EventSubscriberInterface) { $handlerRegistry->addSubscriber($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; }
public function process(ContainerBuilder $container) { $listeners = array(); foreach ($container->findTaggedServiceIds('mailjet.serializer.event_listener') as $id => $tags) { if (!$container->getDefinition($id)->isPublic()) { throw new \RuntimeException(sprintf('The tag "mailjet.serializer.event_listener" of service "%s" requires the service to be public.', $id)); } foreach ($tags as $attributes) { if (!isset($attributes['event'])) { throw new \RuntimeException(sprintf('The tag "mailjet.serializer.event_listener" of service "%s" requires an attribute named "event".', $id)); } $class = isset($attributes['class']) ? strtolower($attributes['class']) : null; $format = isset($attributes['format']) ? $attributes['format'] : null; $method = isset($attributes['method']) ? $attributes['method'] : EventDispatcher::getDefaultMethodName($attributes['event']); $priority = isset($attributes['priority']) ? (int) $attributes['priority'] : 0; $listeners[$attributes['event']][$priority][] = array(array($id, $method), $class, $format); } } foreach ($container->findTaggedServiceIds('mailjet.serializer.event_subscriber') as $id => $tags) { $subscriberDefinition = $container->getDefinition($id); $subscriberClass = $container->getDefinition($id)->getClass(); $subscriberClassReflectionObj = new \ReflectionClass($subscriberClass); if (!$subscriberClassReflectionObj->implementsInterface('JMS\\Serializer\\EventDispatcher\\EventSubscriberInterface')) { throw new \RuntimeException(sprintf('The service "%s" (class: %s) does not implement the EventSubscriberInterface.', $id, $subscriberClass)); } if (!$subscriberDefinition->isPublic()) { throw new \RuntimeException(sprintf('The tag "mailjet.serializer.event_listener" of service "%s" requires the service to be public.', $id)); } foreach (call_user_func(array($subscriberClass, 'getSubscribedEvents')) as $eventData) { if (!isset($eventData['event'])) { throw new \RuntimeException(sprintf('The service "%s" (class: %s) must return an event for each subscribed event.', $id, $subscriberClass)); } $class = isset($eventData['class']) ? strtolower($eventData['class']) : null; $format = isset($eventData['format']) ? $eventData['format'] : null; $method = isset($eventData['method']) ? $eventData['method'] : EventDispatcher::getDefaultMethodName($eventData['event']); $priority = isset($eventData['priority']) ? (int) $eventData['priority'] : 0; $listeners[$eventData['event']][$priority][] = array(array($id, $method), $class, $format); } } if ($listeners) { array_walk($listeners, function (&$value, $key) { ksort($value); }); foreach ($listeners as &$events) { $events = call_user_func_array('array_merge', $events); } $container->getDefinition('mailjet.serializer.event_dispatcher')->addMethodCall('setListeners', array($listeners)); } }
protected function setUp() { $this->factory = new MetadataFactory(new AnnotationDriver(new AnnotationReader())); $this->handlerRegistry = new HandlerRegistry(); $this->handlerRegistry->registerSubscribingHandler(new ConstraintViolationHandler()); $this->handlerRegistry->registerSubscribingHandler(new DateHandler()); $this->handlerRegistry->registerSubscribingHandler(new FormErrorHandler(new IdentityTranslator(new MessageSelector()))); $this->handlerRegistry->registerSubscribingHandler(new PhpCollectionHandler()); $this->handlerRegistry->registerSubscribingHandler(new ArrayCollectionHandler()); $this->handlerRegistry->registerHandler(GraphNavigator::DIRECTION_SERIALIZATION, 'AuthorList', $this->getFormat(), function (VisitorInterface $visitor, $object, array $type, Context $context) { return $visitor->visitArray(iterator_to_array($object), $type, $context); }); $this->handlerRegistry->registerHandler(GraphNavigator::DIRECTION_DESERIALIZATION, 'AuthorList', $this->getFormat(), function (VisitorInterface $visitor, $data, $type, Context $context) { $type = array('name' => 'array', 'params' => array(array('name' => 'integer', 'params' => array()), array('name' => 'JMS\\Serializer\\Tests\\Fixtures\\Author', 'params' => array()))); $elements = $visitor->getNavigator()->accept($data, $type, $context); $list = new AuthorList(); foreach ($elements as $author) { $list->add($author); } return $list; }); $this->dispatcher = new EventDispatcher(); $this->dispatcher->addSubscriber(new DoctrineProxySubscriber()); $namingStrategy = new SerializedNameAnnotationStrategy(new CamelCaseNamingStrategy()); $objectConstructor = new UnserializeObjectConstructor(); $this->serializationVisitors = new Map(array('json' => new JsonSerializationVisitor($namingStrategy), 'xml' => new XmlSerializationVisitor($namingStrategy), 'yml' => new YamlSerializationVisitor($namingStrategy))); $this->deserializationVisitors = new Map(array('json' => new JsonDeserializationVisitor($namingStrategy), 'xml' => new XmlDeserializationVisitor($namingStrategy))); $this->serializer = new Serializer($this->factory, $this->handlerRegistry, $objectConstructor, $this->serializationVisitors, $this->deserializationVisitors, $this->dispatcher); }