Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
621 lars 1
<?php
2
 
3
namespace Doctrine\DBAL\Driver\PgSQL;
4
 
5
use Doctrine\DBAL\Driver\AbstractPostgreSQLDriver;
6
use ErrorException;
7
use SensitiveParameter;
8
 
9
use function addslashes;
10
use function array_filter;
11
use function array_keys;
12
use function array_map;
13
use function array_slice;
14
use function array_values;
15
use function func_get_args;
16
use function implode;
17
use function pg_connect;
18
use function restore_error_handler;
19
use function set_error_handler;
20
use function sprintf;
21
 
22
use const PGSQL_CONNECT_FORCE_NEW;
23
 
24
final class Driver extends AbstractPostgreSQLDriver
25
{
26
    /** {@inheritdoc} */
27
    public function connect(
28
        #[SensitiveParameter]
29
        array $params
30
    ): Connection {
31
        set_error_handler(
32
            static function (int $severity, string $message) {
33
                throw new ErrorException($message, 0, $severity, ...array_slice(func_get_args(), 2, 2));
34
            },
35
        );
36
 
37
        try {
38
            $connection = pg_connect($this->constructConnectionString($params), PGSQL_CONNECT_FORCE_NEW);
39
        } catch (ErrorException $e) {
40
            throw new Exception($e->getMessage(), '08006', 0, $e);
41
        } finally {
42
            restore_error_handler();
43
        }
44
 
45
        if ($connection === false) {
46
            throw new Exception('Unable to connect to Postgres server.');
47
        }
48
 
49
        $driverConnection = new Connection($connection);
50
 
51
        if (isset($params['application_name'])) {
52
            $driverConnection->exec('SET application_name = ' . $driverConnection->quote($params['application_name']));
53
        }
54
 
55
        return $driverConnection;
56
    }
57
 
58
    /**
59
     * Constructs the Postgres connection string
60
     *
61
     * @param array<string, mixed> $params
62
     */
63
    private function constructConnectionString(
64
        #[SensitiveParameter]
65
        array $params
66
    ): string {
67
        $components = array_filter(
68
            [
69
                'host' => $params['host'] ?? null,
70
                'port' => $params['port'] ?? null,
71
                'dbname' => $params['dbname'] ?? 'postgres',
72
                'user' => $params['user'] ?? null,
73
                'password' => $params['password'] ?? null,
74
                'sslmode' => $params['sslmode'] ?? null,
75
            ],
76
            static fn ($value) => $value !== '' && $value !== null,
77
        );
78
 
79
        return implode(' ', array_map(
80
            static fn ($value, string $key) => sprintf("%s='%s'", $key, addslashes($value)),
81
            array_values($components),
82
            array_keys($components),
83
        ));
84
    }
85
}