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

Document how to use named aliases #11339

Merged
merged 1 commit into from Apr 9, 2019

Conversation

@greg0ire
Copy link
Contributor

commented Apr 7, 2019

No description provided.

Show resolved Hide resolved service_container/autowiring.rst Outdated
Show resolved Hide resolved service_container/autowiring.rst Outdated
Show resolved Hide resolved service_container/autowiring.rst Outdated

@greg0ire greg0ire force-pushed the greg0ire:document-named-aliases branch from a35d20e to 0ddb722 Apr 7, 2019


For instance, you may want to use by default the ``Rot13Transformer``
implementation when the ``TransformerInterface`` interface is type
hinted, but use the ``UppercaseTransformer`` implementation in some

This comment has been minimized.

Copy link
@Pierstoval

Pierstoval Apr 7, 2019

Contributor

I suggest you add a slight example of a class needing TransformerInterface and then explain the use-case with Rot13Transformer and UppercaseTransformer, for more conciseness

This comment has been minimized.

Copy link
@greg0ire

greg0ire Apr 7, 2019

Author Contributor

There is already such a class in the previous code (TwitterClient), and Rot13Transformer and UppercaseTransformer are both existing classes used as examples previously, I don't think they require more explanation than they did before this PR, do they?

Show resolved Hide resolved service_container/autowiring.rst Outdated

@greg0ire greg0ire force-pushed the greg0ire:document-named-aliases branch from 7f3216b to fdcea7a Apr 7, 2019

@xabbuh xabbuh added this to the 4.2 milestone Apr 7, 2019

Show resolved Hide resolved service_container/autowiring.rst
Show resolved Hide resolved service_container/autowiring.rst Outdated
Show resolved Hide resolved service_container/autowiring.rst Outdated
Show resolved Hide resolved service_container/autowiring.rst
Show resolved Hide resolved service_container/autowiring.rst Outdated

@greg0ire greg0ire force-pushed the greg0ire:document-named-aliases branch from fdcea7a to 342421e Apr 7, 2019

@greg0ire

This comment has been minimized.

Copy link
Contributor Author

commented Apr 8, 2019

@Pierstoval @OskarStark please review again

@greg0ire

This comment has been minimized.

Copy link
Contributor Author

commented Apr 9, 2019

@tgalopin please review again

specific cases. To do so, you can create a normal alias from the
``TransformerInterface`` interface to ``Rot13Transformer``, and then
create a *named alias* from a special string containing the interface
followed by a variable named you will have to use when doing the

This comment has been minimized.

Copy link
@nicolas-grekas

nicolas-grekas Apr 9, 2019

Member

name (no d)
But can we reformulate this using reverse logic? The code decides the name, configuration adapts to it.

# the ``App\Util\Rot13Transformer`` service will be injected when
# a ``App\Util\TransformerInterface`` type-hint is detected
# an ``App\Util\TransformerInterface`` type-hint is detected,
# but the argument name does not match that of a named alias

This comment has been minimized.

Copy link
@nicolas-grekas

nicolas-grekas Apr 9, 2019

Member

A negative statement might not be clear enough. What about splitting the comment on top of its configuration line also?

@@ -419,20 +459,33 @@ that alias:
use App\Util\UppercaseTransformer;
use App\Util\TransformerInterface;
use App\Service\TwitterClient;
use App\Service\MastodonClient;

This comment has been minimized.

Copy link
@nicolas-grekas

nicolas-grekas Apr 9, 2019

Member

Alpha order

// ...
$container->autowire(Rot13Transformer::class);
$container->autowire(UppercaseTransformer::class);
$container->setAlias(TransformerInterface::class, Rot13Transformer::class);
$container->registerAliasForArgument(

This comment has been minimized.

Copy link
@nicolas-grekas

nicolas-grekas Apr 9, 2019

Member

Using setAlias here might be more clear maybe? registerAliasForArgument would need some specific words for bundle author also.

This comment has been minimized.

Copy link
@greg0ire

greg0ire Apr 9, 2019

Author Contributor

Oh I didn't know I could still use setAlias here, I just had a look at the PR from @tgalopin about flysystem

This comment has been minimized.

Copy link
@greg0ire

greg0ire Apr 9, 2019

Author Contributor

So that would be setAlias(TransformerInterface::class . ' $shoutyTransformer', UppercaseTransformer::class), that's not so great is it? Should I put both, for the sake of completeness? Also, I'm not sure what this has to do with bundle authors?

This comment has been minimized.

Copy link
@nicolas-grekas

nicolas-grekas Apr 9, 2019

Member

Without the spaces yes. That's just more consistent with the other configuration formats for the example.
registerAliasForArgument embeds some logic that is of no use here.
See usage in FrameworkExtension also

This comment has been minimized.

Copy link
@greg0ire

greg0ire Apr 9, 2019

Author Contributor

Ok I replaced all this with setAlias

@greg0ire greg0ire force-pushed the greg0ire:document-named-aliases branch from 342421e to 2ef00d3 Apr 9, 2019

@greg0ire greg0ire force-pushed the greg0ire:document-named-aliases branch from 2ef00d3 to c316951 Apr 9, 2019

@wouterj

This comment has been minimized.

Copy link
Member

commented Apr 9, 2019

Great example, I like it! Thanks for writing these docs.

@wouterj wouterj merged commit c316951 into symfony:4.2 Apr 9, 2019

2 checks passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
symfony SymfonyCloud: Environment deployed
Details

wouterj added a commit that referenced this pull request Apr 9, 2019

feature #11339 Document how to use named aliases (greg0ire)
This PR was merged into the 4.2 branch.

Discussion
----------

Document how to use named aliases

Commits
-------

c316951 Document how to use named aliases

@greg0ire greg0ire deleted the greg0ire:document-named-aliases branch Apr 9, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.