我在我的应用程序中使用第三方软件包进行通知。它一切正常,但我想覆盖服务的某些部分" NotificationManager"该捆绑包,以便将通知与电子邮件相结合。
错误 实现一切后,我收到以下错误消息:
Type error: Argument 1 passed to Mgilet\NotificationBundle\Twig\NotificationExtension::__construct() must be an instance of Mgilet\NotificationBundle\Manager\NotificationManager, instance of NewsBundle\Services\NotificationHelper given, called in /srv/http/sp/app/cache/dev/appDevDebugProjectContainer.php on line 4011
我做了什么 我按照Symfony文档并添加了CompilerPass:
<?php
namespace NewsBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use NewsBundle\Services\NotificationHelper;
class OverrideCompilerPass implements CompilerPassInterface {
public function process(ContainerBuilder $container)
{
$defNewService = $container->getDefinition('mgilet.notification');
$defNewService->setClass('NewsBundle\Services\NotificationHelper');
}
}
?>
并将我的自定义包标记为NotificationBundle的继承:
<?php
namespace NewsBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Mgilet\NotificationBundle\MgiletNotificationBundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use NewsBundle\DependencyInjection\OverrideCompilerPass;
class NewsBundle extends MgiletNotificationBundle
{
public function build(ContainerBuilder $container)
{
parent::build($container);
$container->addCompilerPass(new OverrideCompilerPass());
}
}
然后,我基本上复制并粘贴了第三方软件包中的服务类,只是为了仔细检查一切是否仍然正常。
<?php
namespace NewsBundle\Services;
use Doctrine\ORM\EntityManager;
use Mgilet\NotificationBundle\Model\AbstractNotification;
use Mgilet\NotificationBundle\Model\UserNotificationInterface;
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 NewsBundle\Twig;
use Mgilet\NotificationBundle\Twig\NotificationExtension as BaseExtension;
use NewsBundle\Services\NotificationHelper;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
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
}
我认为通过替换构造函数的第一个参数,use语句会自动覆盖第三方包,但似乎它并没有真正起作用。
在我的config.yml文件中,我有:
notification_helper:
class: NewsBundle\Services\NotificationHelper
arguments: ['@doctrine.orm.entity_manager', AppBundle\Entity\Notification]
notification.twig_extension:
class: NewsBundle\Twig\NotificationExtension
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: NewsBundle\Twig\NotificationExtension
arguments: ['@mgilet.notification', '@security.token_storage', '@twig']
public: false
tags:
- { name: twig.extension }
调试容器时,它甚至不显示为服务,但错误信息保持不变:
Type error: Argument 1 passed to Mgilet\NotificationBundle\Twig\NotificationExtension::__construct() must be an instance of Mgilet\NotificationBundle\Manager\NotificationManager, instance of NewsBundle\Services\NotificationHelper given, called in /srv/http/sp/app/cache/dev/appDevDebugProjectContainer.php on line 4000
答案 0 :(得分:0)
我认为NotificationExtension需要NotificationManager类。您应该从services.yml发送此类。但是你要发送你的服务类; NotificationHelper。您应该发送添加一个,它是Notification Manager类。所以NotificationHelper在NotificationBundle中。