[FrameworkBundle] Allow using the kernel as a registry of controllers and service factories#34881
Merged
nicolas-grekas merged 1 commit intosymfony:masterfrom Dec 17, 2019
Merged
Conversation
2cb3d9a to
86da1af
Compare
dunglas
approved these changes
Dec 8, 2019
stof
reviewed
Dec 9, 2019
stof
reviewed
Dec 9, 2019
62fec54 to
cc9f9f2
Compare
Member
Author
|
PR is ready, now with tests. |
e5873f6 to
11bc128
Compare
weaverryan
reviewed
Dec 9, 2019
weaverryan
reviewed
Dec 9, 2019
weaverryan
reviewed
Dec 9, 2019
7695648 to
bade028
Compare
… and service factories
bade028 to
9c9b99c
Compare
ro0NL
reviewed
Dec 13, 2019
weaverryan
approved these changes
Dec 13, 2019
nicolas-grekas
added a commit
that referenced
this pull request
Dec 17, 2019
… of controllers and service factories (nicolas-grekas) This PR was merged into the 5.1-dev branch. Discussion ---------- [FrameworkBundle] Allow using the kernel as a registry of controllers and service factories | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Fix #28992, fix #29997 | License | MIT | Doc PR | - This PR builds on #34873 and #34872 and allows using the `Kernel` as a registry of autowired controllers and service factories. The `ContainerConfigurator` passed to `configureContainer()` defaults to declaring autowired and autoconfigured services. TL;DR: Silex is back but in a much more powerful way \o/ Here is a Kernel that just works and displays `Hello App\Foo` on the `/` route: ```php class Kernel extends BaseKernel { use MicroKernelTrait; protected function configureContainer(ContainerConfigurator $container): void { $container->services() ->load('App\\', '../src') ->set(Foo::class) ->factory([$this, 'createFoo']); } public function createFoo(Bar $bar) { return new Foo($bar); } protected function configureRoutes(RoutingConfigurator $routes): void { $routes->add('home', '/')->controller([$this, 'helloAction']); } public function helloAction(Foo $foo) { return new Response('Hello '.get_class($foo)); } } ``` Commits ------- 9c9b99c [FrameworkBundle] Allow using the kernel as a registry of controllers and service factories
Contributor
|
That sounds really interesting! Since you closed #28992 is it possible to use anonymous functions to declare services? |
Member
Author
|
Anonymous functions won't happen, I've explained why in #28992 (code relocation is full of edge cases we don't want to deal with). But this PR provides an alternative to using closures, by using methods on the Kernel. Let's see how far this goes :) |
Contributor
|
Oh OK, thanks for the clarification. Since #28992 was specifically about closures, and it was marked as "fixed" by this PR, this was confusing. |
Member
Author
|
Thanks for asking, it deserved the clarification :) |
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR builds on #34873 and #34872 and allows using the
Kernelas a registry of autowired controllers and service factories. TheContainerConfiguratorpassed toconfigureContainer()defaults to declaring autowired and autoconfigured services.TL;DR: Silex is back but in a much more powerful way \o/
Here is a Kernel that just works and displays
Hello App\Fooon the/route: