Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[DependencyInjection] Use current class as default class for factory declarations #20888

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

Closed
lstrojny opened this issue Dec 13, 2016 · 1 comment

Comments

@lstrojny
Copy link
Contributor

Assume this config:

<service class="SomeClass">
    <factory method="create"/>
</service>

I would expect this to call SomeClass::create(). Instead it dies with this error:

Cannot dump definition because of invalid class name ('')

Any reason to not use the current class as the default class for a factory declaration.

@ogizanagi
Copy link
Contributor

It actually makes sense to me 👍

See #20943

@fabpot fabpot closed this as completed Jan 24, 2017
fabpot added a commit that referenced this issue Jan 24, 2017
…ss for factory declarations (ogizanagi)

This PR was merged into the 3.3-dev branch.

Discussion
----------

[DependencyInjection] Use current class as default class for factory declarations

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #20888
| License       | MIT
| Doc PR        | Should update the notice about the "class" attribute on http://symfony.com/doc/current/service_container/factories.html

#20888 makes sense to me, considering the following sample extracted from the documentation:

```xml
<service id="app.newsletter_manager" class="AppBundle\Email\NewsletterManager">
    <factory class="AppBundle\Email\NewsletterManager" method="create" />
</service>
```

The class is used as a factory to create itself, thus it can be simplified to:

```xml
<service id="app.newsletter_manager" class="AppBundle\Email\NewsletterManager">
    <factory method="create" />
</service>
```

However, it's not possible to provide the same feature for the YAML format, because it doesn't allow to distinct a function from a method call if the class is not provided explicitly under the `factory` key, whereas the xml format use a dedicated `function` attribute.
Would this inconsistency between those two formats be a no-go for this feature?

The doc notices:
> When using a factory to create services, the value chosen for the class option has no effect on the resulting service. The actual class name only depends on the object that is returned by the factory. However, the configured class name may be used by compiler passes and therefore should be set to a sensible value.

If this is merged, it should be updated wisely in order to not confuse everyone with this feature when using the xml format.

UPDATE: The yaml format is now supported when the class is not provided in the factory array:

```yml
services:
    my_factory:
        class: Bar\Baz
        factory: [~, 'create']
```

Commits
-------

e6d8570 [DependencyInjection] Use current class as default class for factory declarations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants