-
Notifications
You must be signed in to change notification settings - Fork 146
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
Conversation
@@ -214,6 +214,8 @@ private function configureProviderAndClient(ContainerBuilder $container, $provid | |||
$clientDefinition->addMethodCall('setAsStateless'); | |||
} | |||
|
|||
$container->setAlias($clientClass, $clientServiceKey); |
There was a problem hiding this comment.
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));
}
}
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)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hasAlias()
instead. My bad :)
Boom! |
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