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

[DependencyInjection] Improve ServiceLocatorTagPass service matching#28571

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

Merged

Conversation

@codedmonkey
Copy link
Contributor

@codedmonkeycodedmonkey commentedSep 23, 2018
edited
Loading

QA
Branch?master
Bug fix?no
New feature?yes
BC breaks?no
Deprecations?no
Tests pass?yes
Fixed tickets#26892
LicenseMIT
Doc PRsymfony/symfony-docs#10397

Allows omitting of keys for service locator arguments (it will automatically take over the original definition alias).

Copy link
Member

@nicolas-grekasnicolas-grekas left a comment

Choose a reason for hiding this comment

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

thanks, do you think you could add some tests please?

continue;
}

if (\is_string($type =$v) &&preg_match('/^\??[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+(?:\\\\[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+)*+$/',$type)) {
Copy link
Member

@nicolas-grekasnicolas-grekasSep 23, 2018
edited
Loading

Choose a reason for hiding this comment

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

this will allow<argument>Psr\Logger\LoggerInterface</argument>
alongside with<argument type="service" />
I read too fast this part of your proposal. I think it's not a good idea. It would create an inconsistency in the way the configuration works.
But being able to omit the "key" part looks like a good idea to me.

}

if (\is_int($k)) {
unset($arguments[0][$k]);
Copy link
Member

@nicolas-grekasnicolas-grekasSep 24, 2018
edited
Loading

Choose a reason for hiding this comment

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

I'd suggest moving "unset" outside of the "if": that will allow preserving the order in the list

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

I agree but right after the for loop it does aksort so I presume that's unnecessary.

Choose a reason for hiding this comment

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

I forgot about it, no need then. Thanks.


$this->assertEquals(CustomDefinition::class,\get_class($locator('bar')));
$this->assertEquals(CustomDefinition::class,\get_class($locator('baz')));
$this->assertEquals(CustomDefinition::class,\get_class($locator('some.service')));
Copy link
Member

Choose a reason for hiding this comment

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

assertSame

if (\is_int($k)) {
unset($arguments[0][$k]);

$k = (string)$v;
Copy link
Member

Choose a reason for hiding this comment

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

should we check whether this key already exists in the configured arguments ?

chalasr reacted with thumbs up emoji

Choose a reason for hiding this comment

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

I'm not sure: if the key existsafter the numbered one, it will override it - and if it came before, then the numbered one should win to me. works for you also?

Copy link
Member

Choose a reason for hiding this comment

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

nope, it willalways override it here. If it comes after the numbered one, processing it will see the value set by the processing of the numbered one, not the original one, as it would have been replaced

Choose a reason for hiding this comment

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

Really? foreach operates on a copy of array so that it will yield the original value at this iteration, and overwrite the value set by the numbered step to me.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

Added some basic tests that should confirm@nicolas-grekas description. I do like the idea of preventing unexepected behavior for the user but it works either way for me.

$k = (string)$v;

if (isset($arguments[0][$k])) {
thrownewInvalidArgumentException(sprintf('Invalid definition for service "%s": service key "%s" cannot be inherited from the service definition because the key already exists.',$this->currentId,$k));

Choose a reason for hiding this comment

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

Per my comments above, I'm not sure about this. It should be removed to me.

Choose a reason for hiding this comment

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

(but please add tests showing that the later wins over the former if you can, that'll help prove who is right here :) )

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

Fine by me if all differences have been set aside. I've checked how the different configuration formats handled duplicate declarations and they don't seem work the same at all, so there doesn't seem to be a best way.

Choose a reason for hiding this comment

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

Can you elaborate? What's the difference you spotted?
If we want to keep the exception (I'm fine with it also), I'd suggest a message like:
`Invalid service locator definition for "%s": key "%s" is defined twice.', $this->currentId, $k));

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

Here's my examples:

// PHP - Result: service_b    ->setArguments([[        'service_a' => new Reference('service_a'),        'service_a' => new Reference('service_b'),    ]])// YAML - Result: service_a        arguments:            -                service_a: '@service_a'                service_a: '@service_b'// XML - Result: service_b      <argument type="collection">        <argument type="service" key="service_a" />        <argument type="service" key="service_a" />      </argument>

Not that how PHP handles duplicate array keys matters a lot, just thought it might give a direction for the right solution.

@nicolas-grekas
Copy link
Member

Thanks for the update. Can you add a test case that throws the new exception please?

@codedmonkey
Copy link
ContributorAuthor

I removed the additional exception if that's all right with you?

@nicolas-grekas
Copy link
Member

Thank you@codedmonkey.

@nicolas-grekasnicolas-grekas merged commit1c1210a intosymfony:masterOct 3, 2018
nicolas-grekas added a commit that referenced this pull requestOct 3, 2018
…rvice matching (codedmonkey)This PR was squashed before being merged into the 4.2-dev branch (closes#28571).Discussion----------[DependencyInjection] Improve ServiceLocatorTagPass service matching| Q             | A| ------------- | ---| Branch?       | master| Bug fix?      | no| New feature?  | yes| BC breaks?    | no| Deprecations? | no| Tests pass?   | yes| Fixed tickets |#26892| License       | MIT| Doc PR        |symfony/symfony-docs#10397Allows omitting of keys for service locator arguments (it will automatically take over the original definition alias).Commits-------1c1210a [DependencyInjection] Improve ServiceLocatorTagPass service matching
@nicolas-grekasnicolas-grekas modified the milestones:next,4.2Nov 1, 2018
@fabpotfabpot mentioned this pull requestNov 3, 2018
@fabpotfabpot mentioned this pull requestNov 3, 2018
@codedmonkeycodedmonkey deleted the service-locator branchDecember 14, 2018 23:26
javiereguiluz added a commit to symfony/symfony-docs that referenced this pull requestSep 23, 2019
…tor changes (codedmonkey, javiereguiluz)This PR was submitted for the master branch but it was merged into the 4.3 branch instead (closes#10397).Discussion----------[DependencyInjection] Add documentation for service locator changesAdds documentation for PRsymfony/symfony#28571Commits-------8d8c65e Rewordbc5ef85 [DependencyInjection] Add documentation for service locator changes
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@stofstofstof left review comments

@nicolas-grekasnicolas-grekasnicolas-grekas approved these changes

@chalasrchalasrchalasr approved these changes

Assignees

No one assigned

Projects

None yet

Milestone

4.2

Development

Successfully merging this pull request may close these issues.

5 participants

@codedmonkey@nicolas-grekas@stof@chalasr@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp