This repository has been archived by the owner on Mar 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabaseManager.php
69 lines (60 loc) · 1.92 KB
/
DatabaseManager.php
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
<?php
namespace Wandu\Database;
class DatabaseManager
{
/** @var \Wandu\Database\Connector[] */
protected $connectors = [];
/** @var \Wandu\Database\Contracts\Connection[] */
protected $connections = [];
/** @var \Wandu\Database\Repository[] */
protected $repositories = [];
public function __construct(Configuration $config)
{
$this->config = $config;
}
/**
* @param array|\Wandu\Database\Connector $connector
* @param string $name
* @return \Wandu\Database\Contracts\Connection
*/
public function connect($connector, $name = 'default')
{
if (is_array($connector)) {
$connector = new Connector($connector);
}
$this->connectors[$name] = $connector;
$connection = $connector->connect();
if ($emitter = $this->config->getEmitter()) {
$connection->setEventEmitter($emitter);
}
return $this->connections[$name] = $connection;
}
/**
* @param string $name
* @return \Wandu\Database\Contracts\Connection
*/
public function connection($name = 'default')
{
return isset($this->connections[$name]) ? $this->connections[$name] : null;
}
/**
* @param string $class
* @return \Wandu\Database\Repository
*/
public function repository(string $class): Repository
{
if (!isset($this->repositories[$class])) {
$meta = $this->config->getMetadataReader()->getMetadata($class);
$connection = $meta->getConnection();
$prefix = $this->connectors[$connection]->getPrefix();
$this->repositories[$class] = new Repository(
$this,
$this->connections[$connection],
new QueryBuilder($prefix . $meta->getTable()),
$meta,
$this->config
);
}
return $this->repositories[$class];
}
}