symfonyautowiredautoload

Use an external repository with symfony4 trouble with autoload and parameters


I use two self developed libraries located in github as a private repository so I can reuse it in several projects. I include them via composer:

"license": "proprietary",
"repositories": [
    {
        "type": "vcs",
        "url": "https://github.com/my-account/puc-web-sap-client.git",
        "options": {
            "ssl": {
                "verify_peer": "false"
            }
        }
    },
    {
        "type": "vcs",
        "url": "https://github.com/my-account/puc-web-soap-client.git",
        "options": {
            "ssl": {
                "verify_peer": "false"
            }
        }
    }
],

Now symfony complains that the classes and services cannot be found. My first question is: Why are they not autoloaded like other classes from libraries symfony is using e.g. swiftmailer, phpmd, diablomedia/prettyprinter ? All are libraries, which are not by default part of symfony.

However other answers here said, that I have to add the services manually in my services.yaml Symfony4 use external class library as a service

So I added loads of services from my library to the services.yaml file:

Puc\SapClient\:
    resource: '../vendor/puc/sap-client/src/*'
    autowire: true      # Automatically injects dependencies in your services.
    autoconfigure: true # Automatically registers your services as commands, event subscriber
    public: true


Puc\SapClient\Country\CountrySapServiceInterface:
    alias: Puc\SapClient\Country\CountrySapService

Puc\SapClient\Country\CountryService:
    autowire: true

Puc\SapClient\Currency\CurrencyService:
    autowire: true

Puc\SapClient\House\HouseService:
    autowire: true

Puc\SapClient\Ressort\RessortService:
    autowire: true

Puc\SapClient\Country\CountrySapService:
    autowire: true

Puc\SapClient\Currency\CurrencySapService:
    autowire: true
....

now php bin/console debug:autowiring gives me the following error:

Cannot autowire service "Puc\SapClient\Model\Currency": argument "$currencyIsoCode" of method "__construct()" is type-hinted "string", you should configure its value explicitly.

Well Currency is a Model and not a service. It is filled with values by my library and given back to my main app. This is the constructor:

public function __construct(
    string $currencyIsoCode,
    string $name,
    string $ident,
    int $numberOfDecimals
) {
    $this->currencyIsoCode = $currencyIsoCode;
    $this->name = $name;
    $this->sapIdent = $ident;
    $this->numberOfDecimals = $numberOfDecimals;
}

How can I configure this model to use strings? where do I do it? Do I really have to declare each and every single class my library is using? To define each parameter of each class?


Solution

  • I think that you don't have to import each service separately. Your are already doing that with the "Puc\SapClient" part.

    The problem could be that you are importing your models, which should not be imported.

    In the symfony example project there is this part vor "services.yaml":

    # makes classes in src/ available to be used as services
      # this creates a service per class whose id is the fully-qualified class name
      App\:
        resource: '../src/*'
        exclude: '../src/{Bundle,DependencyInjection,Entity,Model,Migrations,Tests,Kernel.php}'
    

    Then your part would be:

    # makes classes in src/ available to be used as services
      # this creates a service per class whose id is the fully-qualified class name
      Puc\SapClient\:
        resource: '../vendor/puc/sap-client/src/*'
        exclude: ''../vendor/puc/sap-client/src/{Entity,Model,"etc."}'
    

    "etc." Would be everything that is not needed as service.