| 1 | <?php |
||
| 2 | |||
| 3 | namespace Lagdo\DbAdmin\Db; |
||
| 4 | |||
| 5 | use Jaxon\App\View\ViewRenderer; |
||
| 6 | use Jaxon\Di\Container; |
||
| 7 | use Lagdo\DbAdmin\Admin\Admin; |
||
| 8 | use Lagdo\DbAdmin\Db\Facades\AbstractFacade; |
||
| 9 | use Lagdo\DbAdmin\Driver\Utils\Utils; |
||
| 10 | |||
| 11 | /** |
||
| 12 | * Facade to calls to the database functions |
||
| 13 | */ |
||
| 14 | class DbFacade extends AbstractFacade |
||
| 15 | { |
||
| 16 | use Traits\ServerTrait; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 17 | use Traits\UserTrait; |
||
|
0 ignored issues
–
show
|
|||
| 18 | use Traits\DatabaseTrait; |
||
|
0 ignored issues
–
show
|
|||
| 19 | use Traits\TableTrait; |
||
|
0 ignored issues
–
show
|
|||
| 20 | use Traits\SelectTrait; |
||
|
0 ignored issues
–
show
|
|||
| 21 | use Traits\QueryTrait; |
||
|
0 ignored issues
–
show
|
|||
| 22 | use Traits\ViewTrait; |
||
|
0 ignored issues
–
show
|
|||
| 23 | use Traits\CommandTrait; |
||
|
0 ignored issues
–
show
|
|||
| 24 | use Traits\ExportTrait; |
||
|
0 ignored issues
–
show
|
|||
| 25 | use Traits\ImportTrait; |
||
|
0 ignored issues
–
show
|
|||
| 26 | |||
| 27 | /** |
||
| 28 | * The breadcrumbs items |
||
| 29 | * |
||
| 30 | * @var Breadcrumbs |
||
| 31 | */ |
||
| 32 | protected $breadcrumbs; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | protected $dbServer; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | protected $dbName; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | protected $dbSchema; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * The constructor |
||
| 51 | * |
||
| 52 | * @param Container $di |
||
| 53 | * @param Utils $utils |
||
| 54 | * @param ViewRenderer $viewRenderer |
||
| 55 | */ |
||
| 56 | public function __construct(protected Container $di, protected Utils $utils, |
||
| 57 | protected ViewRenderer $viewRenderer) |
||
| 58 | { |
||
| 59 | // Make the translator available into views |
||
| 60 | $viewRenderer->share('trans', $utils->trans); |
||
| 61 | $this->breadcrumbs = new Breadcrumbs(); |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @return string |
||
| 66 | */ |
||
| 67 | public function getServerName(): string |
||
| 68 | { |
||
| 69 | return $this->dbServer; |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Get the breadcrumbs object |
||
| 74 | * |
||
| 75 | * @param bool $withDb |
||
| 76 | * |
||
| 77 | * @return Breadcrumbs |
||
| 78 | */ |
||
| 79 | public function breadcrumbs(bool $withDb = false): Breadcrumbs |
||
| 80 | { |
||
| 81 | if ($withDb) { |
||
| 82 | $this->breadcrumbs->clear(); |
||
| 83 | if ($this->dbName) { |
||
| 84 | $this->breadcrumbs->item("<i><b>$this->dbName</b></i>"); |
||
| 85 | } |
||
| 86 | } |
||
| 87 | return $this->breadcrumbs; |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @return Container |
||
| 92 | */ |
||
| 93 | public function di(): Container |
||
| 94 | { |
||
| 95 | return $this->di; |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Set the current database |
||
| 100 | * |
||
| 101 | * @param string $server The selected server |
||
| 102 | * @param string $database The database name |
||
| 103 | * @param string $schema The database schema |
||
| 104 | * |
||
| 105 | * @return void |
||
| 106 | */ |
||
| 107 | public function selectDatabase(string $server, string $database = '', string $schema = '') |
||
| 108 | { |
||
| 109 | $this->dbServer = $server; |
||
| 110 | $this->dbName = $database; |
||
| 111 | $this->dbSchema = $schema; |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Set the current database name |
||
| 116 | * |
||
| 117 | * @param string $database The database name |
||
| 118 | * |
||
| 119 | * @return void |
||
| 120 | */ |
||
| 121 | public function setCurrentDbName(string $database) |
||
| 122 | { |
||
| 123 | $this->dbName = $database; |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Connect to a database server |
||
| 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 | private function connect(string $server, string $database = '', string $schema = '') |
||
| 136 | { |
||
| 137 | // Prevent multiple calls. |
||
| 138 | if (!$this->driver) { |
||
| 139 | // Save the selected server in the di container. |
||
| 140 | $this->di->val('dbadmin_config_server', $server); |
||
| 141 | // The DI is now able to return the corresponding driver. |
||
| 142 | $this->driver = $this->di->get(AppDriver::class); |
||
| 143 | $this->admin = $this->di->get(Admin::class); |
||
| 144 | } |
||
| 145 | // Open the selected database |
||
| 146 | $this->driver->open($database, $schema); |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @return void |
||
| 151 | */ |
||
| 152 | public function connectToServer() |
||
| 153 | { |
||
| 154 | $this->connect($this->dbServer); |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @return void |
||
| 159 | */ |
||
| 160 | public function connectToDatabase() |
||
| 161 | { |
||
| 162 | $this->connect($this->dbServer, $this->dbName); |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @return void |
||
| 167 | */ |
||
| 168 | public function connectToSchema() |
||
| 169 | { |
||
| 170 | $this->connect($this->dbServer, $this->dbName, $this->dbSchema); |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @param array $options |
||
| 175 | * |
||
| 176 | * @return array |
||
| 177 | */ |
||
| 178 | public function getDatabaseOptions(array $options): array |
||
| 179 | { |
||
| 180 | if ($this->dbName !== '') { |
||
| 181 | $options['database'] = $this->dbName; |
||
| 182 | } |
||
| 183 | if ($this->dbSchema !== '') { |
||
| 184 | $options['schema'] = $this->dbSchema; |
||
| 185 | } |
||
| 186 | return $options; |
||
| 187 | } |
||
| 188 | } |
||
| 189 |