Issues (61)

src/Driver/DbFacade.php (10 issues)

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