-
Notifications
You must be signed in to change notification settings - Fork 613
Expand file tree
/
Copy pathDbUtils.php
More file actions
79 lines (65 loc) · 1.8 KB
/
Copy pathDbUtils.php
File metadata and controls
79 lines (65 loc) · 1.8 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
declare(strict_types=1);
/**
* This file is part of the Phalcon Developer Tools.
*
* (c) Phalcon Team <team@phalcon.io>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Phalcon\DevTools\Utils;
use Phalcon\Config\Config;
use Phalcon\Di\Injectable;
/**
* @property Config $config
*/
class DbUtils extends Injectable
{
/**
* List database tables
*
* @param bool $all
* @param string $connection
* @return array
*/
public function listTables(bool $all = false, $connection = 'db'): array
{
$tables = $all ? ['@' => 'all'] : [];
if ($this->getDI()->has($connection)) {
$connection = $this->getDI()->getShared($connection);
$dbTables = $connection->listTables();
foreach ($dbTables as $dbTable) {
$tables[$dbTable] = $dbTable;
}
}
return $tables;
}
/**
* Resolves the DB Schema
*
* @return null|string
*/
public function resolveDbSchema(): ?string
{
if (!$this->config->offsetExists('database')) {
return null;
}
$config = $this->config->get('database');
if ($config->offsetExists('schema')) {
return $config->get('schema');
}
if ('Postgresql' === $config->get('adapter')) {
return 'public';
}
if ('Sqlite' === $config->get('adapter')) {
// SQLite only supports the current database, unless one is
// attached. This is not the case, so don't return a schema.
return null;
}
if ($config->offsetExists('dbname')) {
return $config->get('dbname');
}
return null;
}
}