使用带有自动加载和参数的symfony4麻烦的外部存储库 [英] Use an external repository with symfony4 trouble with autoload and parameters

查看:59
本文介绍了使用带有自动加载和参数的symfony4麻烦的外部存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用位于github上的两个自行开发的库作为私有存储库,因此可以在多个项目中重用它.我通过作曲家将它们包括在内:

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"
            }
        }
    }
],

现在,symfony抱怨找不到类和服务.我的第一个问题是:为什么它们不像symfony正在使用的库中的其他类那样自动加载swiftmailer,phpmd,diablomedia/prettyprinter ?都是库,默认情况下它们不是symfony的一部分.

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.

但是这里的其他答案说,我必须在我的services.yaml中手动添加服务 Symfony4使用外部类库作为服务

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

因此我将库中的服务负载添加到services.yaml文件中:

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
....

现在php bin/控制台调试:自动装配给我以下错误:

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?

推荐答案

我认为您不必分别导入每个服务.您已经通过"Puc \ SapClient"完成了此操作.部分.

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.

在symfony中示例项目有这部分,例如"services.yaml":

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}'

那么您应该是:

# 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.

这篇关于使用带有自动加载和参数的symfony4麻烦的外部存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆