Search code examples
symfonydependenciesautowiredcode-injection

(Symfony 4) Unable to inject a github library that I installed with composer


I installed the following library from GitHub: https://github.com/rosell-dk/webp-convert

The location of the main class that I need to is (from project root) :

\vendor\rosell-dk\webp-convert\src\WebPConvert.php

Here is how the WebPConvert.php class starts out:

namespace WebPConvert;

use WebPConvert\Converters\ConverterHelper;
use WebPConvert\ServeExistingOrConvert;
use WebPConvert\Serve\ServeExistingOrHandOver;

class WebPConvert
{

In the repository class where I use it, here is how I tried to do my dependency injection:

use WebPConvert\WebPConvert;

class PhotoRepository extends ServiceEntityRepository
{

    /**
     * @var WebPConvert
     */
    protected $webPConverter;

    public function __construct(WebPConvert $webPConverter)
    {
        $this->webPConverter = $webPConverter;
    }

I followed the instructions from https://symfony.com/doc/current/service_container.html

But I still get this message:

Cannot autowire service "App\Repository\PhotoRepository": argument "$webPConverter" of method "__construct()" references class "WebPConvert\WebPConvert" but no such service exists.

I have even tried putting this in my services.yaml and it doesn't work:

App\Repository\PhotoRepository:
    arguments:
        - WebPConvert\WebPConvert

Is there an extra step I am missing?


Solution

  • This is Cerad's answer which worked:

    WebPConvert is not a Symfony bundle so it won't have any services defined. You will have to define them manually. Actually, from the readme file, it looks like WebPConvert::convert is a static method so there is nothing to inject. Just follow the example.