Passed
Push — main ( 1f925a...20361b )
by Thierry
04:43 queued 02:11
created

AppDriver::_grammar()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Lagdo\DbAdmin\Db;
4
5
use Lagdo\DbAdmin\Driver\Db\ConnectionInterface;
6
use Lagdo\DbAdmin\Driver\Db\StatementInterface;
7
use Lagdo\DbAdmin\Driver\Driver;
8
use Lagdo\DbAdmin\Driver\DriverInterface;
9
use Lagdo\DbAdmin\Driver\Driver\DatabaseInterface;
10
use Lagdo\DbAdmin\Driver\Driver\GrammarInterface;
11
use Lagdo\DbAdmin\Driver\Driver\QueryInterface;
12
use Lagdo\DbAdmin\Driver\Driver\ServerInterface;
13
use Lagdo\DbAdmin\Driver\Driver\TableInterface;
14
use Closure;
15
16
use function Jaxon\jaxon;
17
18
/**
19
 * Add callbacks to the driver features.
20
 */
21
class AppDriver extends Driver
22
{
23
    /**
24
     * @var array
25
     */
26
    private array $callbacks = [];
27
28
    /**
29
     * @param Driver $driver
30
     */
31
    public function __construct(protected Driver $driver)
32
    {
33
        // "Clone" the driver instance.
34
        $this->utils = $driver->utils;
35
        $this->config = $driver->config;
36
        $this->mainConnection = $driver->mainConnection;
37
        $this->connection = $driver->connection;
38
    }
39
40
    /**
41
     * @var ServerInterface
42
     */
43
    protected function _server(): ServerInterface
44
    {
45
        return $this->driver->_server();
46
    }
47
48
    /**
49
     * @var DatabaseInterface
50
     */
51
    protected function _database(): DatabaseInterface
52
    {
53
        return $this->driver->_database();
54
    }
55
56
    /**
57
     * @var TableInterface
58
     */
59
    protected function _table(): TableInterface
60
    {
61
        return $this->driver->_table();
62
    }
63
64
    /**
65
     * @var GrammarInterface
66
     */
67
    protected function _grammar(): GrammarInterface
68
    {
69
        return $this->driver->_grammar();
70
    }
71
72
    /**
73
     * @var QueryInterface
74
     */
75
    protected function _query(): QueryInterface
76
    {
77
        return $this->driver->_query();
78
    }
79
80
    /**
81
     * @inheritDoc
82
     */
83
    public function name()
84
    {
85
        return $this->driver->name();
86
    }
87
88
    /**
89
     * @return void
90
     */
91
    protected function beforeConnection(): void
92
    {
93
        $this->driver->beforeConnection();
94
    }
95
96
    /**
97
     * @return void
98
     */
99
    protected function configConnection(): void
100
    {
101
        $this->driver->configConnection();
102
    }
103
104
    /**
105
     * @return void
106
     */
107
    protected function connectionOpened(): void
108
    {
109
        $this->driver->connectionOpened();
110
    }
111
112
    /**
113
     * @inheritDoc
114
     */
115
    public function createConnection(array $options): ConnectionInterface|null
116
    {
117
        return $this->driver->createConnection($options);
118
    }
119
120
    /**
121
     * @param string $query
122
     *
123
     * @return void
124
     */
125
    private function callCallbacks(string $query): void
126
    {
127
        foreach ($this->callbacks as $callback) {
128
            $callback($query);
129
        }
130
    }
131
132
    /**
133
     * @inheritDoc
134
     */
135
    public function addQueryCallback(Closure $callback): void
136
    {
137
        $this->callbacks[] = $callback;
138
    }
139
140
    /**
141
     * @inheritDoc
142
     */
143
    public function multiQuery(string $query): bool
144
    {
145
        $result = $this->driver->multiQuery($query);
146
        // Call the query callbacks.
147
        $this->callCallbacks($query);
148
        return $result;
149
    }
150
151
    /**
152
     * @inheritDoc
153
     */
154
    public function result(string $query, int $field = -1): mixed
155
    {
156
        $result = $this->driver->result($query, $field);
157
        // Call the query callbacks.
158
        $this->callCallbacks($query);
159
        return $result;
160
    }
161
162
    /**
163
     * @inheritDoc
164
     */
165
    public function execute(string $query): StatementInterface|bool
166
    {
167
        $result = $this->driver->execute($query);
168
        // Call the query callbacks.
169
        $this->callCallbacks($query);
170
        return $result;
171
    }
172
173
    /**
174
     * @param array $options
175
     *
176
     * @return DriverInterface|null
177
     */
178
    public static function createDriver(array $options): ?DriverInterface
179
    {
180
        $drivers = Driver::drivers();
181
        $driver = $options['driver'];
182
        $closure = $drivers[$driver] ?? null;
183
        return !$closure ? null : $closure(jaxon()->di(), $options);
184
    }
185
}
186