1 | <?php |
||
2 | |||
3 | namespace Lagdo\DbAdmin\Db; |
||
4 | |||
5 | use Jaxon\Di\Container; |
||
6 | use Lagdo\DbAdmin\Admin\Admin; |
||
7 | use Lagdo\DbAdmin\Db\Facades\AbstractFacade; |
||
8 | use Lagdo\DbAdmin\Driver\Utils\Utils; |
||
9 | use Lagdo\DbAdmin\Driver\DriverInterface; |
||
10 | use Lagdo\DbAdmin\Package; |
||
11 | |||
12 | use function array_merge; |
||
13 | |||
14 | /** |
||
15 | * Facade to calls to the database functions |
||
16 | */ |
||
17 | class DbFacade extends AbstractFacade |
||
18 | { |
||
19 | use Traits\ServerTrait; |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
20 | use Traits\UserTrait; |
||
0 ignored issues
–
show
|
|||
21 | use Traits\DatabaseTrait; |
||
0 ignored issues
–
show
|
|||
22 | use Traits\TableTrait; |
||
0 ignored issues
–
show
|
|||
23 | use Traits\SelectTrait; |
||
0 ignored issues
–
show
|
|||
24 | use Traits\QueryTrait; |
||
0 ignored issues
–
show
|
|||
25 | use Traits\ViewTrait; |
||
0 ignored issues
–
show
|
|||
26 | use Traits\CommandTrait; |
||
0 ignored issues
–
show
|
|||
27 | use Traits\ExportTrait; |
||
0 ignored issues
–
show
|
|||
28 | use Traits\ImportTrait; |
||
0 ignored issues
–
show
|
|||
29 | |||
30 | /** |
||
31 | * The breadcrumbs items |
||
32 | * |
||
33 | * @var array |
||
34 | */ |
||
35 | protected $breadcrumbs = []; |
||
36 | |||
37 | /** |
||
38 | * @var Container |
||
39 | */ |
||
40 | protected $di; |
||
41 | |||
42 | /** |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $dbServer; |
||
46 | |||
47 | /** |
||
48 | * @var string |
||
49 | */ |
||
50 | protected $dbName; |
||
51 | |||
52 | /** |
||
53 | * @var string |
||
54 | */ |
||
55 | protected $dbSchema; |
||
56 | |||
57 | /** |
||
58 | * The constructor |
||
59 | * |
||
60 | * @param Container $di |
||
61 | * @param Utils $utils |
||
62 | * @param Package $package |
||
63 | */ |
||
64 | public function __construct(Container $di, Utils $utils, Package $package) |
||
65 | { |
||
66 | $this->di = $di; |
||
67 | $this->utils = $utils; |
||
68 | $this->package = $package; |
||
69 | // Make the translator available into views |
||
70 | $this->package->view()->share('trans', $this->utils->trans); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Get the breadcrumbs items |
||
75 | * |
||
76 | * @return array |
||
77 | */ |
||
78 | public function getBreadcrumbs(): array |
||
79 | { |
||
80 | return array_merge([$this->package->getServerName($this->dbServer)], $this->breadcrumbs); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Clear the breadcrumbs |
||
85 | * |
||
86 | * @return self |
||
87 | */ |
||
88 | protected function bccl(): self |
||
89 | { |
||
90 | $this->breadcrumbs = []; |
||
91 | return $this; |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Add the selected DB name to the breadcrumbs |
||
96 | * |
||
97 | * @return self |
||
98 | */ |
||
99 | protected function bcdb(): self |
||
100 | { |
||
101 | $this->breadcrumbs = !$this->dbName ? [] : [$this->dbName]; |
||
102 | return $this; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Add an item to the breadcrumbs |
||
107 | * |
||
108 | * @param string $label |
||
109 | * |
||
110 | * @return self |
||
111 | */ |
||
112 | protected function breadcrumb(string $label): self |
||
113 | { |
||
114 | $this->breadcrumbs[] = $label; |
||
115 | return $this; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @return Container |
||
120 | */ |
||
121 | public function di(): Container |
||
122 | { |
||
123 | return $this->di; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Set the current database |
||
128 | * |
||
129 | * @param string $server The selected server |
||
130 | * @param string $database The database name |
||
131 | * @param string $schema The database schema |
||
132 | * |
||
133 | * @return void |
||
134 | */ |
||
135 | public function selectDatabase(string $server, string $database = '', string $schema = '') |
||
136 | { |
||
137 | $this->dbServer = $server; |
||
138 | $this->dbName = $database; |
||
139 | $this->dbSchema = $schema; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * Set the current database name |
||
144 | * |
||
145 | * @param string $database The database name |
||
146 | * |
||
147 | * @return void |
||
148 | */ |
||
149 | public function setCurrentDbName(string $database) |
||
150 | { |
||
151 | $this->dbName = $database; |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * Get the current server |
||
156 | * |
||
157 | * @return string |
||
158 | */ |
||
159 | public function getCurrentServer(): string |
||
160 | { |
||
161 | return $this->dbServer; |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * Connect to a database server |
||
166 | * |
||
167 | * @param string $server The selected server |
||
168 | * @param string $database The database name |
||
169 | * @param string $schema The database schema |
||
170 | * |
||
171 | * @return void |
||
172 | */ |
||
173 | public function connect(string $server, string $database = '', string $schema = '') |
||
174 | { |
||
175 | // Prevent multiple calls. |
||
176 | if (!$this->driver) { |
||
177 | // Save the selected server options in the di container. |
||
178 | $this->di->val('dbadmin_config_driver', $this->package->getServerDriver($server)); |
||
179 | $this->di->val('dbadmin_config_options', $this->package->getServerOptions($server)); |
||
180 | $this->driver = $this->di->get(DriverInterface::class); |
||
181 | $this->admin = $this->di->get(Admin::class); |
||
182 | } |
||
183 | // Open the selected database |
||
184 | $this->driver->open($database, $schema); |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * @return void |
||
189 | */ |
||
190 | public function connectToServer() |
||
191 | { |
||
192 | $this->connect($this->dbServer); |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * @return void |
||
197 | */ |
||
198 | public function connectToDatabase() |
||
199 | { |
||
200 | $this->connect($this->dbServer, $this->dbName); |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * @return void |
||
205 | */ |
||
206 | public function connectToSchema() |
||
207 | { |
||
208 | $this->connect($this->dbServer, $this->dbName, $this->dbSchema); |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * @return array |
||
213 | */ |
||
214 | public function getServerOptions(): array |
||
215 | { |
||
216 | return $this->package->getServerOptions($this->dbServer); |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * Get the remembered queries |
||
221 | * |
||
222 | * @return array |
||
223 | */ |
||
224 | public function queries(): array |
||
225 | { |
||
226 | return $this->driver->queries(); |
||
227 | } |
||
228 | } |
||
229 |