DB::parseHttpDsn()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
namespace FFMVC\Helpers;
4
5
/**
6
 * Db Helper Class
7
 *
8
 * @package helpers
9
 * @author Vijay Mahrra <[email protected]>
10
 * @copyright (c) Copyright 2016 Vijay Mahrra
11
 * @license GPLv3 (http://www.gnu.org/licenses/gpl-3.0.html)
12
 */
13
class DB extends \Prefab
14
{
15
    /**
16
     * Parse a http-style dsn and return parts as an array
17
     *
18
     * @param string $httpDSN http-style dsn, e.g mysql://root:[email protected]:3306/development_db
19
     * @return array string of http dsn parameters, e.g.
20
     * {
21
     *      ["driver"]=> string(5) "mysql"
22
     *      ["scheme"] => string(5) "mysql"
23
     *      ["host"]=> string(9) "127.0.0.1"
24
     *      ["port"]=> int(3306)
25
     *      ["user"]=> string(4) "root"
26
     *      ["pass"]=> string(4) "root"
27
     *      ["name"]=> string(14) "development_db"
28
     *      ["path"]=> string(14) "development_db"
29
     * }
30
     * @see http://php.net/parse_url
31
     */
32
    public static function parseHttpDsn(string $httpDSN): array
33
    {
34
        $m = parse_url($httpDSN);
35
        if (false == $m) {
36
            throw new \FFMVC\InvalidArgumentException('Invalid DSN setting');
37
        }
38
        $m['path'] = substr($m['path'], 1);
39
        $m['name'] = $m['path'];
40
        $m['port'] = empty($m['port']) ? 3306 : (int) $m['port'];
41
        $m['driver'] = $m['scheme'];
42
        return $m;
43
    }
44
45
46
    /**
47
     * Return an f3 (pdo) dsn based on config params OR existing dsn param if set
48
     *
49
     * @param array $config e.g.
50
     * {
51
     *      ["driver"]=> string(5) "mysql"
52
     *      ["host"]=> string(9) "127.0.0.1"
53
     *      ["port"]=> int(3306)
54
     *      ["name"]=> string(14) "development_db"
55
     * }
56
     * @return string $dsn e.g. mysql:host=127.0.0.1;port=3306;dbname=development_db
57
     * @see http://php.net/manual/en/class.pdo.php
58
     */
59
    public static function createDbDsn(array $config): string
60
    {
61
        if (array_key_exists('dsn_http', $config)) {
62
            $config = array_merge($config, self::parseHttpDsn($config['dsn_http']));
63
        }
64
        if (array_key_exists('dsn', $config)) {
65
            return $config['dsn'];
66
        } else {
67
            return sprintf('%s:host=%s;port=%d;dbname=%s',
68
                $config['driver'], $config['host'], $config['port'], $config['name']
69
            );
70
        }
71
    }
72
73
74
    /**
75
     * Return a new \DB\SQL object from the dsn parameters
76
     *
77
     * @param string $dsn e.g. mysql:host=127.0.0.1;port=3306;dbname=development_db
78
     * @param string $user
79
     * @param string $pass
80
     * @param array $options options to PDO
81
     * @link https://fatfreeframework.com/sql
82
     * @see http://php.net/manual/en/class.pdo.php
83
     */
84
    public static function &newDsnDb(string $dsn, string $user, string $pass, array $options = []): \DB\SQL
85
    {
86
        $db = new \DB\SQL(
87
            $dsn,
88
            $user,
89
            $pass,
90
            array_key_exists('options', $options) ? [] : [\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION]
91
        );
92
        return $db;
93
    }
94
95
96
    /**
97
     * Return a new \DB\SQL object from the given parameters
98
     *
99
     * @param string|array http dsn string or arary params params for connection
100
     * Requires:
101
     * {
102
     *      ["driver"]=> string(5) "mysql"
103
     *      ["host"]=> string(9) "127.0.0.1"
104
     *      ["port"]=> int(3306)
105
     *      ["user"]=> string(4) "root"
106
     *      ["pass"]=> string(4) "root"
107
     *      ["name"]=> string(14) "development_db"
108
     * }
109
     * OR
110
     * PDO dsn and and username, password
111
     * @link https://fatfreeframework.com/sql
112
     * @see http://php.net/manual/en/class.pdo.php
113
     */
114
    public static function &newDb($params): \DB\SQL
115
    {
116
        if (is_string($params)) {
117
            $params = self::parseHttpDsn($params);
118
        }
119
120
        if (array_key_exists('dsn', $params)) {
121
            $dsn = $params['dsn'];
122
        } else {
123
            $dsn = self::createDbDsn($params);
124
        }
125
126
        return self::newDsnDb($dsn, $params['user'], $params['pass']);
127
    }
128
129
130
}
131