Symfony:覆盖第三方服务时出错



我在应用程序中使用第三方捆绑包进行通知。一切都很好,但我想覆盖该捆绑包的服务"NotificationManager"的某些部分,以便将通知与电子邮件结合起来。

错误完成所有操作后,我收到以下错误消息:

Type error: Argument 1 passed to MgiletNotificationBundleTwigNotificationExtension::__construct() must be an instance of MgiletNotificationBundleManagerNotificationManager, instance of NewsBundleServicesNotificationHelper given, called in /srv/http/sp/app/cache/dev/appDevDebugProjectContainer.php on line 4011

我所做的我遵循Symfony文档并添加了CompilerPass:

<?php
namespace NewsBundleDependencyInjection;
use SymfonyComponentDependencyInjectionCompilerCompilerPassInterface;
use SymfonyComponentDependencyInjectionContainerBuilder;
use NewsBundleServicesNotificationHelper;
class OverrideCompilerPass implements CompilerPassInterface {
public function process(ContainerBuilder $container)
{
$defNewService = $container->getDefinition('mgilet.notification');
$defNewService->setClass('NewsBundleServicesNotificationHelper');
}
}
?>

并将我的自定义捆绑包标记为NotificationBundle:的继承

<?php
namespace NewsBundle;
use SymfonyComponentHttpKernelBundleBundle;
use MgiletNotificationBundleMgiletNotificationBundle;
use SymfonyComponentDependencyInjectionContainerBuilder;
use NewsBundleDependencyInjectionOverrideCompilerPass;
class NewsBundle extends MgiletNotificationBundle
{
public function build(ContainerBuilder $container)
{
parent::build($container);
$container->addCompilerPass(new OverrideCompilerPass());
}
}

然后,我基本上从第三方捆绑包中复制并粘贴了服务类,只是为了再次检查一切是否正常。

<?php
namespace NewsBundleServices;
use DoctrineORMEntityManager;
use MgiletNotificationBundleModelAbstractNotification;
use MgiletNotificationBundleModelUserNotificationInterface;
class NotificationHelper{
private $om;
private $notification;
private $repository;
/**
* NotificationHelper constructor.
* @param EntityManager $om
* @param $notification
* @internal param $class
*/
public function __construct(EntityManager $om, $notification)
{
$this->om = $om;
$this->notification = $notification;
$this->repository = $om->getRepository($notification);
}
... all the functions from the bundle
}

现在我还需要一个Twig扩展来让一切正常工作,这里出现了我的错误:

<?php
namespace NewsBundleTwig;
use MgiletNotificationBundleTwigNotificationExtension as BaseExtension;
use NewsBundleServicesNotificationHelper;
use SymfonyComponentSecurityCoreAuthenticationTokenStorageTokenStorage;
use Twig_Extension;
class NotificationExtension extends BaseExtension
/**
* Twig extension to display notifications
**/
{
protected $notificationHelper;
protected $storage;
protected $twig;
/**
* NotificationExtension constructor.
* @param NotificationHelper $notificationHelper
* @param TokenStorage $storage
* @param Twig_Environment $twig
*/
public function __construct(NotificationHelper $notificationHelper, TokenStorage $storage, Twig_Environment $twig)
{
$this->notificationHelper = $notificationHelper;
$this->storage = $storage;
$this->twig = $twig;
}
... stuff happening
}

我认为通过替换construct函数的第一个参数和use语句会自动覆盖第三方捆绑包,但它似乎并没有真正起作用。

在我的config.yml文件中,我有:

notification_helper:
class: NewsBundleServicesNotificationHelper
arguments: ['@doctrine.orm.entity_manager', AppBundleEntityNotification]
notification.twig_extension:
class: NewsBundleTwigNotificationExtension
arguments: ['@notification_helper', '@security.token_storage', '@twig']
public: false
tags:
- { name: twig.extension }

更新

我编辑了services.yml,所以现在只剩下twig_extension作为服务,因为当我调试mgilet.notification-service时,它已经分配给了我的自定义service类,所以我认为它是一样的。。

所以现在我的文件中只有这个:

notification.twig_extension:
class: NewsBundleTwigNotificationExtension
arguments: ['@mgilet.notification', '@security.token_storage', '@twig']
public: false
tags:
- { name: twig.extension }

调试容器时,它甚至不会显示为服务,但错误消息保持不变:

Type error: Argument 1 passed to MgiletNotificationBundleTwigNotificationExtension::__construct() must be an instance of MgiletNotificationBundleManagerNotificationManager, instance of NewsBundleServicesNotificationHelper given, called in /srv/http/sp/app/cache/dev/appDevDebugProjectContainer.php on line 4000

我认为NotificationExtension需要NotificationManager类。你应该从services.yml发送这个类。但你正在发送你的服务类;NotificationHelper。您应该发送一个add,它就是NotificationManager类。所以NotificationHelper在NotificationBundle中。

相关内容

最新更新