我的專案需要幫助。我嘗試在使用 Messenger 的服務 (SendInBlueService) 呼叫中注入物體管理器,但 DependencyInjection 找不到 doctrine.orm.entity_manager。
我的考試路線
#[Route('api/testSendInBlue', name: 'testsendinblue')]
public function testMessenger(AsyncMethodService $asyncMethodService): Response
{
$asyncMethodService->async_low_priority(
SendInBlueService::class,
'confirmationMail',
[
$this->getUser()->getId()
]
);
return new Response('Test OK');
}
我的異步方法服務
<?php
namespace App\Service\Messenger;
use Symfony\Component\Messenger\MessageBusInterface;
class AsyncMethodService
{
private MessageBusInterface $messageBus;
public function __construct(MessageBusInterface $messageBus)
{
$this->messageBus = $messageBus;
}
public function async_low_priority(string $serviceName, string $methodName,array $params = [])
{
$this->messageBus->dispatch(new ServiceMethodCallMessageLowPriority(
$serviceName,
$methodName,
$params
)
);
}
public function async_medium_priority(string $serviceName, string $methodName,array $params = [])
{
$this->messageBus->dispatch(new ServiceMethodCallMessageMediumPriority(
$serviceName,
$methodName,
$params
)
);
}
public function async_high_priority(string $serviceName, string $methodName,array $params = [])
{
$this->messageBus->dispatch(new ServiceMethodCallMessageHighPriority(
$serviceName,
$methodName,
$params
));
}
}
我的 ServiceMethodCallMessageLowPriority 對于高和中完全相同
<?php
namespace App\Service\Messenger;
class ServiceMethodCallMessageLowPriority extends ServiceMethodCallMessage
{
}
我的服務方法呼叫訊息
<?php
namespace App\Service\Messenger;
class ServiceMethodCallMessage
{
private string $serviceName;
private string $methodName;
private array $params;
public function __construct(string $serviceName, string $methodName, array $params = [])
{
$this->serviceName = $serviceName;
$this->methodName = $methodName;
$this->params = $params;
}
/**
* @return string
*/
public function getServiceName(): string
{
return $this->serviceName;
}
/**
* @return string
*/
public function getMethodName(): string
{
return $this->methodName;
}
/**
* @return array
*/
public function getParams(): array
{
return $this->params;
}
}
我的 MessengerHandle
<?php
namespace App\Service\Messenger;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
#[AsMessageHandler]
class ServiceMethodCallHandler extends AbstractController
{
private string $path;
public function __construct(string $path)
{
$this->path = $path;
}
public function __invoke(
ServiceMethodCallMessageLowPriority |
ServiceMethodCallMessageMediumPriority |
ServiceMethodCallMessageHighPriority $message
)
{
$containerBuilder = new ContainerBuilder();
$loader = new YamlFileLoader($containerBuilder, new FileLocator($this->path));
$loader->load('services.yaml');
$callable = [
$containerBuilder->get($message->getServiceName()),
$message->getMethodName()
];
call_user_func_array($callable,$message->getParams());
}
}
我的服務發送藍色
<?php
namespace App\Service;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
class SendInBlueService
{
private string $SEND_IN_BLUE_API_KEY;
private EntityManagerInterface $entityManager;
public function __construct(
string $SEND_IN_BLUE_API_KEY,
EntityManagerInterface $entityManager
)
{
$this->SEND_IN_BLUE_API_KEY = $SEND_IN_BLUE_API_KEY;
$this->entityManager = $entityManager;
}
public function confirmationMail(int $userId)
{
dd($this->entityManager);
$user = $this->entityManager->getRepository(User::class)->find($userId);
dd($user);
}
}
我的配置/services.yaml
parameters:
SEND_IN_BLUE_API_KEY: '%env(SEND_IN_BLUE_API_KEY)%'
services:
# default configuration for services in *this* file
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
App\:
resource: '../src/'
exclude:
- '../src/Entity/'
- '../src/Kernel.php'
- '../src/DependencyInjection/'
# Messenger Declaration Service
App\Service\Messenger\ServiceMethodCallHandler:
arguments: ['%kernel.project_dir%/config']
App\Service\SendInBlueService:
class: App\Service\SendInBlueService
arguments: ['%env(SEND_IN_BLUE_API_KEY)%','@doctrine.orm.entity_manager']
為了完成我的錯誤:

我嘗試在我的服務中使用 ContainerBuilder 獲取 EntityManager,但我有同樣的錯誤。
我想我有一個問題,因為 Messenger 使用其他內核實體,并且在這個實體中,DependencyInjection 沒有加載所有包。
如果有人有想法。謝謝
uj5u.com熱心網友回復:
我發現通過使用別名的自動裝配/依賴注入總是非常混亂(什么是可見的,什么是隱藏的,等等),并且通常選擇使用默認引數系結:

我通過在 __invoke 方法中構建新容器來完成。這個構建我可以呼叫我的服務,但我創建了另一個錯誤。通過@Cerad 訊息,我了解到在呼叫中創建新容器是錯誤的。我重新開始我的第一個錯誤。
我通過在 service.yaml 中添加公共來找到問題
services:
# default configuration for services in *this* file
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
# 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/Entity/'
- '../src/Kernel.php'
- '../src/DependencyInjection/'
# Messenger Declaration Service
App\Service\Messenger\ServiceMethodCallHandler:
arguments: ['@service_container']
App\Service\SendInBlueService:
public: true
class: App\Service\SendInBlueService
arguments: ['@doctrine.orm.entity_manager','%env(SEND_IN_BLUE_API_KEY)%']
謝謝你們
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/533877.html
