Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Autowiring] Add interface FQCN as a valid service name to avoid collisions#21132

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed

Conversation

@theofidry
Copy link
Contributor

QA
Branch?master? (not sure)
Bug fix?yes/no (not sure)
New feature?yes
BC breaks?possibly
Deprecations?no
Tests pass?yes
Fixed ticketsnone
LicenseMIT
Doc PRTODO

As of now, if you have:

interface CollisionInterface {}class Aimplements CollisionInterface {}class UseCollision{function__construct(CollisionInterface$collision) {}}
services:app.a:class:Aautowire:trueapp.b:class:ACollisionInterface:'@app.b'app.use_collision:class:UseCollisionautowire:true

It would fail, because you haveapp.a andapp.b which are both implementingCollisionInterface. However as you can see, in this scenario this is a bit stupid, as if you bother doingCollisionInterface: '@app.b', this is equivalent to (or rather should be IMO) have:

services:#...app.b:class:Aautowired_types:[ CollisionInterface ]

You might wonder what's the diff and why bother yourself with that, the diff IMO is the location of your service definitions. For example I may try to follow a more DDD oriented architecture, e.g. the hexagonal architecture, where I have:

config/  domain.yml # service definitions related to the domain layer  infrastructure.yml # service definitions related to the infrastructure layersrc/  Domain/    CollisionInterface.php  Infrastructure/    A.php    UseCollision.php

and try to have something like:

# config/domain.ymlservices:App\Domain\CollisionInterface:'@app.b'
# config/infrastructure.ymlapp.a:class:Aautowire:trueapp.b:class:Aapp.use_collision:class:UseCollisionautowire:true

I am not sure if this can be caused any BC and if this should be considered as a feature or a bugfix.

/cc@dunglas

@GuilhemN
Copy link
Contributor

Sounds hard to support without having a dedicated system inContainerBuilder. For example we can't support the following using the current autowiring types system as the service is not yet defined:

services:CollisionInterface:'@app.b'app.a:class:Aautowire:trueapp.b:class:Aapp.use_collision:class:UseCollisionautowire:true

@nicolas-grekasnicolas-grekas added this to the3.x milestoneJan 2, 2017
@dunglas
Copy link
Member

Shouldn't we deprecate theautowiring_types key to only use the `CollisionInterface: '@app.b' syntax now?

privatefunctioncreateAutowiredDefinition(\ReflectionClass$typeHint,$id)
{
if (isset($this->ambiguousServiceTypes[$typeHint->name])) {
if (isset($this->ambiguousServiceTypes[$typeHintName =$typeHint->name])) {
Copy link
Member

Choose a reason for hiding this comment

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

Why is necessary to create the$typeHintName variable? It makes the code harder to read for no benefit over using the prop directly.

{
$container =newContainerBuilder();

$container->register('c1',__NAMESPACE__.'\CollisionA');
Copy link
Member

Choose a reason for hiding this comment

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

You can useCollisionA::class for Symfony 3+. Same of lines below.

@theofidry
Copy link
ContributorAuthor

Shouldn't we deprecate the autowiring_types key to only use the `CollisionInterface: '@app.b' syntax now?

I'm for it, but should it be done here or another PR? Also should this be merged in master or an older branch?

@theofidry
Copy link
ContributorAuthor

theofidry commentedJan 3, 2017
edited
Loading

@GuilhemN I'm not sure to understand your concern, all services are registered being process that way so the order does not matter.

@GuilhemN
Copy link
Contributor

GuilhemN commentedJan 3, 2017
edited
Loading

@theofidry oops, I didn't realize it was a PR and I was thinking you'll try to implement it otherwise... 😐

LGTM 👍 I'm also for deprecating theautowiring_types key.

$matchingServices =implode(',',$this->ambiguousServiceTypes[$typeHint->name]);

thrownewRuntimeException(sprintf('Unable to autowire argument of type "%s" for the service "%s". Multiple services exist for this %s (%s).',$typeHint->name,$id,$classOrInterface,$matchingServices),1);
thrownewRuntimeException(sprintf('Unable to autowire argument of type "%s" for the service "%s". Multiple services exist for this %s (%s).',$typeHint->name,$id,$classOrInterface,$matchingServices));
Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

needs to be reverted

@theofidry
Copy link
ContributorAuthor

@dunglas updated, but I'm still unsure on which branch we should merge this as if we want to deprecateautowiring_types, it would need to be merged on the oldest possible, as you cannot do withoutautowiring_types without this PR.

@nicolas-grekas
Copy link
Member

the deprecation part is for master for sure
so you may need to split this PR in two if a bug needs to be fixed in a lower branch

@theofidry
Copy link
ContributorAuthor

theofidry commentedJan 6, 2017
edited
Loading

@nicolas-grekas I'm actually not sure if this should be considered a feature or a bug (hence my question of which branch to target). The PR doesn't need to be split, deprecatingautowiring_types is not done yet and I plan to do it in another PR.

@dunglas
Copy link
Member

👍

privatefunctioncreateAutowiredDefinition(\ReflectionClass$typeHint,$id)
{
if (isset($this->ambiguousServiceTypes[$typeHint->name])) {
if ($this->container->has($typeHint->name)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can't this be movedhere to avoid building the map when not necessary?

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

Good point, gonna try

@nicolas-grekas
Copy link
Member

Closing, see ##21494.

@theofidry
Copy link
ContributorAuthor

Thanks :)

@theofidrytheofidry deleted the bugfix/autowire-interface branchFebruary 1, 2017 21:02
@nicolas-grekasnicolas-grekas modified the milestones:3.x,3.3Mar 24, 2017
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@dunglasdunglasdunglas requested changes

+1 more reviewer

@GuilhemNGuilhemNGuilhemN left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Projects

None yet

Milestone

3.3

Development

Successfully merging this pull request may close these issues.

5 participants

@theofidry@GuilhemN@dunglas@nicolas-grekas@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp