dbUser = $dbUser; $this->dbPassword = $dbPassword; $this->user = $user; } public function createConnection(): Connection { if (!$this->user->isLoggedIn()) { throw new \LogicException('Uživatel není přihlášen.'); } $identity = $this->user->getIdentity(); $host = $identity->server ?? null; $databaseName = $identity->database ?? null; $firmaId = $identity->firma_id ?? null; if (empty($host) || empty($databaseName) || empty($firmaId)) { throw new \LogicException('Přihlášený uživatel nemá v tabulce LOGIN definovaný SERVER, DATABASE nebo FIRMA_ID.'); } return $this->createTenantConnection((string) $host, (string) $databaseName, (int) $firmaId); } public function createExplorer(): Explorer { return $this->createExplorerFromConnection($this->createConnection()); } public function createTenantExplorer(string $host, string $databaseName, int $firmaId): Explorer { return $this->createExplorerFromConnection( $this->createTenantConnection($host, $databaseName, $firmaId) ); } private function createTenantConnection(string $host, string $databaseName, int $firmaId): Connection { $dsn = "sqlsrv:Server={$host};Database={$databaseName};LoginTimeout=10;"; $connection = new Connection( $dsn, $this->dbUser, $this->dbPassword, [ 'lazy' => false, 'driverOptions' => [ \PDO::SQLSRV_ATTR_ENCODING => \PDO::SQLSRV_ENCODING_UTF8, ] ] ); $connection->query('EXEC sp_set_session_context @key = N\'FirmaId\', @value = ?', $firmaId); return $connection; } private function createExplorerFromConnection(Connection $connection): Explorer { $storage = new MemoryStorage(); $structure = new Structure($connection, $storage); return new Explorer($connection, $structure); } }