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] Create an util class to determine services implementing a FQCN#20940

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
GuilhemN wants to merge1 commit intosymfony:masterfromGuilhemN:FQCN

Conversation

@GuilhemN
Copy link
Contributor

@GuilhemNGuilhemN commentedDec 15, 2016
edited
Loading

QA
Branch?master
Bug fix?no
New feature?yes
BC breaks?no
Deprecations?no
Tests pass?yes
Fixed tickets#18300
LicenseMIT
Doc PR

Reopening of#18300.

This PR creates a new util class: theServiceTypeHelper. It extracts some of theAutowirePass logic usable like this (only during the container compilation):

$container =newContainerBuilder();$helper =newServiceTypeHelper($container);$helper->getOfType(Bar::class);

This could be useful in theDunglasActionBundle, to detect when a class is already registered in the container.

Edit: one new advantage of usingautowireTypes: the types map is no longer built if they are sufficient!

$services =$this->typeHelper->getOfType($type);
if (1 ===count($services)) {
return$services[0];
}elseif (1 <count($services)) {
Copy link
Member

Choose a reason for hiding this comment

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

justif here

GuilhemN reacted with thumbs up emoji

try {
$name = (new \ReflectionClass($class))->name;
}catch (\ReflectionException$e) {
Copy link
Member

Choose a reason for hiding this comment

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

this works fine only if you have a fallback autoloader throwing a ReflectionException as a last resort to avoid the fatal error. As you extracted the logic, you need to handle this requirement in your new service

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

This is needed in theAutowirePass because it fetches parameters type hint but this helper doesn't use anything else than the class name, is it still needed?

Copy link
Member

Choose a reason for hiding this comment

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

the class may extend a non-existent parent class (if the service relies on an optional dependency which is not installed)

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

You're right, it's updated in98481b9.

*/
finalclass ServiceTypeHelper
{
privatestatic$classesName;
Copy link
Member

Choose a reason for hiding this comment

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

classNames would be a better name

GuilhemN reacted with thumbs up emoji
*
* @param Definition $definition
*
* @return string|false
Copy link
Member

Choose a reason for hiding this comment

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

I would usestring|null (a nullable string type) instead of this mixed type

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

I changed it but I'm not sure that's worth it (I used?: to not change theisset to aarray_key_exists).

@stof
Copy link
Member

Does it work fine when the compiler pass registers new autowired services ? The compiler pass currently updates it type map for them, but you removed lots of the logic.

@GuilhemN
Copy link
ContributorAuthor

@stof I forgot to update this, it is fixed in98481b9, preciselyhere.

$name =false;
}

returnself::$classNames[$class] =$name ?:null;
Copy link
Member

Choose a reason for hiding this comment

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

this will assign null, not false.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

Should I put brackets around it, usefalse or usearray_key_exists?

@nicolas-grekas
Copy link
Member

Generally speaking, types should not be used for wiring things. We added some features recently to make it possible (ie#21530 ) but there is a big difference: there, the type is used on a very local scope only (the config file that uses it). Here, it opens the door to global type-related side effects.
👎

@GuilhemN
Copy link
ContributorAuthor

@nicolas-grekas this proposal was mostly intended for the DunglasActionBundle but now that you implementedpsr4 and_instanceof in a more generic way I think we indeed don't need it anymore.
Thanks for this features btw!

@nicolas-grekasnicolas-grekas modified the milestone:3.xMar 24, 2017
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@stofstofstof left review comments

Assignees

No one assigned

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

4 participants

@GuilhemN@stof@nicolas-grekas@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp