From f0de3393828243fdd000e09f915e9c858a19e2f4 Mon Sep 17 00:00:00 2001 From: Arjan van den Bos Date: Wed, 21 Sep 2016 14:02:32 +0200 Subject: [PATCH] Refactored the GelfHandlerFactory to be compatible with Zend ServiceManager v3 --- src/Handler/Factory/GelfHandlerFactory.php | 10 +++++++++- .../Handler/Factory/GelfHandlerFactoryTest.php | 17 +++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Handler/Factory/GelfHandlerFactory.php b/src/Handler/Factory/GelfHandlerFactory.php index 79c810e..1218603 100644 --- a/src/Handler/Factory/GelfHandlerFactory.php +++ b/src/Handler/Factory/GelfHandlerFactory.php @@ -19,13 +19,21 @@ class GelfHandlerFactory implements FactoryInterface /** * @param array $options */ - public function __construct(array $options) + public function __construct(array $options = []) { + // Zend ServiceManager v2 allows factory creationOptions $this->options = $options; } public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { + /** + * Avoid a BC break; Zend ServiceManager v2 will pass the options via the constructor, v3 to the __invoke() + */ + if (null !== $options) { + $this->options = array_merge($this->options, $options); + } + if (!isset($this->options['host'])) { throw new Exception\RuntimeException('Gelf handler needs a host value'); } diff --git a/tests/Handler/Factory/GelfHandlerFactoryTest.php b/tests/Handler/Factory/GelfHandlerFactoryTest.php index 119254d..7b350f5 100644 --- a/tests/Handler/Factory/GelfHandlerFactoryTest.php +++ b/tests/Handler/Factory/GelfHandlerFactoryTest.php @@ -21,6 +21,23 @@ public function testInstantiateGelfHandler() $this->assertInstanceOf(GelfHandler::class, $gelfHandler); } + public function testInstantiateGelfHandlerViaInvokeOptions() + { + $serviceLocator = $this->createMock(ServiceLocatorInterface::class); + + $gelfHandlerFactory = new GelfHandlerFactory(); + $gelfHandler = $gelfHandlerFactory->__invoke( + $serviceLocator, + GelfHandler::class, + [ + 'host' => 'domain.com', + 'port' => 123, + ] + ); + + $this->assertInstanceOf(GelfHandler::class, $gelfHandler); + } + /** * @expectedException \MonologModule\Exception\RuntimeException */