Skip to content

[FrameworkBundle] Allow using the kernel as a registry of controllers and service factories #34881

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 17, 2019

Conversation

nicolas-grekas
Copy link
Member

@nicolas-grekas nicolas-grekas commented Dec 8, 2019

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:

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));
    }
}

@nicolas-grekas nicolas-grekas force-pushed the silex-is-back branch 3 times, most recently from 62fec54 to cc9f9f2 Compare December 9, 2019 14:50
@nicolas-grekas
Copy link
Member Author

PR is ready, now with tests.
Failures are all false positives.

Copy link
Member

@yceruto yceruto left a comment

Choose a reason for hiding this comment

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

Nice :), LGTM!

@nicolas-grekas nicolas-grekas force-pushed the silex-is-back branch 2 times, most recently from e5873f6 to 11bc128 Compare December 9, 2019 15:40
@nicolas-grekas nicolas-grekas force-pushed the silex-is-back branch 4 times, most recently from 7695648 to bade028 Compare December 11, 2019 10:47
Copy link
Member

@weaverryan weaverryan left a comment

Choose a reason for hiding this comment

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

I like this = +1

Just make sure you make fabbot happy ;)

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
@nicolas-grekas nicolas-grekas merged commit 9c9b99c into symfony:master Dec 17, 2019
@nicolas-grekas nicolas-grekas deleted the silex-is-back branch December 17, 2019 10:16
@mnapoli
Copy link
Contributor

mnapoli commented Dec 17, 2019

That sounds really interesting! Since you closed #28992 is it possible to use anonymous functions to declare services?

@nicolas-grekas
Copy link
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 :)

@mnapoli
Copy link
Contributor

mnapoli commented Dec 17, 2019

Oh OK, thanks for the clarification.

Since #28992 was specifically about closures, and it was marked as "fixed" by this PR, this was confusing.

@nicolas-grekas
Copy link
Member Author

Thanks for asking, it deserved the clarification :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[RFC] [DI] Functional Service Support [DependencyInjection] Request for anonymous function factories
9 participants