Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Use prefix in Logger abstract factory #4254

Merged
merged 3 commits into from

3 participants

@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.
...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
@weierophinney 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
@weierophinney 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
@weierophinney weierophinney [#4254] Remove unneeded annotations 5230a7a
@mwillbanks mwillbanks referenced this pull request from a commit
@mwillbanks 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
@mwillbanks mwillbanks merged commit 5230a7a into zendframework:develop

1 check failed

Details default The Travis build failed
@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney [#4254] Remove unneeded annotations df8a00f
@ghost Unknown referenced this pull request from a commit
@mwillbanks 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
@ghost Unknown referenced this pull request from a commit
@mwillbanks mwillbanks Merge branch 'feature/4254' into develop
Close: #4254
553d60f
@gianarb gianarb referenced this pull request from a commit in zendframework/zend-log
@mwillbanks 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/zf2zendframework/zf2#4253 for cache
  objects.
- Not a BC break, as the LoggerAbstractServiceFactory has not yet been
  released.

[zendframework/zf2#4254] Remove unneeded annotations …
- per @samsonasik
c353346
@gianarb gianarb referenced this pull request from a commit in zendframework/zend-log
@mwillbanks mwillbanks Merge branch 'feature/4254' into develop 3fc0bdb
@weierophinney weierophinney referenced this pull request from a commit in zendframework/zend-cache
@weierophinney weierophinney [zendframework/zf2#4254] Remove unneeded annotations a84f6b7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 29, 2013
  1. @weierophinney

    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. @weierophinney

    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. @weierophinney
This page is out of date. Refresh to see the latest.
View
57 library/Zend/Cache/Service/StorageCacheAbstractFactory.php
@@ -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);
+ }
+}
View
20 library/Zend/Log/LoggerAbstractServiceFactory.php
@@ -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]);
}
}
View
71 tests/ZendTest/Cache/Service/StorageCacheAbstractFactoryTest.php
@@ -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'));
+ }
+}
View
12 tests/ZendTest/Log/LoggerAbstractServiceFactoryTest.php
@@ -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.