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] Automatic registration of FQCN services#18268

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

Conversation

@hason
Copy link
Contributor

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

All services can be accessed by FQCN, the same concept as for form types:

# routing.ymlindex:path:/defaults:_controller:'AppBundle\Controller\MyController:index'
# services.ymlservices:my_controller:class:AppBundle\Controller\MyControllerarguments:      -'@Symfony\Bridge\Doctrine\RegistryInterface'
class OldControllerextends Controller{publicfunctionindexAction()    {$em =$this->get(RegistryInterface::class)->getManager();    }}

TomasVotruba reacted with thumbs up emojiKoc and derrabus reacted with confused emoji
@Nicofuma
Copy link
Contributor

To me it looks very close to the issue solved by the autowiring... What is the issue you are trying to solve? (at least I think that a big part of the code could be shared)

@hasonhasonforce-pushed theclass-named-services branch 2 times, most recently from46dfd0f tofb1e27eCompareMarch 23, 2016 09:25
@hasonhasonforce-pushed theclass-named-services branch fromfb1e27e to88ccab4CompareMarch 23, 2016 09:51

class AmbiguousService
{
static public function throwException($class, $services)
Copy link
Member

Choose a reason for hiding this comment

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

What is the goal of this class ?

@GuilhemN
Copy link
Contributor

as@Nicofuma said, this is very close to what does autowiring and makes the container even bigger... so I'd say 👎 as is.

@dunglas
Copy link
Member

What can be nice instead is an utility class returning the list of services of a given type. (This class can be used internally by the autowiring system and in external bundles needing such features).

GuilhemN, backbone87, and derrabus reacted with thumbs up emoji

@backbone87
Copy link
Contributor

$em = $this->get(RegistryInterface::class)->getManager(); looks acutally quite neat, but maybe i am missing something

@dunglas
Copy link
Member

@backbone87 it is indeed attractive.

To prevent any confusion, why not introducing a new method returning a list of all services of a given type:

$container->getOfType(RegistryInterface::class);// the instance of the service of the given type, throw an exception if there is several services of this type$container->getAllOfType(RegistryInterface::class);// an array of services having the given type
GuilhemN, Exter-N, fesor, TomasVotruba, and theofidry reacted with thumbs up emoji

@hason
Copy link
ContributorAuthor

@dunglas And why introduce a new method (compare it withhttp://php-di.org/)? What is use case of the public method$container->getAllOfType(RegistryInterface::class);?

@hason
Copy link
ContributorAuthor

@Ener-Getick@Nicofuma Autowiring is only a special application of FQCN services. Symfony supports this feature for autowired services, but you can't make a reference to service by its type manually. That's inconsistent behavior.

@dunglas
Copy link
Member

A new method because it doesn't do the same thing: get retrieves a service by its name, getOfType retrieves a service by one of its types (SOLID code).

getAllOfType can be handy to retrieve all your entity managers, or all your elastica finders for instance, or all your API Platform data providers. I can think to a lot of usages.

GuilhemN reacted with thumbs up emoji

@hason
Copy link
ContributorAuthor

@dunglas The service name can be string or FQCN. The string is alias for FQCN and vice versa.

services:Twig_Environment:class:Twig_Environmenttwig:alias:Twig_Environment

What is the correct call,$container->get(Twig_Environment::class) or$container->getOfType(Twig_Environment::class)?

Mentioned reasons for the methodgetAllOfType() are solved by tags.

@Exter-N
Copy link

It would also be nice to makegetAllOfType return the service identifiers as keys of the returned array.

GuilhemN reacted with thumbs up emoji

@GuilhemN
Copy link
Contributor

I opened#18300 which is an alternative approach using the methods@dunglas proposed (and one more:getServiceIdsOfType).
This also replaces the utility class described by@dunglas.

@nicolas-grekas
Copy link
Member

The DI container is not a service locator and should not be
This leans over this limit to me (also getAllOfType, getOfType, etc.)
👎 as well.

chalasr reacted with thumbs up emoji

@GuilhemN
Copy link
Contributor

@nicolas-grekas what do you think about the utility class I introduced in#18300 (i changed my mind and decided to create a new class instead of introducing this kind of methods in the container, see#18300 (comment), it's only possible to use it at compilation not at runtime) ?

@hason
Copy link
ContributorAuthor

@nicolas-grekas Exactly! This PR is introducing automatic registration of service aliases. It has nothing to do with a service locator.

@hason
Copy link
ContributorAuthor

I created thehttps://github.com/symfonette/class-named-services library.

@nicolas-grekasnicolas-grekas changed the title[POC] [WIP] [DependencyInjection] Automatic registration of FQCN services[DependencyInjection] Automatic registration of FQCN servicesDec 6, 2016
@nicolas-grekasnicolas-grekas added this to the3.x milestoneDec 6, 2016
fabpot added a commit that referenced this pull requestJan 7, 2017
…-grekas)This PR was merged into the 3.3-dev branch.Discussion----------[DI] Optional class for named services| Q             | A| ------------- | ---| Branch?       | master| Bug fix?      | no| New feature?  | yes| BC breaks?    | no| Deprecations? | yes| Tests pass?   | yes| Fixed tickets | -| License       | MIT| Doc PR        | -Continues#20264:- makes the id-to-class mapping context-free (no `class_exist` check),- deals with ChildDefinition (which should not have this rule applied on them),- deprecates FactoryReturnTypePass as discussed on slack and reported in this comment:#19191 (comment)From@hason:> I prefer class named services (in applications) because naming things are too hard:``` yamlservices:    Vendor\Namespace\Class:        class: Vendor\Namespace\Class        autowire: true```> This PR solves redundant parameter for class:``` yamlservices:    Vendor\Namespace\Class:        autowire: true```> Inspirations:https://laravel.com/docs/5.3/container,#18268,http://php-di.org/,Commits-------a18c4b6 [DI] Add tests for class named services71b17c7 [DI] Optional class for named services
@TomasVotruba
Copy link
Contributor

I thinks this is solved by#21133, isn't it?

@theofidry
Copy link
Contributor

@TomasVotruba yes, actually it's been working for a while already#21133 just made it easier. There is this however that's still missing and might be interesting:

# routing.ymlindex:path:/defaults:_controller:'AppBundle\Controller\MyController:index'

@nicolas-grekas
Copy link
Member

Closing then, thanks@hason

@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

No reviews

Assignees

No one assigned

Projects

None yet

Milestone

3.3

Development

Successfully merging this pull request may close these issues.

12 participants

@hason@Nicofuma@GuilhemN@dunglas@backbone87@Exter-N@nicolas-grekas@TomasVotruba@theofidry@stof@javiereguiluz@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp