Skip to content

Commit

Permalink
Improve flexibility
Browse files Browse the repository at this point in the history
  • Loading branch information
neeckeloo committed Dec 5, 2014
1 parent fdd60bc commit ae8056f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
17 changes: 15 additions & 2 deletions src/Factory/LoggerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private function createHandler($handler)
));
}

$handlerPluginManager = $this->serviceLocator->get('MonologModule\Handler\HandlerPluginManager');
$handlerPluginManager = $this->getPluginManager('MonologModule\Handler\HandlerPluginManager');
$instance = $this->createInstanceFromParams($handler, $handlerPluginManager);

if (isset($handler['formatter'])) {
Expand All @@ -81,7 +81,7 @@ private function createFormatter($formatter)
));
}

$formatterPluginManager = $this->serviceLocator->get('MonologModule\Formatter\FormatterPluginManager');
$formatterPluginManager = $this->getPluginManager('MonologModule\Formatter\FormatterPluginManager');

return $this->createInstanceFromParams($formatter, $formatterPluginManager);
}
Expand Down Expand Up @@ -131,4 +131,17 @@ private function createProcessor($processor)
'Processor must be a callable or the FQCN of an invokable class'
);
}

/**
* @param string $name
* @return \Zend\ServiceManager\AbstractPluginManager
*/
protected function getPluginManager($name)
{
if (!$this->serviceLocator || !$this->serviceLocator->has($name)) {
return null;
}

return $this->serviceLocator->get($name);
}
}
34 changes: 32 additions & 2 deletions tests/Factory/LoggerFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public function testCreateLoggerWithoutName()
public function testCreateLoggerWithHandler()
{
$serviceLocator = $this->getMock('Zend\ServiceManager\ServiceLocatorInterface');
$serviceLocator
->expects($this->once())
->method('has')
->with('MonologModule\Handler\HandlerPluginManager')
->will($this->returnValue(true));
$serviceLocator
->expects($this->once())
->method('get')
Expand All @@ -59,6 +64,11 @@ public function testCreateLoggerWithHandler()
public function testCreateLoggerWithHandlerIncludingOptions()
{
$serviceLocator = $this->getMock('Zend\ServiceManager\ServiceLocatorInterface');
$serviceLocator
->expects($this->once())
->method('has')
->with('MonologModule\Handler\HandlerPluginManager')
->will($this->returnValue(true));
$serviceLocator
->expects($this->once())
->method('get')
Expand Down Expand Up @@ -122,11 +132,21 @@ public function testCreateLoggerWithHandlerAndFormatter()
$serviceLocator = $this->getMock('Zend\ServiceManager\ServiceLocatorInterface');
$serviceLocator
->expects($this->at(0))
->method('has')
->with('MonologModule\Handler\HandlerPluginManager')
->will($this->returnValue(true));
$serviceLocator
->expects($this->at(1))
->method('get')
->with('MonologModule\Handler\HandlerPluginManager')
->will($this->returnValue(null));
$serviceLocator
->expects($this->at(1))
->expects($this->at(2))
->method('has')
->with('MonologModule\Formatter\FormatterPluginManager')
->will($this->returnValue(true));
$serviceLocator
->expects($this->at(3))
->method('get')
->with('MonologModule\Formatter\FormatterPluginManager')
->will($this->returnValue(null));
Expand Down Expand Up @@ -158,11 +178,21 @@ public function testCreateLoggerWithHandlerAndFormatterIncludingOptions()
$serviceLocator = $this->getMock('Zend\ServiceManager\ServiceLocatorInterface');
$serviceLocator
->expects($this->at(0))
->method('has')
->with('MonologModule\Handler\HandlerPluginManager')
->will($this->returnValue(true));
$serviceLocator
->expects($this->at(1))
->method('get')
->with('MonologModule\Handler\HandlerPluginManager')
->will($this->returnValue(null));
$serviceLocator
->expects($this->at(1))
->expects($this->at(2))
->method('has')
->with('MonologModule\Formatter\FormatterPluginManager')
->will($this->returnValue(true));
$serviceLocator
->expects($this->at(3))
->method('get')
->with('MonologModule\Formatter\FormatterPluginManager')
->will($this->returnValue(null));
Expand Down

0 comments on commit ae8056f

Please sign in to comment.