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

Commit

Permalink
Resolved conflicts, ran php-cs-fixer
Browse files Browse the repository at this point in the history
Created abstract factory for caches …
- Allows having multiple cache objects by implementing an abstract
  factory.
- When requesting a cache services, prefix with 'Cache\', but
  configuration omits that prefix for brevity.

Force service prefix in LoggerAbstractServiceFactory …
- 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/zf2zendframework/zf2#4253 for cache
  objects.
- Not a BC break, as the LoggerAbstractServiceFactory has not yet been
  released.

[zendframework/zendframework#4254] Remove unneeded annotations …
- per @samsonasik
  • Loading branch information
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
34 changes: 24 additions & 10 deletions src/LoggerAbstractServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,40 @@
class LoggerAbstractServiceFactory implements AbstractFactoryInterface
{
/**
* @param ServiceLocatorInterface $serviceLocator
* @param string $name
* @param string $requestedName
* @param ServiceLocatorInterface $serviceLocator
* @param string $name
* @param string $requestedName
* @return bool
*/
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]);
}

/**
* @param ServiceLocatorInterface $serviceLocator
* @param string $name
* @param string $requestedName
* @param ServiceLocatorInterface $serviceLocator
* @param string $name
* @param string $requestedName
* @return \Zend\Log\Logger
*/
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 test/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 c353346

Please sign in to comment.