Skip to content

Cannot autowire service Http\HttplugBundle\Collector\Formatter #210

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
soullivaneuh opened this issue Oct 6, 2017 · 9 comments
Closed

Comments

@soullivaneuh
Copy link
Contributor

soullivaneuh commented Oct 6, 2017

Q A
Bug? yes
New Feature? no
Version v1.7.1

Actual Behavior

I have the following error with no-debug env:

  [Symfony\Component\DependencyInjection\Exception\AutowiringFailedException]                                                                                                                                                                 
  Cannot autowire service "Http\HttplugBundle\Collector\Formatter": argument "$formatter" of method "__construct()" references interface "Http\Message\Formatter" but no such service exists.                                                 
  Cannot autowire service "httplug.factory.docker": argument "$formatter" of method "Http\HttplugBundle\Collector\ProfileClientFactory::__construct()" references class "Http\HttplugBundle\Collector\Formatter" but no such service exists.  

Expected Behavior

No error and service building like it works with debug setting.

Steps to Reproduce

Define the following services:

services:
  httplug.factory.docker:
    class: Http\HttplugBundle\Collector\ProfileClientFactory
    arguments:
      $factory: [ Docker\DockerClient, 'createFromEnv' ]
  Docker\Docker:
    arguments:
      $httpClient: '@httplug.client.docker'

And run:

./bin/console cache:clear --env=prod

Possible Solutions

See #210 (comment)

@soullivaneuh
Copy link
Contributor Author

soullivaneuh commented Oct 6, 2017

It seems this class is loaded only with profiling enable:

if ($profilingEnabled) {
$loader->load('data-collector.xml');

It seems legit, but it's the only way I found to get httplug logging with this client: https://github.com/docker-php/docker-php/blob/b1f822980dbbaad6c76fbcf1f17d26dfab7c7df6/src/DockerClient.php#L62-L94

Without having to re-define anything.

Related to: docker-php/docker-php#257

Maybe it would be great to have a CallbackClientFactory for this?

@soullivaneuh
Copy link
Contributor Author

soullivaneuh commented Oct 6, 2017

I finally solved my issue by creating this class:

use Http\HttplugBundle\ClientFactory\ClientFactory;

/**
 * Special factory that call another factory not implementing ClientFactory.
 *
 * @author Sullivan Senechal <[email protected]>
 */
final class CallableClientFactory implements ClientFactory
{
    /**
     * @var callable
     */
    private $factory;

    /**
     * @param callable $factory
     */
    public function __construct(callable $factory)
    {
        $this->factory = $factory;
    }

    /**
     * {@inheritdoc}
     */
    public function createClient(array $config = [])
    {
        return \call_user_func($this->factory, $config);
    }
}

And replacing the ProfileClientFactory by this one:

services:
  httplug.factory.docker:
    class: AppBundle\Httplug\ClientFactory\CallableClientFactory
    arguments:
      $factory: [ Docker\DockerClient, 'createFromEnv' ]
  Docker\Docker:
    arguments:
      $httpClient: '@httplug.client.docker'

If you think this is a good solution, I may write a PR for that.

@fbourigault
Copy link
Contributor

All collector stuff is internal and should not be used by other libraries/bundles.

@dbu
Copy link
Collaborator

dbu commented Oct 8, 2017

what exactly are you trying to achieve @soullivaneuh ? if its about logging, the idea would be to use the logger plugin with the plugin client: http://docs.php-http.org/en/latest/plugins/logger.html

if that plugin does not work for you, we need to better understand what you want to do to figure out what the right solution is.

@soullivaneuh
Copy link
Contributor Author

@dbu It's about logging. My solution currently works, but it's maybe not the simplier.

I also forget my configuration file:

httplug:
  plugins:
    logger: ~
  clients:
    default:
      factory: 'httplug.factory.guzzle6'
      plugins: ['httplug.plugin.logger']
    docker:
      factory: 'httplug.factory.docker'
      plugins: ['httplug.plugin.logger']
  profiling:
    captured_body_length: 1000

I have to do this because I have to use this part: https://github.com/docker-php/docker-php/blob/961e1d4f2b5d6617920babe440f585d1d517a763/src/DockerClient.php#L62-L95

Do you have a cleaner view of what I'm trying to do? Can it be done simpler?

Maybe a fix must be done on the docker-php library to create a proper factory.

@dbu
Copy link
Collaborator

dbu commented Oct 25, 2017

i think i would try to provide a factory. we provide an example in http://docs.php-http.org/en/latest/plugins/introduction.html#plugin-client-libraries - then you can have a DockerFactory::createFromEnv that does most of what the constructor does and don't need your own client class.

@dbu
Copy link
Collaborator

dbu commented Dec 26, 2017

@soullivaneuh did you find a solution, can we close this issue? or do we need to refactor something?

@soullivaneuh
Copy link
Contributor Author

@dbu I didn't get time to found a solution, but I think it should be solved on docker-php side: docker-php/docker-php#255 (comment)

You may close it if you want. I'll open a new one if something more precise has to be done.

@dbu
Copy link
Collaborator

dbu commented Dec 26, 2017

alright. sure, open a new issue or ask us on slack if you need something.

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

3 participants