-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Document how to use named aliases #11339
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
Conversation
a35d20e
to
0ddb722
Compare
service_container/autowiring.rst
Outdated
|
||
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 |
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.
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
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.
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?
7f3216b
to
fdcea7a
Compare
fdcea7a
to
342421e
Compare
@Pierstoval @OskarStark please review again |
@tgalopin please review again |
service_container/autowiring.rst
Outdated
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 |
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.
name (no d)
But can we reformulate this using reverse logic? The code decides the name, configuration adapts to it.
service_container/autowiring.rst
Outdated
# 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 |
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.
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; |
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.
Alpha order
service_container/autowiring.rst
Outdated
|
||
// ... | ||
$container->autowire(Rot13Transformer::class); | ||
$container->autowire(UppercaseTransformer::class); | ||
$container->setAlias(TransformerInterface::class, Rot13Transformer::class); | ||
$container->registerAliasForArgument( |
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.
Using setAlias here might be more clear maybe? registerAliasForArgument would need some specific words for bundle author also.
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.
Oh I didn't know I could still use setAlias
here, I just had a look at the PR from @tgalopin about flysystem
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.
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?
There was a problem hiding this comment.
Choose a reason for hiding this com 67ED ment
The reason will be displayed to describe this comment to others. Learn more.
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
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.
Ok I replaced all this with setAlias
342421e
to
2ef00d3
Compare
2ef00d3
to
c316951
Compare
Great example, I like it! Thanks for writing these docs. |
This PR was merged into the 4.2 branch. Discussion ---------- Document how to use named aliases Commits ------- c316951 Document how to use named aliases
No description provided.