-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServices.php
More file actions
133 lines (107 loc) · 3.63 KB
/
Services.php
File metadata and controls
133 lines (107 loc) · 3.63 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
/**
* This file is part of Blitz PHP framework - Database Layer.
*
* (c) 2022 Dimitri Sitchet Tomkeu <devcode.dst@gmail.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace BlitzPHP\Database\Config;
use BlitzPHP\Container\Services as BaseServices;
use BlitzPHP\Contracts\Database\BuilderInterface;
use BlitzPHP\Contracts\Database\ConnectionInterface;
use BlitzPHP\Database\Builder\BaseBuilder;
use BlitzPHP\Database\Connection\BaseConnection;
use BlitzPHP\Database\DatabaseManager;
use Dimtrovich\DbDumper\Exporter;
use Dimtrovich\DbDumper\Importer;
use InvalidArgumentException;
/**
* Services de base de données
*/
class Services extends BaseServices
{
/**
* Instance du gestionnaire de base de données
*/
protected static ?DatabaseManager $manager = null;
/**
* Récupère le gestionnaire de base de données
*/
public static function dbManager(): DatabaseManager
{
if (static::$manager === null) {
static::$manager = new DatabaseManager(static::logger(), static::event());
}
return static::$manager;
}
/**
* Récupère une connexion à la base de données
*
* @return BaseConnection
*/
public static function database(?string $group = null, bool $shared = true): ConnectionInterface
{
$connection = static::dbManager()->connect($group, $shared);
if (!$connection instanceof BaseConnection) {
throw new InvalidArgumentException('La connexion retournée n\'est pas une instance de BaseConnection');
}
return $connection;
}
/**
* Récupère un query builder
*
* @return BaseBuilder
*
* @deprecated 1.0 use static::database()->table($tablename) instead
*/
public static function builder(?string $group = null, bool $shared = true): BuilderInterface
{
$key = 'builder_' . ($group ?? 'default');
if ($shared && isset(static::$instances[$key])) {
return static::$instances[$key];
}
$builder = static::dbManager()->builder(static::database($group, $shared));
if ($shared) {
static::$instances[$key] = $builder;
}
return $builder;
}
/**
* Récupère un exportateur de base de données
*/
public static function dbExporter(?ConnectionInterface $db = null, array $config = [], bool $shared = true): Exporter
{
if ($shared) {
return static::sharedInstance('dbExporter', $db, $config);
}
$db ??= static::database();
$config = $config ?: (array) config('dump', []);
// Initialiser la connexion si nécessaire
$db->initialize();
$exporter = new Exporter($db->getDatabase(), $db->getConnection(), $config);
if ($shared) {
static::$instances[Exporter::class] = $exporter;
}
return $exporter;
}
/**
* Récupère un importateur de base de données
*/
public static function dbImporter(?ConnectionInterface $db = null, array $config = [], bool $shared = true): Importer
{
if ($shared) {
return static::sharedInstance('dbImporter', $db, $config);
}
$db ??= static::database();
$config = $config ?: (array) config('dump', []);
// Initialiser la connexion si nécessaire
$db->initialize();
$importer = new Importer($db->getDatabase(), $db->getConnection(), $config);
if ($shared) {
static::$instances[Importer::class] = $importer;
}
return $importer;
}
}