Skip to content

[DependencyInjection] Added a shortcut method for autowired definitions #20648

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 13, 2016

Conversation

wouterj
Copy link
Member

@wouterj wouterj commented Nov 26, 2016

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets -
License MIT
Doc PR todo

This is a simple proposal to make adding autowired definitions to the ContainerBuilder a little easier. Registering autowired services in PHP code was quite verbose at the moment, while the whole point of autowiring is quick service registration.

Before

$container->register('app.twig_extension', AppExtension::class)
    ->setAutowired(true)
    ->addTag('twig.extension')
;

After

$container->autowire('app.twig_extension', AppExtension::class)
    ->addTag('twig.extension')
;

With #20264, this will be even nicer:

$container->autowire(AppExtension::class)
    ->addTag('twig.extension')
;

*/
public function autowire($id, $class = null)
{
return $this->setDefinition((new Definition($class))->setAutowired(true));
Copy link
Contributor

Choose a reason for hiding this comment

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

$id forgotten ;)

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, updated it and added a simple test.

@nicolas-grekas
Copy link
Member

We should plan for #20167 also

@wouterj
Copy link
Member Author

wouterj commented Nov 26, 2016

@nicolas-grekas I agree. What about introducing a third (optional) type parameter?

@chalasr
Copy link
Member

chalasr commented Nov 27, 2016

I like the shortcut but the name made me think the method allows to autowire an existing service more than registering a new autowired one (i.e replace Definition::setAutowire*).
Maybe registerAutowired() would be easier to understand, even if less cool.

@nicolas-grekas
Copy link
Member

autowire LGTM :)

@dunglas
Copy link
Member

dunglas commented Dec 1, 2016

I love it 👍

@nicolas-grekas nicolas-grekas added this to the 3.x milestone Dec 6, 2016
@fabpot
Copy link
Member

fabpot commented Dec 13, 2016

Thank you @wouterj.

@fabpot fabpot merged commit 6ef4ce8 into symfony:master Dec 13, 2016
fabpot added a commit that referenced this pull request Dec 13, 2016
…wired definitions (wouterj)

This PR was merged into the 3.3-dev branch.

Discussion
----------

[DependencyInjection] Added a shortcut method for autowired definitions

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | todo

This is a simple proposal to make adding autowired definitions to the `ContainerBuilder` a little easier. Registering autowired services in PHP code was quite verbose at the moment, while the whole point of autowiring is quick service registration.

**Before**
```php
$container->register('app.twig_extension', AppExtension::class)
    ->setAutowired(true)
    ->addTag('twig.extension')
;
```

**After**
```php
$container->autowire('app.twig_extension', AppExtension::class)
    ->addTag('twig.extension')
;
```

With #20264, this will be even nicer:
```php
$container->autowire(AppExtension::class)
    ->addTag('twig.extension')
;
```

Commits
-------

6ef4ce8 Added a shortcut method for autowired definitions
@wouterj wouterj deleted the patch-17 branch December 13, 2016 09:19
@nicolas-grekas nicolas-grekas modified the milestones: 3.x, 3.3 Mar 24, 2017
@fabpot fabpot mentioned this pull request May 1, 2017
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.

7 participants