Skip to content

[DependencyInjection] Add the constructor option to service definition #18070

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
Jun 6, 2023

Conversation

alexandre-daubois
Copy link
Member

@OskarStark OskarStark added the Waiting Code Merge Docs for features pending to be merged label Mar 18, 2023
@carsonbot carsonbot modified the milestones: 6.3, next Mar 18, 2023
fabpot added a commit to symfony/symfony that referenced this pull request Mar 31, 2023
…utoconfigure]` (alexandre-daubois)

This PR was merged into the 6.3 branch.

Discussion
----------

[DependencyInjection] Add `constructor` option to `#[Autoconfigure]`

| Q             | A
| ------------- | ---
| Branch?       | 6.3
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | _NA_
| License       | MIT
| Doc PR        | symfony/symfony-docs#18070

Following [this discussion](https://twitter.com/nicolasgrekas/status/1634259754104025088?s=20) on Twitter, here is my try to use service factories with attributes. This PR adds the `constructor` option to the `Autoconfigure` attribute, and more generally, the `constructor` option on service definitions.

This allows to write services using a factory this way:

```php
#[Autoconfigure(constructor: 'create')]
class FactoryAttributeService
{
    public function __construct(private readonly string $bar)
    {
    }

    public function getBar(): string
    {
        return $this->bar;
    }

    public static function create(string $foo = 'foo'): static
    {
        return new self($foo);
    }
}
```

Commits
-------

8dda3f0 [DependencyInjection] Add `constructor` option to services declaration and to `#[Autoconfigure]`
symfony-splitter pushed a commit to symfony/dependency-injection that referenced this pull request Mar 31, 2023
…utoconfigure]` (alexandre-daubois)

This PR was merged into the 6.3 branch.

Discussion
----------

[DependencyInjection] Add `constructor` option to `#[Autoconfigure]`

| Q             | A
| ------------- | ---
| Branch?       | 6.3
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | _NA_
| License       | MIT
| Doc PR        | symfony/symfony-docs#18070

Following [this discussion](https://twitter.com/nicolasgrekas/status/1634259754104025088?s=20) on Twitter, here is my try to use service factories with attributes. This PR adds the `constructor` option to the `Autoconfigure` attribute, and more generally, the `constructor` option on service definitions.

This allows to write services using a factory this way:

```php
#[Autoconfigure(constructor: 'create')]
class FactoryAttributeService
{
    public function __construct(private readonly string $bar)
    {
    }

    public function getBar(): string
    {
        return $this->bar;
    }

    public static function create(string $foo = 'foo'): static
    {
        return new self($foo);
    }
}
```

Commits
-------

8dda3f0b77 [DependencyInjection] Add `constructor` option to services declaration and to `#[Autoconfigure]`
@javiereguiluz javiereguiluz removed the Waiting Code Merge Docs for features pending to be merged label Jun 6, 2023
@javiereguiluz javiereguiluz modified the milestones: next, 6.3 Jun 6, 2023
@javiereguiluz
Copy link
Member

I totally missed this one. Thanks for this contribution Alex!

@javiereguiluz javiereguiluz merged commit e08dc4c into symfony:6.3 Jun 6, 2023
@alexandre-daubois alexandre-daubois deleted the service-constructor branch June 6, 2023 08:48
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.

4 participants