我正在尝试创建与数据库的动态连接。
为此,我有:
// App/Services/Config/Database/Connection.php
<?php
namespace App\Service\Config\Database;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Configuration;
use Doctrine\Common\EventManager;
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class Connection extends \Doctrine\DBAL\Connection
{
public function __construct(
array $params,
Driver $driver,
?Configuration $config = null,
?EventManager $eventManager = null
)
{
$company = "api";
$db_name = "speyce_" . $company;
$params['dbname'] = $db_name;
parent::__construct($params, $driver, $config, $eventManager);
}
}
我在 JWT 的有效负载中得到了数据库名称,如下所示:
// App/Service/ConnectionService.php
<?php
namespace App\Service;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
class ConnectionService
{
public function dbName(
TokenStorageInterface $tokenStorageInterface,
JWTTokenManagerInterface $jwtManager
)
{
$decodedJwtToken = $jwtManager->decode($tokenStorageInterface->getToken());
return $decodedJwtToken['company'];
}
}
这两个功能独立工作。但是如何在 Connection.php 中调用我的服务方法(connectionService->dbName)?
我无法在构造函数的参数中调用我的 ConnectionService,因为它只接受 4 个参数。