|
1
|
|
|
<?php |
|
2
|
|
|
namespace Wandu\Database; |
|
3
|
|
|
|
|
4
|
|
|
use Wandu\Database\Connection\MysqlConnection; |
|
5
|
|
|
use Wandu\Database\Contracts\ConnectionInterface; |
|
6
|
|
|
use Wandu\Database\Contracts\Entity\MetadataReaderInterface; |
|
7
|
|
|
use Wandu\Database\Exception\DriverNotFoundException; |
|
8
|
|
|
use Wandu\Database\Repository\Repository; |
|
9
|
|
|
|
|
10
|
|
|
class Manager |
|
11
|
|
|
{ |
|
12
|
|
|
/** @var \Wandu\Database\Contracts\Entity\MetadataReaderInterface */ |
|
13
|
|
|
protected $reader; |
|
14
|
|
|
|
|
15
|
|
|
/** @var \Wandu\Database\Contracts\ConnectionInterface[] */ |
|
16
|
|
|
protected $connections = []; |
|
17
|
|
|
|
|
18
|
|
|
/** @var \Wandu\Database\Repository\Repository[] */ |
|
19
|
|
|
protected $repositories = []; |
|
20
|
|
|
|
|
21
|
18 |
|
public function __construct(MetadataReaderInterface $reader) |
|
22
|
|
|
{ |
|
23
|
18 |
|
$this->reader = $reader; |
|
24
|
18 |
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param array|\Wandu\Database\Configuration|\Wandu\Database\Contracts\ConnectionInterface $connection |
|
28
|
|
|
* @param string $name |
|
29
|
|
|
* @return \Wandu\Database\Contracts\ConnectionInterface |
|
30
|
|
|
*/ |
|
31
|
18 |
|
public function connect($connection, $name = 'default') |
|
32
|
|
|
{ |
|
33
|
18 |
|
if (!$connection instanceof Configuration) { |
|
34
|
18 |
|
$connection = new Configuration($connection); |
|
|
|
|
|
|
35
|
|
|
} |
|
36
|
18 |
|
if (!$connection instanceof ConnectionInterface) { |
|
37
|
18 |
|
switch ($connection->getDriver()) { |
|
38
|
18 |
|
case Configuration::DRIVER_MYSQL: |
|
39
|
17 |
|
$connection = new MysqlConnection($connection); |
|
40
|
17 |
|
break; |
|
41
|
|
|
default: |
|
42
|
1 |
|
throw new DriverNotFoundException($connection->getDriver()); |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
17 |
|
$connection->connect(); |
|
46
|
17 |
|
return $this->connections[$name] = $connection; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param string $name |
|
51
|
|
|
* @return \Wandu\Database\Contracts\ConnectionInterface |
|
52
|
|
|
*/ |
|
53
|
14 |
|
public function connection($name = 'default') |
|
54
|
|
|
{ |
|
55
|
14 |
|
return isset($this->connections[$name]) ? $this->connections[$name] : null; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param string $class |
|
60
|
|
|
* @param string $connection |
|
61
|
|
|
* @return \Wandu\Database\Repository\Repository |
|
62
|
|
|
*/ |
|
63
|
14 |
|
public function repository(string $class, string $connection = 'default'): Repository |
|
64
|
|
|
{ |
|
65
|
14 |
|
$repoName = "{$class}@{$connection}"; |
|
66
|
14 |
|
if (!isset($this->repositories[$repoName])) { |
|
67
|
14 |
|
$this->repositories[$repoName] = new Repository($this, $this->reader->getMetadataFrom($class)); |
|
68
|
|
|
} |
|
69
|
14 |
|
return $this->repositories[$repoName]; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.