diff --git a/src/LoggerAbstractServiceFactory.php b/src/LoggerAbstractServiceFactory.php index 56f389e3..ccb4c648 100644 --- a/src/LoggerAbstractServiceFactory.php +++ b/src/LoggerAbstractServiceFactory.php @@ -20,40 +20,69 @@ class LoggerAbstractServiceFactory implements AbstractFactoryInterface { /** - * @param ServiceLocatorInterface $serviceLocator + * @var array + */ + protected $config; + + /** + * Configuration key holding logger configuration + * + * @var string + */ + protected $configKey = 'log'; + + /** + * @param ServiceLocatorInterface $services * @param string $name * @param string $requestedName * @return bool */ - public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) + public function canCreateServiceWithName(ServiceLocatorInterface $services, $name, $requestedName) { - if ('logger\\' != substr(strtolower($requestedName), 0, 7)) { + $config = $this->getConfig($services); + if (empty($config)) { return false; } - $config = $serviceLocator->get('Config'); - if (!isset($config['log'])) { - return false; - } - - $config = array_change_key_case($config['log']); - $service = substr(strtolower($requestedName), 7); - - return isset($config[$service]); + return isset($config[$requestedName]); } /** - * @param ServiceLocatorInterface $serviceLocator + * @param ServiceLocatorInterface $services * @param string $name * @param string $requestedName - * @return \Zend\Log\Logger + * @return Logger */ - public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) + public function createServiceWithName(ServiceLocatorInterface $services, $name, $requestedName) { - $config = $serviceLocator->get('Config'); - $config = array_change_key_case($config['log']); - $service = substr(strtolower($requestedName), 7); + $config = $this->getConfig($services); + return new Logger($config[$requestedName]); + } + + /** + * Retrieve configuration for loggers, if any + * + * @param ServiceLocatorInterface $services + * @return array + */ + protected function getConfig(ServiceLocatorInterface $services) + { + if ($this->config !== null) { + return $this->config; + } + + if (!$services->has('Config')) { + $this->config = array(); + return $this->config; + } + + $config = $services->get('Config'); + if (!isset($config[$this->configKey])) { + $this->config = array(); + return $this->config; + } - return new Logger($config[$service]); + $this->config = $config[$this->configKey]; + return $this->config; } } diff --git a/test/LoggerAbstractServiceFactoryTest.php b/test/LoggerAbstractServiceFactoryTest.php index eea941e8..111b808b 100644 --- a/test/LoggerAbstractServiceFactoryTest.php +++ b/test/LoggerAbstractServiceFactoryTest.php @@ -24,7 +24,7 @@ class LoggerAbstractServiceFactoryTeset extends \PHPUnit_Framework_TestCase /** * @var \Zend\ServiceManager\ServiceLocatorInterface */ - private $serviceManager; + protected $serviceManager; /** * Set up LoggerAbstractServiceFactory and loggers configuration. @@ -51,8 +51,8 @@ protected function setUp() public function providerValidLoggerService() { return array( - array('Logger\Application\Frontend'), - array('Logger\Application\Backend'), + array('Application\Frontend'), + array('Application\Backend'), ); } @@ -63,8 +63,8 @@ public function providerInvalidLoggerService() { return array( array('Logger\Application\Unknown'), - array('Application\Frontend'), - array('Application\Backend'), + array('Logger\Application\Frontend'), + array('Application\Backend\Logger'), ); }