如何在我的插件中可靠地使用 Showpare 6 商店插件中的服务而不会出现问题

Lou*_*ann 3 symfony shopware shopware6

我们已经通过 Composer 从 Shopware 6 商店安装了一个插件。对于我们自己的功能,我们希望使用插件中的服务。

由于 shopware 6 中的插件可以卸载和/或停用,因此我们无法可靠地使用我们自己的插件/捆绑包中 services.xml 中插件中的服务。如果商店插件仍然被卸载/未激活,我们会收到错误“服务“...”依赖于不存在的服务”。

这个例子说明了这种情况:


    <services>
        <service id="MyPlugin..." public="true">
            <!-- Service might be uninstalled/inactive. Conditional?-->
            <argument type="service" id="ThirdPartyPlugin"/>
        </service>
    </services>

Run Code Online (Sandbox Code Playgroud)

在 Shopware 中是否有任何最佳实践来处理这种情况?我们是否可以实现一个条件,即仅当第 3 方插件处于活动状态时才会定义我们的服务?

j_e*_*ing 5

on-invalid="null"您可以在连接依赖项时使用,null如果不存在,那么您的服务将被注入而不是真正的服务,然后您的服务仍然必须处理在这种情况下应该发生的事情(例如提前返回,错误消息等),但是在至少 symfony 容器应该再次启动。

请参阅https://symfony.com/doc/current/service_container/optional_dependencies.html#setting-missing-dependencies-to-null更多信息。

因此,对于您的示例,它应该如下所示:

  <services>
        <service id="MyPlugin..." public="true">
            <!-- Service might be uninstalled/inactive. Null will be injected instead-->
            <argument type="service" id="ThirdPartyPlugin" on-invalid="null"/>
        </service>
    </services>
Run Code Online (Sandbox Code Playgroud)