Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set an alias to every client service #62

Merged
merged 3 commits into from
Sep 27, 2017
Merged

Set an alias to every client service #62

merged 3 commits into from
Sep 27, 2017

Conversation

MolloKhan
Copy link
Contributor

@MolloKhan MolloKhan commented Sep 26, 2017

Fix #60

I feel like the service Id and alias name are inverted, but tests does not pass if I do it the other way around

@@ -214,6 +214,8 @@ private function configureProviderAndClient(ContainerBuilder $container, $provid
$clientDefinition->addMethodCall('setAsStateless');
}

$container->setAlias($clientClass, $clientServiceKey);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great! Two minor things:

$container->setAlias($clientClass, new Alias($clientServiceKey, false));

This will create a private alias... no reason for the user to need to fetch this service directly from the container with that id.

Second. There's one edge case: if someone (for some reason) configures two clients with the same provider (e.g. 2 facebook clients). In that case, the alias would be created twice, the second would override the first. It would be better to not create any alias in this case, so that the user receives an exception if they try to autowire it (it will be up to them to create an alias if they want to autowire).

How to do this easily? Let's add a new private property to the class: private $duplicateProviderTypes = array(). Then, we can do something like:

// add an alias, but only if a provider type is used only 1 time
if (!in_array($providerType, $this->duplicateProviderTypes)) {
    // alias already exists? This is a duplicate type, record it
    if ($container->getAlias($clientClass)) {
        $this->duplicateProviderTypes[] = $providerType;
    } else {
        // all good, add the alias
        $container->setAlias($clientClass, new Alias($clientServiceKey, false));
    }
}

@MolloKhan
Copy link
Contributor Author

Done!

// add an alias, but only if a provider type is used only 1 time
if (!in_array($providerType, $this->duplicateProviderTypes)) {
// alias already exists? This is a duplicate type, record it
if ($container->getAlias($clientClass)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hasAlias() instead. My bad :)

@weaverryan weaverryan merged commit 4eace79 into master Sep 27, 2017
@weaverryan weaverryan deleted the client-aliases branch September 27, 2017 17:08
@weaverryan
Copy link
Member

Boom!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants