I'm trying to create a dynamic connection to databases.
For that I have:
// 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);
}
}
I got the DB name in the payload of JWT, like this:
// 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'];
}
}
These 2 features work independently. But how can I call my service's method (connectionService->dbName) in Connection.php?
I can't call my ConnectionService in the parameters of the Constructor, because it only accepts 4 parameters.