Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'hotfix/abstract-manager-prefix-removal' of git://github.com/weierophinney/zf2 into weierophinney-hotfix/abstract-manager-prefix-removal
  • Loading branch information
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 24 deletions.
67 changes: 48 additions & 19 deletions src/LoggerAbstractServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
10 changes: 5 additions & 5 deletions test/LoggerAbstractServiceFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class LoggerAbstractServiceFactoryTeset extends \PHPUnit_Framework_TestCase
/**
* @var \Zend\ServiceManager\ServiceLocatorInterface
*/
private $serviceManager;
protected $serviceManager;

/**
* Set up LoggerAbstractServiceFactory and loggers configuration.
Expand All @@ -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'),
);
}

Expand All @@ -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'),
);
}

Expand Down

0 comments on commit a011068

Please sign in to comment.