1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* Copyright (C) 2020-2025 Iain Cambridge |
7
|
|
|
* |
8
|
|
|
* This program is free software: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as published by |
10
|
|
|
* the Free Software Foundation, either version 2.1 of the License, or |
11
|
|
|
* (at your option) any later version. |
12
|
|
|
* |
13
|
|
|
* This program is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
* GNU Lesser General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU General Public License |
19
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace Parthenon\MultiTenancy\Dbal; |
23
|
|
|
|
24
|
|
|
use Doctrine\Common\EventManager; |
25
|
|
|
use Doctrine\DBAL\Configuration; |
26
|
|
|
use Doctrine\DBAL\Connection; |
27
|
|
|
use Doctrine\DBAL\Driver; |
28
|
|
|
use Doctrine\DBAL\Event\ConnectionEventArgs; |
29
|
|
|
use Doctrine\DBAL\Events; |
30
|
|
|
use Parthenon\MultiTenancy\Entity\TenantInterface; |
31
|
|
|
use Parthenon\MultiTenancy\TenantProvider\TenantProviderInterface; |
32
|
|
|
|
33
|
|
|
class TenantConnection extends Connection |
34
|
|
|
{ |
35
|
|
|
private TenantProviderInterface $currentTenantProvider; |
36
|
|
|
private TenantInterface $tenant; |
37
|
|
|
private bool $connected = false; |
38
|
|
|
|
39
|
|
|
private array $params; |
40
|
|
|
|
41
|
|
|
public function __construct(array $params, Driver $driver, ?Configuration $config = null, ?EventManager $eventManager = null) |
42
|
|
|
{ |
43
|
|
|
$this->params = $params; |
44
|
|
|
parent::__construct($params, $driver, $config, $eventManager); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function setCurrentTenantProvider(TenantProviderInterface $currentTenantProvider): void |
48
|
|
|
{ |
49
|
|
|
$this->currentTenantProvider = $currentTenantProvider; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function connect(bool $refresh = false): bool |
53
|
|
|
{ |
54
|
|
|
if ($this->connected && !$refresh) { |
55
|
|
|
return false; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$tenant = $this->currentTenantProvider->getCurrentTenant(); |
59
|
|
|
|
60
|
|
|
$this->close(); |
61
|
|
|
$this->tenant = $tenant; |
62
|
|
|
|
63
|
|
|
$this->params['dbname'] = $tenant->getDatabase(); |
64
|
|
|
$this->_conn = $this->_driver->connect($this->params); |
65
|
|
|
$this->connected = true; |
66
|
|
|
if ($this->_eventManager->hasListeners(Events::postConnect)) { |
|
|
|
|
67
|
|
|
$eventArgs = new ConnectionEventArgs($this); |
|
|
|
|
68
|
|
|
$this->_eventManager->dispatchEvent(Events::postConnect, $eventArgs); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return true; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|