src/Extension/Doctrine/Connection/ConnectionWrapper.php line 62

Open in your IDE?
  1. <?php
  2. namespace App\Extension\Doctrine\Connection;
  3. use Doctrine\DBAL\Connection;
  4. use Doctrine\DBAL\Event\ConnectionEventArgs;
  5. use Doctrine\DBAL\Events;
  6. use Symfony\Component\HttpFoundation\Session\Session;
  7. /**
  8.  * @author Dawid zulus Pakula [zulus@w3des.net]
  9.  */
  10. class ConnectionWrapper extends Connection
  11. {
  12.     const SESSION_ACTIVE_DYNAMIC_CONN 'cliente_connection';
  13.     /**
  14.      * @var Session
  15.      */
  16.     private $session;
  17.     /**
  18.      * @var bool
  19.      */
  20.     private $_isConnected false;
  21.     /**
  22.      * @param Session $sess
  23.      * @return void
  24.      */
  25.     public function setSession(Session $sess)
  26.     {
  27.         $this->session $sess;
  28.     }
  29.     /**
  30.      * @param string $dbname
  31.      * @param string|null $host
  32.      * @return void
  33.      */
  34.     public function forceSwitch(string $dbname, ?string $host NULL)
  35.     {
  36.         if ( $this->session->has(self::SESSION_ACTIVE_DYNAMIC_CONN) ) {
  37.             $session $this->session->get(self::SESSION_ACTIVE_DYNAMIC_CONN);
  38.             if ( $session['dbname'] === $dbname ) {
  39.                 return;
  40.             }
  41.         }
  42.         $this->session->set(self::SESSION_ACTIVE_DYNAMIC_CONN, ['dbname'=>$dbname,'host'=>$host]);
  43.         if ( $this->isConnected() ) {
  44.             $this->close();
  45.         }
  46.     }
  47.     /**
  48.      * {@inheritdoc}
  49.      */
  50.     public function connect()
  51.     {
  52.         if ( !$this->session->has(self::SESSION_ACTIVE_DYNAMIC_CONN) ) {
  53.             throw new \InvalidArgumentException('You have to inject into valid context first');
  54.         }
  55.         if ( $this->isConnected() ) {
  56.             return true;
  57.         }
  58.         $clienteParams $this->session->get(self::SESSION_ACTIVE_DYNAMIC_CONN);
  59.         $params $this->getParams();
  60.         $params['dbname'] = $clienteParams['dbname'];
  61.         if ( NULL !== $clienteParams['host'] ) {
  62.             $params['host'] = $clienteParams['host'];
  63.         }
  64.         $this->_conn $this->_driver->connect(
  65.              $params
  66.             ,$params['user']
  67.             ,$params['password']
  68.         );
  69.         if ( $this->_eventManager->hasListeners(Events::postConnect) ) {
  70.             $eventArgs = new ConnectionEventArgs($this);
  71.             $this->_eventManager->dispatchEvent(Events::postConnect$eventArgs);
  72.         }
  73.         $this->_isConnected true;
  74.         return $this->_isConnected;
  75.     }
  76.     /**
  77.      * {@inheritdoc}
  78.      */
  79.     public function isConnected()
  80.     {
  81.         return $this->_isConnected;
  82.     }
  83.     /**
  84.      * {@inheritdoc}
  85.      */
  86.     public function close()
  87.     {
  88.         if ($this->isConnected()) {
  89.             parent::close();
  90.             $this->_isConnected false;
  91.         }
  92.     }
  93. }