Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.

Autowiring for Flagception\Manager\FeatureManagerInterface does not work as stated in the docs #75

Closed
dsentker opened this issue Mar 3, 2020 · 10 comments
Assignees
Labels

Comments

@dsentker
Copy link

dsentker commented Mar 3, 2020

This bundle is used in an PHP 7.3 / Symfony 4.4 application. There is a service which expects an Flagception\Manager\FeatureManagerInterface instance in the constructor:

use Flagception\Manager\FeatureManagerInterface;

// [...] 

/** @var FeatureManagerInterface */
private $featureManager;

public function __construct(FeatureManagerInterface $featureManager)
{
    $this->featureManager = $featureManager;
}

Symfony throws a RuntimeException now:

Cannot autowire service "App\Foo\Bar": argument "$featureManager" of method "__construct()" references interface "Flagception\Manager\FeatureManagerInterface" but no such service exists. You should maybe alias this interface to the existing "flagception.manager.feature_manager" service.

The documentation does not mention that I have to add the following entry in the services.yaml:

# [...]
Flagception\Manager\FeatureManagerInterface:
    alias: flagception.manager.feature_manager

# [...]

As soon as this addition has been added, the autowiring works as expected. Was it forgotten in the documentation or did I do something wrong?

@migo315
Copy link
Contributor

migo315 commented Mar 3, 2020

This should work normally. The we have defined an alias for the FeatureManagerInterface here: https://github.com/bestit/flagception-bundle/blob/3.x/src/Resources/config/services.yml#L9

Do you have activate autowire in your project (https://symfony.com/doc/current/service_container/autowiring.html)?

# config/services.yaml
services:
    _defaults:
        autowire: true

@dsentker
Copy link
Author

dsentker commented Mar 3, 2020

Yes:

services:
    _defaults:
        autowire:      true
        autoconfigure: true
        bind:
            $projectDir: '%kernel.project_dir%'

The same thing happens if I type-hint the FeatureManager instead of the FeatureManagerInterface.

Maybe it is because flagception.manager.feature_manager is not autowired / autoconfigured?

λ php bin/console debug:container Flagception

 Select one of the following services to display its information:
  [0 ] flagception.configurator.array_configurator
  [1 ] flagception.configurator.constraint_configurator
  [2 ] flagception.configurator.environment_configurator
  [3 ] flagception.configurator.contentful_configurator
  [4 ] flagception.configurator.cookie_configurator
  [5 ] flagception.manager.feature_manager
  [6 ] flagception.expression_language
  [7 ] flagception.twig.toggle_extension
  [8 ] flagception.profiler.feature_data_collector
  [9 ] flagception.bag.feature_result_bag
  [10] flagception.bag.feature_config_bag
  [11] flagception.constraint.constraint_resolver
  [12] flagception.factory.expression_language_factory
  [13] flagception.constraint_provider.date_provider
  [14] flagception.constraint_provider.match_provider
  [15] flagception.activator.chain_activator
  [16] flagception.decorator.chain_decorator
  [17] flagception.listener.routing_metadata_subscriber
  [18] flagception.activator.array_activator
  [19] flagception.activator.constraint_activator
  [20] flagception.activator.environment_activator
 > 5


Information for Service "flagception.manager.feature_manager"
=============================================================

 Class FeatureManager

 ---------------- -------------------------------------
  Option           Value
 ---------------- -------------------------------------
  Service ID       flagception.manager.feature_manager
  Class            Flagception\Manager\FeatureManager
  Tags             -
  Public           yes
  Synthetic        no
  Lazy             no
  Shared           yes
  Abstract         no
  Autowired        no
  Autoconfigured   no
 ---------------- -------------------------------------

@dsentker
Copy link
Author

dsentker commented Mar 3, 2020

If i inject the FeatureManager manually, it works too.

App\Foo\Bar:
    arguments: ['@flagception.manager.feature_manager']

@migo315
Copy link
Contributor

migo315 commented Mar 3, 2020

The same thing happens if I type-hint the FeatureManager instead of the FeatureManagerInterface.

Yes, the FeatureManger is not aliased and should not work. But FeatureManagerInterface should work. We use always flagception with autowire. I don't have to define this manually in any project until now. That's very strange.

Maybe it is because flagception.manager.feature_manager is not autowired / autoconfigured?

No, thats irrelevant.

I will try to reproduce it in the next days. You can set it manually until it is solved (like you already do).

@migo315 migo315 added the bug label Mar 3, 2020
@migo315 migo315 self-assigned this Mar 3, 2020
@migo315
Copy link
Contributor

migo315 commented Mar 3, 2020

Which version do you have installed?

@dsentker
Copy link
Author

dsentker commented Mar 3, 2020

Thank you for your help.
flagception/flagception 1.5.0
flagception/flagception-bundle 3.0.0
Symfony 4.4.5

@dsentker
Copy link
Author

dsentker commented Mar 3, 2020

I created an example repository which reproduces this issue:
https://github.com/dsentker/flagception-invalid-autowiring
The FeatureManagerInterface injection is here: App\Service\FooService::__construct()
I created two simple tests to check if the service is injected correct. Alternatively, you can start the application with symfony serve and use your browser to access the start page.

In addition I have the following comments:

  • I first tried to use symfony new xyz --full --version=4.4. With this setup, i was not able to install this bundle with composer due to the following error. It seems that the ToggleExtension refers to the deprecated Twig_Extension class.

CRITICAL [php] Uncaught Error: Class 'Twig_Extension' not found ["exception" => Error { …}]
In ToggleExtension.php line 17:
!! Attempted to load class "Twig_Extension" from the global namespace.
!! Did you forget a "use" statement?

  • I then copied the relevant parts of my composer.json, which I also use in my application (where the problem occurred). Then i was able to install your bundle.
  • The entries from composer.json come from an installation of Symfony 4.1, which I then updated regularly with composer update up to version 4.4.5. But I don't think that's relevant.

I hope that helps!

@dsentker
Copy link
Author

dsentker commented Mar 3, 2020

OK, sorry for the comment spam, but i found the issue.

I installed your bundle with composer require flagception/flagception-bundle. For a reason i do not know, composer decided to add the following line to my composer.json: "flagception/flagception-bundle": "3",

This is wrong, as this states that the v3.0.0 is requested (instead of 3.5.1). The 3.0.0 version from your bundle is missing the Interface-alias line in the services.yaml.

So the solution is too simple: I changed the line in the composer.json to "flagception/flagception-bundle": "^3" and now it works.

The only question that remains: Why does composer write the explicit reference to version 3.0.0 ("3") in the composer.json?

@dsentker
Copy link
Author

dsentker commented Mar 3, 2020

I created #76 to avoid misunderstandings (so, and now I'm no longer annoying) ;-)

@migo315
Copy link
Contributor

migo315 commented May 25, 2020

Thanks for your work. ;-)

I think we can close this issue.

@migo315 migo315 closed this as completed May 25, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants