| @@ 21-72 (lines=52) @@ | ||
| 18 | * |
|
| 19 | * @package Nnx\JmsSerializerModule\EventDispatcher |
|
| 20 | */ |
|
| 21 | class EventDispatcherFactory implements FactoryInterface, MutableCreationOptionsInterface |
|
| 22 | { |
|
| 23 | use MutableCreationOptionsTrait; |
|
| 24 | ||
| 25 | /** |
|
| 26 | * @inheritdoc |
|
| 27 | * |
|
| 28 | * @param ServiceLocatorInterface $serviceLocator |
|
| 29 | * |
|
| 30 | * @return EventDispatcher |
|
| 31 | * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException |
|
| 32 | * @throws \Nnx\JmsSerializerModule\EventDispatcher\Exception\RuntimeException |
|
| 33 | */ |
|
| 34 | public function createService(ServiceLocatorInterface $serviceLocator) |
|
| 35 | { |
|
| 36 | $eventDispatcher = new EventDispatcher(); |
|
| 37 | $creationOptions = $this->getCreationOptions(); |
|
| 38 | ||
| 39 | $subscribers = []; |
|
| 40 | if (array_key_exists('subscribers', $creationOptions)) { |
|
| 41 | if (!is_array($creationOptions['subscribers'])) { |
|
| 42 | $errMsg = 'Subscribers for handler registry is not array'; |
|
| 43 | throw new Exception\RuntimeException($errMsg); |
|
| 44 | } |
|
| 45 | $subscribers = $creationOptions['subscribers']; |
|
| 46 | } |
|
| 47 | ||
| 48 | foreach ($subscribers as $subscriberName) { |
|
| 49 | $subscriber = null; |
|
| 50 | if (is_string($subscriberName)) { |
|
| 51 | if ($serviceLocator->has($subscriberName)) { |
|
| 52 | $subscriber = $serviceLocator->get($subscriberName); |
|
| 53 | } elseif (class_exists($subscriberName)) { |
|
| 54 | $r = new ReflectionClass($subscriberName); |
|
| 55 | $subscriber = $r->newInstance(); |
|
| 56 | } |
|
| 57 | } |
|
| 58 | ||
| 59 | if (!$subscriber instanceof EventSubscriberInterface) { |
|
| 60 | $errMsg = sprintf( |
|
| 61 | 'Subscriber of type %s is invalid; must implement %s', |
|
| 62 | (is_object($subscriber) ? get_class($subscriber) : gettype($subscriber)), |
|
| 63 | EventSubscriberInterface::class |
|
| 64 | ); |
|
| 65 | throw new Exception\RuntimeException($errMsg); |
|
| 66 | } |
|
| 67 | $eventDispatcher->addSubscriber($subscriber); |
|
| 68 | } |
|
| 69 | ||
| 70 | return $eventDispatcher; |
|
| 71 | } |
|
| 72 | } |
|
| 73 | ||
| @@ 21-71 (lines=51) @@ | ||
| 18 | * |
|
| 19 | * @package Nnx\JmsSerializerModule\HandlerRegistry |
|
| 20 | */ |
|
| 21 | class HandlerRegistryFactory implements FactoryInterface, MutableCreationOptionsInterface |
|
| 22 | { |
|
| 23 | use MutableCreationOptionsTrait; |
|
| 24 | ||
| 25 | /** |
|
| 26 | * @param ServiceLocatorInterface $serviceLocator |
|
| 27 | * |
|
| 28 | * @return HandlerRegistry |
|
| 29 | * @throws \Nnx\JmsSerializerModule\HandlerRegistry\Exception\RuntimeException |
|
| 30 | * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException |
|
| 31 | * @throws \Nnx\JmsSerializerModule\MetadataDriver\Exception\RuntimeException |
|
| 32 | */ |
|
| 33 | public function createService(ServiceLocatorInterface $serviceLocator) |
|
| 34 | { |
|
| 35 | $handlerRegistry = new HandlerRegistry(); |
|
| 36 | $creationOptions = $this->getCreationOptions(); |
|
| 37 | ||
| 38 | $subscribers = []; |
|
| 39 | if (array_key_exists('subscribers', $creationOptions)) { |
|
| 40 | if (!is_array($creationOptions['subscribers'])) { |
|
| 41 | $errMsg = 'Subscribers for handler registry is not array'; |
|
| 42 | throw new Exception\RuntimeException($errMsg); |
|
| 43 | } |
|
| 44 | $subscribers = $creationOptions['subscribers']; |
|
| 45 | } |
|
| 46 | ||
| 47 | foreach ($subscribers as $subscriberName) { |
|
| 48 | $subscriber = null; |
|
| 49 | if (is_string($subscriberName)) { |
|
| 50 | if ($serviceLocator->has($subscriberName)) { |
|
| 51 | $subscriber = $serviceLocator->get($subscriberName); |
|
| 52 | } elseif (class_exists($subscriberName)) { |
|
| 53 | $r = new ReflectionClass($subscriberName); |
|
| 54 | $subscriber = $r->newInstance(); |
|
| 55 | } |
|
| 56 | } |
|
| 57 | ||
| 58 | if (!$subscriber instanceof SubscribingHandlerInterface) { |
|
| 59 | $errMsg = sprintf( |
|
| 60 | 'Subscriber of type %s is invalid; must implement %s', |
|
| 61 | (is_object($subscriber) ? get_class($subscriber) : gettype($subscriber)), |
|
| 62 | SubscribingHandlerInterface::class |
|
| 63 | ); |
|
| 64 | throw new Exception\RuntimeException($errMsg); |
|
| 65 | } |
|
| 66 | $handlerRegistry->registerSubscribingHandler($subscriber); |
|
| 67 | } |
|
| 68 | ||
| 69 | return $handlerRegistry; |
|
| 70 | } |
|
| 71 | } |
|
| 72 | ||