8000 Synthetic service cannot be define if binds are present · Issue #32874 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Synthetic service cannot be define if binds are present #32874

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
noemi-salaun opened this issue Aug 1, 2019 · 5 comments
Closed

Synthetic service cannot be define if binds are present #32874

noemi-salaun opened this issue Aug 1, 2019 · 5 comments

Comments

@noemi-salaun
Copy link
Contributor

Symfony version(s) affected: tested with 3.4 and 4.3

Description

If I define a synthetic service in my service.yaml config file where binds are already defined, I get the error

Invalid service "my_synthetic_service": class "" does not exist.

How to reproduce

  1. Create a new app:
symfony new my-app
  1. Add the service definition and the bind in config/services.yaml
services:
    _defaults:
        # ...
        bind:
            $someBind: someValue

    # ...

    my_synthetic_service:
        synthetic: true
        public: true
  1. Run any console command
./bin/console debug:container my_synthetic_service
  1. Get the full error

Unused binding "$someBind" in service "my_synthetic_service".
Could be related to:

  • Invalid service "my_synthetic_service": class "" does not exist.

This error is a little more understandable than the first one, but I have to try finding the minimal failing exemple to get this error

  1. Add some service that use the bind
<?php
namespace App\Service;
class SomeService
{
    public function __construct(string $someBind) {  }
}
  1. Re-run the console command
  2. Get the minimal error

Invalid service "my_synthetic_service": class "" does not exist.

This error is way worse to debug the real problem

@xabbuh
Copy link
Member
xabbuh commented Aug 2, 2019

What is the real name for the service that you used? So far it seems to me that you just forgot to configure its class.

@noemi-salaun
Copy link
Contributor Author

I do not have to declare my service class if I follow the documentation for synthetic services.

See https://symfony.com/doc/current/service_container/synthetic_services.html :

# config/services.yaml
services:
    # synthetic services don't specify a class
    app.synthetic_service:
        synthetic: true

If I remove the bind declaration, everything works fine.

Another exemple of synthetic service is the kernel service. If you look at its definition, you can see that it does not define its class.

https://github.com/symfony/symfony/blob/3.4/src/Symfony/Bundle/FrameworkBundle/Resources/config/services.xml#L48

<service id="kernel" synthetic="true" public="true" />

@k0d3r1s
Copy link
Contributor
k0d3r1s commented Aug 7, 2019

if I add:

if (null !== $class && !$definition->isSynthetic()) {

around try on line 145 in symfony/dependency-injection/Compiler/AbstractRecursivePass.php and give $r variable default null value, this doesn't happen anymore.
can you try do that and see if it works? i could do the PR than if it fixes it.

I did add that null !== $class check but in my limited synthetic service knowledge, I don't actually see point of that check. Maybe someone more experienced with this can tell if it's a needed check to have here?

Than maybe there's no need to do if around try and just add:

if ($definition->isSynthetic()) {  
    return null;  
}

at the top of the function

@nicolas-grekas
Copy link
Member

add [...] at the top of the function

@k0d3r1s looks like this could be the fix. Could you please send a PR on branch 3.4, with a test case?

@k0d3r1s
Copy link
Contributor
k0d3r1s commented Sep 8, 2019

@nicolas-grekas can you point me towards an example on how to define that default bind inside a test case?

Edit: Nevermind :)

nicolas-grekas added a commit that referenced this issue Sep 27, 2019
…synthetic (k0d3r1s)

This PR was squashed before being merged into the 3.4 branch (closes #33625).

Discussion
----------

[DependencyInjection] Fix wrong exception when service is synthetic

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | #32874
| License       | MIT

This fixes wrongfully thrown exception when service is defined as synthetic and some arguments are binded in _defaults

Commits
-------

152dec9 [DependencyInjection] Fix wrong exception when service is synthetic
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

5 participants
0