-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSchemaConnection.php
More file actions
56 lines (42 loc) · 1.37 KB
/
SchemaConnection.php
File metadata and controls
56 lines (42 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
declare(strict_types=1);
namespace Macpaw\PostgresSchemaBundle\Doctrine;
use Doctrine\DBAL\Connection as DBALConnection;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Macpaw\PostgresSchemaBundle\Exception\UnsupportedPlatformException;
use Macpaw\SchemaContextBundle\Service\BaggageSchemaResolver;
class SchemaConnection extends DBALConnection
{
private static ?BaggageSchemaResolver $schemaResolver = null;
public static function setSchemaResolver(BaggageSchemaResolver $resolver): void
{
self::$schemaResolver = $resolver;
}
public function connect(): bool
{
$connection = parent::connect();
if (!self::$schemaResolver) {
return $connection;
}
$schema = self::$schemaResolver->getSchema();
if (!$schema) {
return $connection;
}
$this->ensurePostgreSql();
$this->applySearchPath($schema);
return $connection;
}
private function ensurePostgreSql(): void
{
$platform = $this->getDatabasePlatform();
if (!$platform instanceof PostgreSQLPlatform) {
throw new UnsupportedPlatformException(get_class($platform));
}
}
private function applySearchPath(string $schema): void
{
if ($this->_conn !== null) {
$this->_conn->exec('SET search_path TO ' . $schema);
}
}
}