1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Recca0120\Repository; |
4
|
|
|
|
5
|
|
|
use Illuminate\Container\Container; |
6
|
|
|
use Illuminate\Database\ConnectionResolverInterface; |
7
|
|
|
use Illuminate\Database\Connectors\ConnectionFactory; |
8
|
|
|
|
9
|
|
|
class SqliteConnectionResolver implements ConnectionResolverInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* All of the registered connections. |
13
|
|
|
* |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
protected $connections = []; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The default connection name. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $default = 'default'; |
24
|
|
|
/** |
25
|
|
|
* The current globally available container (if any). |
26
|
|
|
* |
27
|
|
|
* @var static |
28
|
|
|
*/ |
29
|
|
|
private static $instance; |
30
|
|
|
|
31
|
1 |
|
public function __construct(ConnectionFactory $factory = null) |
|
|
|
|
32
|
|
|
{ |
33
|
1 |
|
$this->factory = $connectionFactory = new ConnectionFactory( |
|
|
|
|
34
|
1 |
|
Container::getInstance() ?: new Container |
35
|
|
|
); |
36
|
1 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Get a database connection instance. |
40
|
|
|
* |
41
|
|
|
* @param string $name |
42
|
|
|
* @return \Illuminate\Database\ConnectionInterface |
43
|
|
|
*/ |
44
|
31 |
|
public function connection($name = null) |
45
|
|
|
{ |
46
|
31 |
|
if (is_null($name)) { |
47
|
31 |
|
$name = $this->getDefaultConnection(); |
48
|
|
|
} |
49
|
|
|
|
50
|
31 |
|
if (isset($this->connections[$name]) === false) { |
51
|
1 |
|
$this->connections[$name] = $this->factory->make([ |
52
|
1 |
|
'driver' => 'sqlite', |
53
|
|
|
'database' => ':memory:', |
54
|
1 |
|
], $name); |
55
|
|
|
} |
56
|
|
|
|
57
|
31 |
|
return $this->connections[$name]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get the default connection name. |
62
|
|
|
* |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
31 |
|
public function getDefaultConnection() |
66
|
|
|
{ |
67
|
31 |
|
return $this->default; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Set the default connection name. |
72
|
|
|
* |
73
|
|
|
* @param string $name |
74
|
|
|
* @return void |
75
|
|
|
*/ |
76
|
|
|
public function setDefaultConnection($name) |
77
|
|
|
{ |
78
|
|
|
$this->default = $name; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Set the globally available instance of the SqliteConnectionResolver. |
83
|
|
|
* |
84
|
|
|
* @return static |
85
|
|
|
*/ |
86
|
31 |
|
public static function getInstance(ConnectionFactory $factory = null) |
87
|
|
|
{ |
88
|
31 |
|
if (is_null(static::$instance)) { |
|
|
|
|
89
|
1 |
|
static::$instance = new static($factory); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
31 |
|
return static::$instance; |
|
|
|
|
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.