|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* components |
|
4
|
|
|
* |
|
5
|
|
|
* @author Wolfy-J |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace Spiral\ODM; |
|
8
|
|
|
|
|
9
|
|
|
use MongoDB\Driver\Manager; |
|
10
|
|
|
use Spiral\Core\Container\InjectorInterface; |
|
11
|
|
|
use Spiral\Core\Container\SingletonInterface; |
|
12
|
|
|
use Spiral\Core\FactoryInterface; |
|
13
|
|
|
use Spiral\ODM\Configs\MongoConfig; |
|
14
|
|
|
use Spiral\ODM\Entities\MongoDatabase; |
|
15
|
|
|
use Spiral\ODM\Exceptions\ODMException; |
|
16
|
|
|
|
|
17
|
|
|
class MongoManager implements InjectorInterface, SingletonInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var MongoDatabase[] |
|
21
|
|
|
*/ |
|
22
|
|
|
private $databases = []; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var MongoConfig |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $config; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var FactoryInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $factory; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param MongoConfig $config |
|
36
|
|
|
* @param FactoryInterface $factory |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct(MongoConfig $config, FactoryInterface $factory) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->config = $config; |
|
41
|
|
|
$this->factory = $factory; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Add new database in a database pull, database name will be automatically selected from given |
|
46
|
|
|
* instance. |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $name Internal database name. |
|
49
|
|
|
* @param MongoDatabase $database |
|
50
|
|
|
* |
|
51
|
|
|
* @return self|$this |
|
52
|
|
|
* |
|
53
|
|
|
* @throws ODMException |
|
54
|
|
|
*/ |
|
55
|
|
|
public function addDatabase(string $name, MongoDatabase $database): MongoManager |
|
56
|
|
|
{ |
|
57
|
|
|
if (isset($this->databases[$name])) { |
|
58
|
|
|
throw new ODMException("Database '{$name}' already exists"); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$this->databases[$name] = $database; |
|
62
|
|
|
|
|
63
|
|
|
return $this; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Register new mongo database using given name and connection options (compatible with MongoDB |
|
68
|
|
|
* class). |
|
69
|
|
|
* |
|
70
|
|
|
* @param string $name Internal database name (for injections and etc). |
|
71
|
|
|
* @param string $server Server uri. |
|
72
|
|
|
* @param string $database Database name. |
|
73
|
|
|
* @param array $driverOptions |
|
74
|
|
|
* @param array $options Database options. |
|
75
|
|
|
* |
|
76
|
|
|
* @return MongoDatabase |
|
77
|
|
|
*/ |
|
78
|
|
|
public function createDatabase( |
|
79
|
|
|
string $name, |
|
80
|
|
|
string $server, |
|
81
|
|
|
string $database, |
|
82
|
|
|
array $driverOptions = [], |
|
83
|
|
|
array $options = [] |
|
84
|
|
|
): MongoDatabase { |
|
85
|
|
|
/* |
|
86
|
|
|
* Database will be automatically connected here. |
|
87
|
|
|
*/ |
|
88
|
|
|
$instance = $this->factory->make(MongoDatabase::class, [ |
|
89
|
|
|
'databaseName' => $database, |
|
90
|
|
|
'manager' => new Manager($server, $options, $driverOptions), |
|
91
|
|
|
'options' => $options |
|
92
|
|
|
]); |
|
93
|
|
|
|
|
94
|
|
|
$this->addDatabase($name, $instance); |
|
95
|
|
|
|
|
96
|
|
|
return $instance; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Create specified or select default instance of MongoDatabase. |
|
101
|
|
|
* |
|
102
|
|
|
* @param string $database Database name (internal). |
|
103
|
|
|
* |
|
104
|
|
|
* @return MongoDatabase |
|
105
|
|
|
* |
|
106
|
|
|
* @throws ODMException |
|
107
|
|
|
*/ |
|
108
|
|
|
public function database(string $database = null): MongoDatabase |
|
109
|
|
|
{ |
|
110
|
|
|
if (empty($database)) { |
|
111
|
|
|
$database = $this->config->defaultDatabase(); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
//Spiral support ability to link multiple virtual databases together using aliases |
|
115
|
|
|
$database = $this->config->resolveAlias($database); |
|
116
|
|
|
|
|
117
|
|
|
if (isset($this->databases[$database])) { |
|
118
|
|
|
return $this->databases[$database]; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
if (!$this->config->hasDatabase($database)) { |
|
122
|
|
|
throw new ODMException( |
|
123
|
|
|
"Unable to initiate MongoDatabase, no presets for '{$database}' found" |
|
124
|
|
|
); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
$options = $this->config->databaseOptions($database); |
|
128
|
|
|
|
|
129
|
|
|
//Initiating database instance |
|
130
|
|
|
return $this->createDatabase( |
|
131
|
|
|
$database, |
|
132
|
|
|
$options['server'], |
|
133
|
|
|
$options['database'], |
|
134
|
|
|
$options['driverOptions'] ?? [], |
|
135
|
|
|
$options['options'] ?? [] |
|
136
|
|
|
); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Get every know database. |
|
141
|
|
|
* |
|
142
|
|
|
* @return MongoDatabase[] |
|
143
|
|
|
*/ |
|
144
|
|
View Code Duplication |
public function getDatabases(): array |
|
|
|
|
|
|
145
|
|
|
{ |
|
146
|
|
|
$result = []; |
|
147
|
|
|
|
|
148
|
|
|
//Include manually added databases |
|
149
|
|
|
foreach ($this->config->databaseNames() as $name) { |
|
150
|
|
|
$result[] = $this->database($name); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
return $result; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Automatic injection of MongoDatabase. |
|
158
|
|
|
* |
|
159
|
|
|
* {@inheritdoc} |
|
160
|
|
|
* |
|
161
|
|
|
* @return MongoDatabase |
|
162
|
|
|
*/ |
|
163
|
|
|
public function createInjection(\ReflectionClass $class, string $context = null) |
|
164
|
|
|
{ |
|
165
|
|
|
return $this->database($context); |
|
166
|
|
|
} |
|
167
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.