1
|
|
|
<?php |
2
|
|
|
namespace Wandu\Database; |
3
|
|
|
|
4
|
|
|
use Wandu\Caster\Caster; |
5
|
|
|
use Wandu\Caster\CastManagerInterface; |
6
|
|
|
use Wandu\Database\Connection\MysqlConnection; |
7
|
|
|
use Wandu\Database\Contracts\ConnectionInterface; |
8
|
|
|
use Wandu\Database\Contracts\Entity\MetadataReaderInterface; |
9
|
|
|
use Wandu\Database\Exception\DriverNotFoundException; |
10
|
|
|
use Wandu\Database\Repository\Repository; |
11
|
|
|
use Wandu\Event\DispatcherInterface; |
12
|
|
|
|
13
|
|
|
class Manager |
14
|
|
|
{ |
15
|
|
|
/** @var \Wandu\Database\Contracts\Entity\MetadataReaderInterface */ |
16
|
|
|
protected $reader; |
17
|
|
|
|
18
|
|
|
/** @var \Wandu\Caster\Caster */ |
19
|
|
|
protected $caster; |
20
|
|
|
|
21
|
|
|
/** @var \Wandu\Event\DispatcherInterface */ |
22
|
|
|
protected $dispatcher; |
23
|
|
|
|
24
|
|
|
/** @var \Wandu\Database\Contracts\ConnectionInterface[] */ |
25
|
|
|
protected $connections = []; |
26
|
|
|
|
27
|
|
|
/** @var \Wandu\Database\Repository\Repository[] */ |
28
|
|
|
protected $repositories = []; |
29
|
|
|
|
30
|
20 |
|
public function __construct( |
31
|
|
|
MetadataReaderInterface $reader, |
32
|
|
|
CastManagerInterface $caster = null |
33
|
|
|
) { |
34
|
20 |
|
$this->reader = $reader; |
35
|
20 |
|
$this->caster = $caster ?: new Caster(); |
|
|
|
|
36
|
20 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param \Wandu\Event\DispatcherInterface $dispatcher |
40
|
|
|
*/ |
41
|
18 |
|
public function setEventDispatcher(DispatcherInterface $dispatcher) |
42
|
|
|
{ |
43
|
18 |
|
$this->dispatcher = $dispatcher; |
44
|
18 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param array|\Wandu\Database\Configuration|\Wandu\Database\Contracts\ConnectionInterface $connection |
48
|
|
|
* @param string $name |
49
|
|
|
* @return \Wandu\Database\Contracts\ConnectionInterface |
50
|
|
|
*/ |
51
|
20 |
|
public function connect($connection, $name = 'default') |
52
|
|
|
{ |
53
|
20 |
|
if (!$connection instanceof Configuration) { |
54
|
20 |
|
$connection = new Configuration($connection); |
|
|
|
|
55
|
|
|
} |
56
|
20 |
|
if (!$connection instanceof ConnectionInterface) { |
57
|
20 |
|
switch ($connection->getDriver()) { |
58
|
20 |
|
case Configuration::DRIVER_MYSQL: |
59
|
19 |
|
$connection = new MysqlConnection($connection, $this->dispatcher); |
60
|
19 |
|
break; |
61
|
|
|
default: |
62
|
1 |
|
throw new DriverNotFoundException($connection->getDriver()); |
63
|
|
|
} |
64
|
|
|
} |
65
|
19 |
|
$connection->connect(); |
66
|
19 |
|
return $this->connections[$name] = $connection; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $name |
71
|
|
|
* @return \Wandu\Database\Contracts\ConnectionInterface |
72
|
|
|
*/ |
73
|
16 |
|
public function connection($name = 'default') |
74
|
|
|
{ |
75
|
16 |
|
return isset($this->connections[$name]) ? $this->connections[$name] : null; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $class |
80
|
|
|
* @param string $connection |
81
|
|
|
* @return \Wandu\Database\Repository\Repository |
82
|
|
|
*/ |
83
|
16 |
|
public function repository(string $class, string $connection = 'default'): Repository |
84
|
|
|
{ |
85
|
16 |
|
$repoName = "{$class}@{$connection}"; |
86
|
16 |
|
if (!isset($this->repositories[$repoName])) { |
87
|
16 |
|
$this->repositories[$repoName] = new Repository($this, $this->reader->getMetadataFrom($class), $this->caster); |
88
|
|
|
} |
89
|
16 |
|
return $this->repositories[$repoName]; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.