1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Milkmeowo\Framework\Database\Providers; |
4
|
|
|
|
5
|
|
|
use LogicException; |
6
|
|
|
use Illuminate\Database\Connection; |
7
|
|
|
use Illuminate\Support\ServiceProvider; |
8
|
|
|
|
9
|
|
|
class DatabaseServiceProvider extends ServiceProvider |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Resolve the connection via bindings. |
13
|
|
|
* |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
const CONNECTION_RESOLVER_BINDINGS = 'bindings'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Resolve the connection via the connection resolver method. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
const CONNECTION_RESOLVER_METHOD = 'connection-method'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The connection classes to use for the drivers. |
27
|
|
|
* |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected $classes = [ |
31
|
|
|
'mysql' => 'Milkmeowo\Framework\Database\Connection\MysqlConnection', |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
public function register() |
35
|
|
|
{ |
36
|
|
|
$this->registerConnections(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Register the database connection extensions. |
41
|
|
|
* |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
|
|
public function registerConnections() |
45
|
|
|
{ |
46
|
|
|
$driver = $this->connectionResolverDriver(); |
47
|
|
|
|
48
|
|
|
$method = 'registerVia'.studly_case($driver); |
49
|
|
|
|
50
|
|
|
if (method_exists($this, $method)) { |
51
|
|
|
return $this->{$method}(); |
52
|
|
|
} |
53
|
|
|
throw new LogicException(sprintf('Connection registration method [%s] does not exist.', $method)); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Determine the driver for resolving the connection. |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
public function connectionResolverDriver() |
62
|
|
|
{ |
63
|
|
|
if (method_exists(Connection::class, 'resolverFor')) { |
64
|
|
|
return self::CONNECTION_RESOLVER_METHOD; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return self::CONNECTION_RESOLVER_BINDINGS; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Register the database connection extensions through app bindings. |
72
|
|
|
* |
73
|
|
|
* @return void |
74
|
|
|
*/ |
75
|
|
|
public function registerViaBindings() |
76
|
|
|
{ |
77
|
|
|
foreach ($this->classes as $driver => $class) { |
78
|
|
|
$this->app->bind('db.connection.'.$driver, $class); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Register the database connection extensions through the `Connection` method. |
84
|
|
|
* |
85
|
|
|
* @return void |
86
|
|
|
*/ |
87
|
|
|
public function registerViaConnectionMethod() |
88
|
|
|
{ |
89
|
|
|
foreach ($this->classes as $driver => $class) { |
90
|
|
|
Connection::resolverFor($driver, function ($connection, $database, $prefix, $config) use ($class) { |
91
|
|
|
return new $class($connection, $database, $prefix, $config); |
92
|
|
|
}); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Check if the connection resolver method is of the given type. |
98
|
|
|
* |
99
|
|
|
* @param string $driver |
100
|
|
|
* |
101
|
|
|
* @return bool |
102
|
|
|
*/ |
103
|
|
|
public function isConnectionResolver($driver) |
104
|
|
|
{ |
105
|
|
|
return $this->connectionResolverDriver() == $driver; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|