Skip to content

Commit

Permalink
Merge pull request #4 from arjanvdbos/fix-deprecated-factory-constructor
Browse files Browse the repository at this point in the history
Fix GelfHandlerFactory to be compatible with ServiceManager v3
  • Loading branch information
neeckeloo committed Sep 22, 2016
2 parents 237c9ac + f0de339 commit 25bec64
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Handler/Factory/GelfHandlerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
17 changes: 17 additions & 0 deletions tests/Handler/Factory/GelfHandlerFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit 25bec64

Please sign in to comment.