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

[DI] Allow extensions to create ServiceLocator as services#21770

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
fabpot merged 1 commit intosymfony:masterfromnicolas-grekas:di-service-locator
Mar 5, 2017

Conversation

@nicolas-grekas
Copy link
Member

@nicolas-grekasnicolas-grekas commentedFeb 26, 2017
edited
Loading

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

https://github.com/symfony/symfony/pull/21770/files?w=1

With this PR, DI extensions are able to create "service locator" services.
They are easily created as such:

$container->register('my_service_locator', ServiceLocator::class)    ->addArgument(array('exposed_id' =>newServiceClosureArgument(newReference('internal_id')),    ))

I already need this in two different PRs to come.

ogizanagi, chalasr, and sstok reacted with thumbs up emoji
foreach ($value->getValues()as$k =>$v) {
if ($v->getInvalidBehavior() === ContainerInterface::IGNORE_ON_INVALID_REFERENCE && !$this->has((string)$v)) {
continue;
}
Copy link
Member

Choose a reason for hiding this comment

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

good catch

@nicolas-grekasnicolas-grekasforce-pushed thedi-service-locator branch 2 times, most recently from5af7880 toc831e2dCompareFebruary 28, 2017 17:06
@nicolas-grekas
Copy link
MemberAuthor

@stof I updated this PR so that ServiceLocatorArgument doesn't need to be a Definition anymore. I now use a new ServiceClosureArgument type instead. See updated PR description.
How does it look to you? The PR only needs tests now to me.

chalasr reacted with hooray emoji

returnsprintf("function ()%s { return %s; }",$this->dumpValue($reference,$interpolate));
}

returnsprintf("function ()%s {\n return %s;\n }",$this->dumpValue($reference,$interpolate));
Copy link
Member

@chalasrchalasrFeb 28, 2017
edited
Loading

Choose a reason for hiding this comment

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

not enough values|too much placeholders?

should bereturn sprintf("function (): %s {\n return %s;\n }", $type, $this->dumpValue($reference, $interpolate)); at the end, same above (missing colon+space after parenthesis in case the type exists, and $type argument)

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

why? indentation is desired in the output
the type is not mandatory, always putting the: would be a parse error

Copy link
Member

Choose a reason for hiding this comment

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

So$type misses a: when available and$type is missing as second argument of sprintf?

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

indeed, fixed

@nicolas-grekasnicolas-grekasforce-pushed thedi-service-locator branch 5 times, most recently fromda4e441 to4cf0b83CompareFebruary 28, 2017 22:22
@nicolas-grekas
Copy link
MemberAuthor

nicolas-grekas commentedFeb 28, 2017
edited
Loading

Tests added, PR ready

@nicolas-grekasnicolas-grekasforce-pushed thedi-service-locator branch 5 times, most recently fromdb6375f to54855feCompareMarch 2, 2017 13:07
Copy link
Member

@chalasrchalasr left a comment

Choose a reason for hiding this comment

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

👍

Copy link
Member

@dunglasdunglas left a comment

Choose a reason for hiding this comment

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

👍

@nicolas-grekas
Copy link
MemberAuthor

nicolas-grekas commentedMar 4, 2017
edited
Loading

I realized that there is a way to decouple autowiring and typed references.
Thus, I renamed the previously calledAutowirableReference toTypedReference.
These will be autowired only if their holding-definition is itself autowired.
Independently from autowiring, these references allow to generate return-type-hinted service-locator factories, making things more strict thus more robust for the user.

@fabpot
Copy link
Member

Thank you@nicolas-grekas.

@fabpotfabpot merged commit1d96633 intosymfony:masterMar 5, 2017
fabpot added a commit that referenced this pull requestMar 5, 2017
…ices (nicolas-grekas)This PR was merged into the 3.3-dev branch.Discussion----------[DI] Allow extensions to create ServiceLocator as services| Q             | A| ------------- | ---| Branch?       | master| Bug fix?      | no| New feature?  | yes| BC breaks?    | no| Deprecations? | no| Tests pass?   | yes| Fixed tickets | -| License       | MIT| Doc PR        | -https://github.com/symfony/symfony/pull/21770/files?w=1With this PR, DI extensions are able to create "service locator" services.They are easily created as such:```php$container->register('my_service_locator', ServiceLocator::class)    ->addArgument(array(        'exposed_id' => new ServiceClosureArgument(new Reference('internal_id')),    ))```I already need this in two different PRs to come.Commits-------1d96633 [DI] Allow creating ServiceLocator-based services in extensions
@nicolas-grekasnicolas-grekas deleted the di-service-locator branchMarch 5, 2017 21:08
fabpot added a commit that referenced this pull requestMar 22, 2017
…s" tag to inject services into actions (nicolas-grekas)This PR was merged into the 3.3-dev branch.Discussion----------[FrameworkBundle] Add new "controller.service_arguments" tag to inject services into actions| Q             | A| ------------- | ---| Branch?       | master| Bug fix?      | no| New feature?  | yes| BC breaks?    | no| Deprecations? | no| Tests pass?   | (no test yet)| Fixed tickets | -| License       | MIT| Doc PR        | -Talking with@simensen and@weaverryan, we wondered if we could leverage the `ArgumentResolver` mechanism to make it inject services on demand, using e.g. autowiring.```phpclass PostController{  public function indexAction(Request $request, PostRepository $postRepository)  {    // PostRepository comes from the container    $postRepository->findAll(); // ...  }}```This PR achieves that, using a new "controller.service_arguments" tag. Typically:```yamlservices:    AppBundle\Controller\PostController:        autowire: true        tags:            - name: controller.service_arguments```It also supports with explicit wiring (thus doesn't necessarily require autowiring if you don't want to use it):```yamlservices:    AppBundle\Controller\PostController:        tags:            - name: controller.service_arguments              action: fooAction              argument: logger              id: my_logger```~~The attached diff is bigger than strictly required for now, until#21770 is merged.~~Todo:- [x] rebase on top of#21770 when merged- [x] add tests- [x] add cleaning pass to remove empty service locatorsCommits-------9c6e672 [FrameworkBundle] Add new "controller.service_arguments" tag to inject services into actions
@fabpotfabpot mentioned this pull requestMay 1, 2017
@TomasVotruba
Copy link
Contributor

TomasVotruba commentedMay 1, 2017
edited
Loading

I got feeling that I might use this feature... but do not understand it yet.

Could you share some more "before/after" examples please? 2-3 would be great, so I could understand it better.

@nicolas-grekas
Copy link
MemberAuthor

Can't answer in detail now, but please check#21708 it might help, either the code or the feature.

OskarStark added a commit to symfony/symfony-docs that referenced this pull requestMar 9, 2023
…es (HypeMC)This PR was merged into the 5.4 branch.Discussion----------[DependencyInjection] Add section about Service ClosuresDocuments service closures added insymfony/symfony#21770 andsymfony/symfony#41176, and the `service_closure()` PHP-DSL function added insymfony/symfony#42625 .Commits-------f2b2fdb [DI] Add section about Service Closures
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@dunglasdunglasdunglas approved these changes

@chalasrchalasrchalasr approved these changes

Assignees

No one assigned

Projects

None yet

Milestone

3.3

Development

Successfully merging this pull request may close these issues.

6 participants

@nicolas-grekas@fabpot@TomasVotruba@dunglas@chalasr@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp