New in Symfony 3.3: Getter autowiring

Contributed by
Kévin Dunglas
in #21031.

Autowiring is a set of features that enable RAD ("Rapid Application Development") on Symfony applications. Some projects and developers need this kind of development and that's why we keep adding new features to it.

In Symfony 3.3, we decided to add support for getter autowiring for those applications using PHP 7.0 or newer. Consider the following sample classes:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
// src/AppBundle/Manager/Publisher.php
namespace AppBundle\Manager;
use AppBundle\Parser\Markdown;

class Publisher
{
    protected function getMarkdownParser(): Markdown { }

    // ...
}

// src/AppBundle/Parser/Markdown.php
namespace AppBundle\Parser\Markdown;

class Markdown
{
    // ...
}

Now, add the following service configuration:

1
2
3
4
# app/config/services.yml
services:
    AppBundle\Manager\Publisher:
        autowire: ['get*']

Getter autowiring automatically registers the Markdown class as a service and returns an instance of it when the getMarkdownParser() method is called in the Publisher class (and only at this time, lazy loading it).

This feature is part of our experimental features program, so its implementation could change in Symfony 3.4 or we could even remove it entirely. That's why we need you to test it in your real applications and share your thoughts about it with us.

The documentation of this feature is not ready yet, but you can follow @symfonydocs on Twitter to get notified when we publish it.

Comments

So the Publisher class will be a proxy which getMarkdownParser() method will be implemented correctly, returning the correct markdown service? o.O
@Alex Rock Ancelet, indeed :)

Please note that the proxy getter injection part was implemented by Nicolas Grekas in https://github.com/symfony/symfony/pull/20973 . The PR by Kevin Dunglas only added autowire support for this.

Without autowiring you would need a config like:

```
services:
app.markdown_parser:
class: AppBundle\Parser\Markdown

AppBundle\Manager\Publisher:
autowire: ['get*']
getters:
getMarkdownParser: '@app.markdown_parser'
```
See follow up of this article in http://symfony.com/blog/new-in-symfony-3-3-getter-injection

Comments are closed.

To ensure that comments stay relevant, they are closed for old posts.