Вызовите Сервис в Соединении, который расширяет \ Doctrine \ DBAL \ Connection - PullRequest
0 голосов
/ 08 октября 2019

Я пытаюсь создать динамическое соединение с базами данных.

Для этого у меня есть:

// 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'];
    }
}

Эти 2 функции работают независимо. Но как я могу вызвать метод моего сервиса (connectionService-> dbName) в Connection.php?

Я не могу вызвать свой ConnectionService в параметрах Конструктора, потому что он принимает только 4 параметра.

Спасибо за вашу помощь.

1 Ответ

0 голосов
/ 08 октября 2019

Я думаю, что вы можете добавить его в конструктор, например: // 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 App\Service\ConnectionService;
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;

class Connection extends \Doctrine\DBAL\Connection
{
    /**
     *@var ConnectionService
     */
    protected $connectionService;

    public function __construct(
        ConnectionService $connectionService
        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);
        $this->$connectionService = $connectionService;
    }        
}

Или с помощью инжектора установки:

namespace App\Service\Config\Database;

use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Configuration;
use Doctrine\Common\EventManager;
use App\Service\ConnectionService;
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;

class Connection extends \Doctrine\DBAL\Connection
{
    /**
     *@var ConnectionService
     */
    protected $connectionService;

    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);
    }   

    public function setConnectionService(ConnectionService $cs){
       $this->connectionService = $cs
    }     
}

В твоих services.yml

  connection.service:
      class: App\Service\ConnectionService

    App\Service\Config\Database\Connection:
      calls:
        - ['setConnectionService', ["@connection.service"]]
...