8000 Document how to use named aliases by greg0ire · Pull Request #11339 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

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

Merged
merged 1 commit into from
Apr 9, 2019

Conversation

greg0ire
Copy link
Contributor
@greg0ire greg0ire commented Apr 7, 2019

No description provided.

@wouterj wouterj added the ⭐️ EU-FOSSA Hackathon https://symfony.com/blog/the-symfony-and-api-platform-hackathon-is-coming label 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
Copy link
Contributor

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

Copy link
Contributor Author

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?

@greg0ire greg0ire force-pushed the document-named-aliases branch from 7f3216b to fdcea7a Compare April 7, 2019 09:56
@xabbuh xabbuh added this to the 4.2 milestone Apr 7, 2019
@greg0ire
Copy link
Contributor Author
greg0ire commented Apr 8, 2019

@Pierstoval @OskarStark please review again

@greg0ire
Copy link
Contributor Author
greg0ire 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
Copy link
Member

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.

# 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
Copy link
Member

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;
Copy link
Member

Choose a reason for hiding this comment

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

Alpha order


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

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.

Copy link
Contributor Author

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

Copy link
Contributor Author

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?

Copy link
Member

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

Copy link
Contributor Author

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

@greg0ire greg0ire force-pushed the document-named-aliases branch from 342421e to 2ef00d3 Compare April 9, 2019 06:21
@greg0ire greg0ire force-pushed the document-named-aliases branch from 2ef00d3 to c316951 Compare April 9, 2019 06:28
@wouterj
Copy link
Member
wouterj 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
wouterj added a commit that referenced this pull request Apr 9, 2019
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 document-named-aliases branch April 9, 2019 19:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DependencyInjection ⭐️ EU-FOSSA Hackathon https://symfony.com/blog/the-symfony-and-api-platform-hackathon-is-coming Status: Reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants
0