Skip to content

Autowiring is not working properly in Symfony 3.2.7 #22309

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
secit-pl opened this issue Apr 6, 2017 · 7 comments
Closed

Autowiring is not working properly in Symfony 3.2.7 #22309

secit-pl opened this issue Apr 6, 2017 · 7 comments

Comments

@secit-pl
Copy link

secit-pl commented Apr 6, 2017

Q A
Bug report? yes
Feature request? no
BC Break report? no
Symfony version 3.2.7

I'm not able to autowire the Symfony\Component\Translation\DataCollectorTranslator to more than one service in Symfony 3.2.7. In 3.2.6 all worked fine.

Example code:

Service1.php

<?php

namespace Acme\BugTestBundle\Service;

use Symfony\Component\Translation\DataCollectorTranslator;

class Service1
{
    /**
     * @var DataCollectorTranslator
     */
    private $translator;

    /**
     * Service1 constructor.
     *
     * @param DataCollectorTranslator $translator
     */
    public function __construct(DataCollectorTranslator $translator)
    {
        $this->translator = $translator;
    }
}

Service 2:

<?php

namespace Acme\BugTestBundle\Service;

class Service2 extends Service1
{
}

services.yml

services:
    bug.service1:
        class: Acme\BugTestBundle\Service\Service1
        autowire: true

    bug.service2:
        class: Acme\BugTestBundle\Service\Service2
        autowire: true

The part of the production cache:
var/cache/prod/appProdProjectContainer.php

/*
 * Gets the 'bug.service1' service.
 *
 * This service is shared.
 * This method always returns the same instance of the service.
 *
 * This service is autowired.
 *
 * @return \Acme\BugTestBundle\Service\Service1 A Acme\BugTestBundle\Service\Service1 instance
 */
protected function getBug_Service1Service()
{
    return $this->services['bug.service1'] = new \Acme\BugTestBundle\Service\Service1(new \Symfony\Component\Translation\DataCollectorTranslator($this->get('translator.default')));
}

/*
 * Gets the 'bug.service2' service.
 *
 * This service is shared.
 * This method always returns the same instance of the service.
 *
 * This service is autowired.
 *
 * @return \Acme\BugTestBundle\Service\Service2 A Acme\BugTestBundle\Service\Service2 instance
 */
protected function getBug_Service2Service()
{
    return $this->services['bug.service2'] = new \Acme\BugTestBundle\Service\Service2();
}

As you see only the first service was properly autowired.

@secit-pl secit-pl changed the title Autowiring is not working propperly in Symfony 3.2.7 Autowiring is not working properly in Symfony 3.2.7 Apr 6, 2017
@nicolas-grekas
Copy link
Member

Also reported as a comment here: #22254 (comment)

What error do you get?

@nicolas-grekas
Copy link
Member

nicolas-grekas commented Apr 6, 2017

@enumag @secit-pl could you please fork the Standard Edition and provide us a reproducer?
I just tried the example here locally but didn't manage to make it fail.
Maybe of interest: which version of PHP do you use?
Out of curiosity, does it work when commenting this line?

@enumag
Copy link
Contributor

enumag commented Apr 6, 2017

@nicolas-grekas PHP 7.1.3. Commenting the line you mentioned didn't help. Reproducing it on Standard Edition will probably take some time. Maybe next week.

@secit-pl
Copy link
Author

secit-pl commented Apr 6, 2017

@nicolas-grekas here is the example on the fresh symfony installation. Just:

#> git clone https://github.com/secit-pl/symfony-issue-22309-example.git
#> composer update

When you navigate to the / rotue you'll see the error 500.

The only error reported by symfony is the standard PHP Error:

[2017-04-06 14:54:06] request.INFO: Matched route "acme_bugtest_default_index". {"route":"acme_bugtest_default_index","route_parameters":{"_controller":"Acme\\BugTestBundle\\Controller\\DefaultController::indexAction","_route":"acme_bugtest_default_index"},"request_uri":"http://localhost/symfony-issue-22309-example/web/","method":"GET"} []
[2017-04-06 14:54:06] security.INFO: Populated the TokenStorage with an anonymous Token. [] []
[2017-04-06 14:54:06] php.CRITICAL: Type error: Too few arguments to function Acme\BugTestBundle\Service\Service1::__construct(), 0 passed in C:\xampp\htdocs\symfony-issue-22309-example\var\cache\prod\appProdProjectContainer.php on line 355 and exactly 1 expected {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Type error: Too few arguments to function Acme\\BugTestBundle\\Service\\Service1::__construct(), 0 passed in C:\\xampp\\htdocs\\symfony-issue-22309-example\\var\\cache\\prod\\appProdProjectContainer.php on line 355 and exactly 1 expected at C:\\xampp\\htdocs\\symfony-issue-22309-example\\src\\Acme\\BugTestBundle\\Service\\Service1.php:19)"} []
[2017-04-06 14:54:06] request.CRITICAL: Uncaught PHP Exception Symfony\Component\Debug\Exception\FatalThrowableError: "Type error: Too few arguments to function Acme\BugTestBundle\Service\Service1::__construct(), 0 passed in C:\xampp\htdocs\symfony-issue-22309-example\var\cache\prod\appProdProjectContainer.php on line 355 and exactly 1 expected" at C:\xampp\htdocs\symfony-issue-22309-example\src\Acme\BugTestBundle\Service\Service1.php line 19 {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Type error: Too few arguments to function Acme\\BugTestBundle\\Service\\Service1::__construct(), 0 passed in C:\\xampp\\htdocs\\symfony-issue-22309-example\\var\\cache\\prod\\appProdProjectContainer.php on line 355 and exactly 1 expected at C:\\xampp\\htdocs\\symfony-issue-22309-example\\src\\Acme\\BugTestBundle\\Service\\Service1.php:19)"} []

I've got the error on PHP 7.1.1 (Windows) and 7.1.3 (Linux).

@secit-pl
Copy link
Author

secit-pl commented Apr 6, 2017

@nicolas-grekas as @enumag wrote, commenting the line you mentioned didn't help (tested on PHP 7.1.1).

@nicolas-grekas
Copy link
Member

Fixed in #22311, thanks for the reproducer

fabpot added a commit that referenced this issue Apr 6, 2017
This PR was merged into the 2.8 branch.

Discussion
----------

[DI] Fix second auto-registration

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

My bad

Commits
-------

c02de84 [DI] Fix second auto-registration
@fabpot fabpot closed this as completed Apr 6, 2017
@enumag
Copy link
Contributor

enumag commented Apr 10, 2017

@nicolas-grekas Thanks, the fix works for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants