Skip to content

Commit

Permalink
Force service prefix in LoggerAbstractServiceFactory
Browse files Browse the repository at this point in the history
- To prevent collisions with other abstract factories, the logger
  abstract factory should require that, when requesting a service, the
  service name be prefixed. I chose "Logger", as that's the service type
  being requested.
- This complements the approach in zendframework#4253 for cache
  objects.
- Not a BC break, as the LoggerAbstractServiceFactory has not yet been
  released.
  • Loading branch information
weierophinney committed Apr 29, 2013
1 parent c4f2a2a commit 54150db
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
20 changes: 16 additions & 4 deletions library/Zend/Log/LoggerAbstractServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,18 @@ class LoggerAbstractServiceFactory implements AbstractFactoryInterface
*/
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{
$config = $serviceLocator->get('Config');
return isset($config['log'][$requestedName]);
if ('logger\\' != substr(strtolower($requestedName), 0, 7)) {
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]);
}

/**
Expand All @@ -39,7 +49,9 @@ public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator
*/
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{
$config = $serviceLocator->get('Config');
return new Logger($config['log'][$requestedName]);
$config = $serviceLocator->get('Config');
$config = array_change_key_case($config['log']);
$service = substr(strtolower($requestedName), 7);
return new Logger($config[$service]);
}
}
12 changes: 7 additions & 5 deletions tests/ZendTest/Log/LoggerAbstractServiceFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ protected function setUp()

$this->serviceManager->setService('Config', array(
'log' => array(
'Application\Frontend\Logger' => array(),
'Application\Backend\Logger' => array(),
'Application\Frontend' => array(),
'Application\Backend' => array(),
),
));
}
Expand All @@ -51,8 +51,8 @@ protected function setUp()
public function providerValidLoggerService()
{
return array(
array('Application\Frontend\Logger'),
array('Application\Backend\Logger'),
array('Logger\Application\Frontend'),
array('Logger\Application\Backend'),
);
}

Expand All @@ -62,7 +62,9 @@ public function providerValidLoggerService()
public function providerInvalidLoggerService()
{
return array(
array('Application\Unknown\Logger'),
array('Logger\Application\Unknown'),
array('Application\Frontend'),
array('Application\Backend'),
);
}

Expand Down

0 comments on commit 54150db

Please sign in to comment.