Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Get an abstract defined service from an alias #5752

Closed
wants to merge 2 commits into from

4 participants

@jmleroux

When you want to get a service leading to to an abstract factory from an alias.

I ended up with this issue when trying to get a cache instance wich i defined in my configuration :

return array(
    'service_manager' => array(
        'abstract_factories' => array(
            'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
        ),
        'aliases' => array (
            'app_cache' => 'memcached',
        ),
    ),
    'caches' => array(
        'memcached' => array(
            'adapter' => array(
                'name' => 'memcached',
                // other config values...
            ),
        ),
    ),
)

The purpose is to be able to change the cache by changing the alias.

Without this PR, ZF try to create this service :

$this->canCreateFromAbstractFactory('memcached', 'cache_core');

but 'cache_core' is not a cache configuration key.

@jmleroux jmleroux Get an abstract defined service from an alias
When you want to get a service leading to to an abstract factory from an alias.
ac1f8c1
@blanchonvincent

Hey @jmleroux, can you provide unit tests ?

@jmleroux

I can't run ServiceManager test group.
I can run all groups, but it's a bit long to run to validate my tests.

How can i run ServiceManager group ?

My try :

php run-tests.php -g ZendTest/ServiceManager
@jmleroux

Note : we would not have this problem without the $name in the method canCreateServiceWithName
see #5755

@Ocramius
Collaborator

This is not supported. I simply disallow service aliases for DoctrineModule's services, for example.

This is fixed with #5500 and won't land in 2.x

@jmleroux

OK, i'll do without.
I there a link available to an article or discussion explaining why removing aliases ?

I used them a few times and found them useful

@Ocramius
Collaborator

@jmleroux not removing them, just pushing them out as "non-core" feature - they work exactly like before

Aliases and normalization make work for abstract factories a real pain. The combination of the two is even worse :-)

@jmleroux

OK for ZF3.

But my fix is a simple one and makes aliases work with abstract factory.
Why not considering it until the release of ZF3 ?

@jmleroux

I see that the problem has already been raised : #5535
But my fix is way more simple.

@Ocramius
Collaborator

Indeed, this solution is way simpler

@jmleroux

ping @Ocramius
Some news about this PR ?
Just to know if I can rely on "aliased abstract factories" before ZF3 ?

@Ocramius
Collaborator

@jmleroux the PR looks good to me

@jmleroux

Any change to see it merged ?

chat-potte

@weierophinney weierophinney added this to the 2.2.6 milestone
@weierophinney weierophinney self-assigned this
@weierophinney weierophinney referenced this pull request from a commit
@weierophinney weierophinney [#5752] CS fixes
- multiline comment formatting, trailing whitespace
5877f43
@weierophinney weierophinney closed this pull request from a commit
@weierophinney weierophinney Merge branch 'hotfix/5752'
Close #5752
Fixes #5535
1ef51f6
@weierophinney weierophinney referenced this pull request from a commit
@weierophinney weierophinney Merge branch 'hotfix/5752' into develop
Forward port #5752
ea8fdd1
@jmleroux jmleroux deleted the jmleroux:patch-2 branch
@weierophinney weierophinney referenced this pull request from a commit in zendframework/zend-servicemanager
@weierophinney weierophinney Merge pull request zendframework/zf2#5752 from jmleroux/patch-2
Get an abstract defined service from an alias
13d474d
@weierophinney weierophinney referenced this pull request from a commit in zendframework/zend-servicemanager
@weierophinney weierophinney [zendframework/zf2#5752] CS fixes
- multiline comment formatting, trailing whitespace
b352d58
@weierophinney weierophinney referenced this pull request from a commit in zendframework/zend-servicemanager
@weierophinney weierophinney Merge branch 'hotfix/5752' f848448
@weierophinney weierophinney referenced this pull request from a commit in zendframework/zend-servicemanager
@weierophinney weierophinney Merge branch 'hotfix/5752' into develop fa1a9da
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 27, 2014
  1. @jmleroux

    Get an abstract defined service from an alias

    jmleroux authored
    When you want to get a service leading to to an abstract factory from an alias.
Commits on Jan 28, 2014
  1. @jmleroux
This page is out of date. Refresh to see the latest.
View
6 library/Zend/ServiceManager/ServiceManager.php
@@ -501,6 +501,12 @@ public function get($name, $usePeeringServiceManagers = true)
|| $this->canCreateFromAbstractFactory($cName, $name)
) {
$instance = $this->create(array($cName, $name));
+ } elseif ($isAlias && $this->canCreateFromAbstractFactory($name, $cName)) {
+ // case of an alias leading to an abstract factory :
+ // 'my-alias' => 'my-abstract-defined-service'
+ // $name = 'my-alias'
+ // $cName = 'my-abstract-defined-service'
+ $instance = $this->create(array($name, $cName));
} elseif ($usePeeringServiceManagers && !$this->retrieveFromPeeringManagerFirst) {
$instance = $this->retrieveFromPeeringManager($name);
}
View
10 tests/ZendTest/ServiceManager/ServiceManagerTest.php
@@ -243,6 +243,16 @@ public function testGetWithAlias()
/**
* @covers Zend\ServiceManager\ServiceManager::get
*/
+ public function testGetAbstractFactoryWithAlias()
+ {
+ $this->serviceManager->addAbstractFactory('ZendTest\ServiceManager\TestAsset\FooAbstractFactory');
+ $this->serviceManager->setAlias('foo', 'ZendTest\ServiceManager\TestAsset\FooAbstractFactory');
+ $this->assertInstanceOf('ZendTest\ServiceManager\TestAsset\Foo', $this->serviceManager->get('foo'));
+ }
+
+ /**
+ * @covers Zend\ServiceManager\ServiceManager::get
+ */
public function testGetWithScopedContainer()
{
$this->serviceManager->setService('foo', 'bar');
Something went wrong with that request. Please try again.