Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Use prefix in Logger abstract factory #4254

Merged
merged 3 commits into from

3 participants

Matthew Weier O'Phinney Abdul Malik Ikhsan Mike Willbanks
Matthew Weier O'Phinney

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/zf2#4253 for cache objects.
  • Not a BC break, as the LoggerAbstractServiceFactory has not yet been released.
...est/Cache/Service/StorageCacheAbstractFactoryTest.php
((5 lines not shown))
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_Cache
+ */
+
+namespace ZendTest\Cache\Service;
+
+use Zend\Cache;
+use Zend\ServiceManager\ServiceManager;
+
+/**
+ * @category Zend
+ * @package Zend_Cache
+ * @subpackage UnitTests
+ * @group Zend_Cache

only @group

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
...est/Cache/Service/StorageCacheAbstractFactoryTest.php
@@ -0,0 +1,75 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_Cache

remove @package

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
weierophinney added some commits
Matthew Weier O'Phinney weierophinney 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.
c4f2a2a
Matthew Weier O'Phinney weierophinney 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/zf2#4253 for cache
  objects.
- Not a BC break, as the LoggerAbstractServiceFactory has not yet been
  released.
54150db
Matthew Weier O'Phinney weierophinney [#4254] Remove unneeded annotations 5230a7a
Mike Willbanks mwillbanks referenced this pull request from a commit
Mike Willbanks mwillbanks Resolved conflicts, ran php-cs-fixer
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/zf2#4253 for cache
  objects.
- Not a BC break, as the LoggerAbstractServiceFactory has not yet been
  released.

[#4254] Remove unneeded annotations …
- per @samsonasik
0eb24d6
Mike Willbanks mwillbanks merged commit 5230a7a into from
Deleted user Unknown referenced this pull request from a commit
Matthew Weier O'Phinney weierophinney [#4254] Remove unneeded annotations df8a00f
Deleted user Unknown referenced this pull request from a commit
Mike Willbanks mwillbanks Resolved conflicts, ran php-cs-fixer
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/zf2#4253 for cache
  objects.
- Not a BC break, as the LoggerAbstractServiceFactory has not yet been
  released.

[#4254] Remove unneeded annotations …
- per @samsonasik
83ba84f
Deleted user Unknown referenced this pull request from a commit
Mike Willbanks mwillbanks Merge branch 'feature/4254' into develop
Close: #4254
553d60f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 29, 2013
  1. Matthew Weier O'Phinney

    Created abstract factory for caches

    weierophinney authored
    - 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.
  2. Matthew Weier O'Phinney

    Force service prefix in LoggerAbstractServiceFactory

    weierophinney authored
    - 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/zf2#4253 for cache
      objects.
    - Not a BC break, as the LoggerAbstractServiceFactory has not yet been
      released.
  3. Matthew Weier O'Phinney
This page is out of date. Refresh to see the latest.
57 library/Zend/Cache/Service/StorageCacheAbstractFactory.php
View
@@ -0,0 +1,57 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+
+namespace Zend\Cache\Service;
+
+use Zend\Cache\StorageFactory;
+use Zend\ServiceManager\AbstractFactoryInterface;
+use Zend\ServiceManager\ServiceLocatorInterface;
+
+/**
+ * Storage cache factory for multiple caches.
+ */
+class StorageCacheAbstractFactory implements AbstractFactoryInterface
+{
+ /**
+ * @param ServiceLocatorInterface $serviceLocator
+ * @param string $name
+ * @param string $requestedName
+ * @return bool
+ */
+ public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
+ {
+ if ('cache\\' !== substr(strtolower($requestedName), 0, 6)) {
+ return false;
+ }
+
+ $config = $serviceLocator->get('Config');
+ if (!isset($config['caches'])) {
+ return false;
+ }
+
+ $config = array_change_key_case($config['caches']);
+ $service = substr(strtolower($requestedName), 6);
+ return isset($config[$service]) && is_array($config[$service]);
+ }
+
+ /**
+ * @param ServiceLocatorInterface $serviceLocator
+ * @param string $name
+ * @param string $requestedName
+ * @return \Zend\Cache\Storage\StorageInterface
+ */
+ public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
+ {
+ $config = $serviceLocator->get('Config');
+ $config = array_change_key_case($config['caches']);
+ $service = substr(strtolower($requestedName), 6);
+ $cacheConfig = $config[$service];
+ return StorageFactory::factory($cacheConfig);
+ }
+}
20 library/Zend/Log/LoggerAbstractServiceFactory.php
View
@@ -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]);
}
/**
@@ -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]);
}
}
71 tests/ZendTest/Cache/Service/StorageCacheAbstractFactoryTest.php
View
@@ -0,0 +1,71 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+
+namespace ZendTest\Cache\Service;
+
+use Zend\Cache;
+use Zend\ServiceManager\ServiceManager;
+
+/**
+ * @group Zend_Cache
+ */
+class StorageCacheAbstractFactoryTest extends \PHPUnit_Framework_TestCase
+{
+ protected $sm;
+
+ public function setUp()
+ {
+ Cache\StorageFactory::resetAdapterPluginManager();
+ Cache\StorageFactory::resetPluginManager();
+ $this->sm = new ServiceManager();
+ $this->sm->setService('Config', array('caches' => array(
+ 'Memory' => array(
+ 'adapter' => 'Memory',
+ 'plugins' => array('Serializer', 'ClearExpiredByFactor'),
+ ),
+ 'invalid' => array(
+ 'adapter' => 'Memory',
+ 'plugins' => array('Serializer', 'ClearExpiredByFactor'),
+ ),
+ 'Foo' => array(
+ 'adapter' => 'Memory',
+ 'plugins' => array('Serializer', 'ClearExpiredByFactor'),
+ ),
+ )));
+ $this->sm->addAbstractFactory('Zend\Cache\Service\StorageCacheAbstractFactory');
+ }
+
+ public function tearDown()
+ {
+ Cache\StorageFactory::resetAdapterPluginManager();
+ Cache\StorageFactory::resetPluginManager();
+ }
+
+ public function testCanLookupCacheByName()
+ {
+ $this->assertTrue($this->sm->has('Cache\Memory'));
+ $this->assertTrue($this->sm->has('Cache\Foo'));
+ }
+
+ public function testCanRetrieveCacheByName()
+ {
+ $cacheA = $this->sm->get('Cache\Memory');
+ $this->assertInstanceOf('Zend\Cache\Storage\Adapter\Memory', $cacheA);
+
+ $cacheB = $this->sm->get('Cache\Foo');
+ $this->assertInstanceOf('Zend\Cache\Storage\Adapter\Memory', $cacheB);
+
+ $this->assertNotSame($cacheA, $cacheB);
+ }
+
+ public function testInvalidCacheServiceNameWillBeIgnored()
+ {
+ $this->assertFalse($this->sm->has('invalid'));
+ }
+}
12 tests/ZendTest/Log/LoggerAbstractServiceFactoryTest.php
View
@@ -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(),
),
));
}
@@ -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'),
);
}
@@ -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'),
);
}
Something went wrong with that request. Please try again.