|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This software package is licensed under AGPL or Commercial license. |
|
5
|
|
|
* |
|
6
|
|
|
* @package maslosoft/mangan |
|
7
|
|
|
* @licence AGPL or Commercial |
|
8
|
|
|
* @copyright Copyright (c) Piotr Masełkowski <[email protected]> |
|
9
|
|
|
* @copyright Copyright (c) Maslosoft |
|
10
|
|
|
* @copyright Copyright (c) Others as mentioned in code |
|
11
|
|
|
* @link https://maslosoft.com/mangan/ |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Maslosoft\Mangan; |
|
15
|
|
|
|
|
16
|
|
|
use function iterator_to_array; |
|
17
|
|
|
use Maslosoft\Addendum\Interfaces\AnnotatedInterface; |
|
18
|
|
|
use Maslosoft\Mangan\Exceptions\CommandException; |
|
19
|
|
|
use Maslosoft\Mangan\Exceptions\CommandNotFoundException; |
|
20
|
|
|
use Maslosoft\Mangan\Helpers\CollectionNamer; |
|
21
|
|
|
use Maslosoft\Mangan\Model\Command\User; |
|
22
|
|
|
use Maslosoft\Mangan\Traits\AvailableCommands; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Command |
|
26
|
|
|
* |
|
27
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
|
28
|
|
|
*/ |
|
29
|
|
|
class Command |
|
30
|
|
|
{ |
|
31
|
|
|
|
|
32
|
|
|
use AvailableCommands; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* |
|
36
|
|
|
* @var AnnotatedInterface |
|
37
|
|
|
*/ |
|
38
|
|
|
private $model = null; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* |
|
42
|
|
|
* @var Mangan |
|
43
|
|
|
*/ |
|
44
|
|
|
private $mn = null; |
|
45
|
6 |
|
|
|
46
|
|
|
private $collection = ''; |
|
47
|
6 |
|
|
|
48
|
6 |
|
public function __construct(AnnotatedInterface $model = null, Mangan $mangan = null) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->model = $model; |
|
51
|
|
|
if (!empty($mangan)) |
|
52
|
6 |
|
{ |
|
53
|
|
|
$this->mn = $mangan; |
|
54
|
4 |
|
} |
|
55
|
4 |
|
if (empty($model)) |
|
56
|
|
|
{ |
|
57
|
2 |
|
$this->mn = Mangan::fly(); |
|
58
|
2 |
|
return; |
|
59
|
|
|
} |
|
60
|
3 |
|
$this->mn = Mangan::fromModel($model); |
|
61
|
|
|
$this->collection = CollectionNamer::nameCollection($model); |
|
62
|
3 |
|
} |
|
63
|
3 |
|
|
|
64
|
3 |
|
public function call($command, $arguments = []) |
|
65
|
|
|
{ |
|
66
|
1 |
|
$arg = $this->model ? CollectionNamer::nameCollection($this->model) : true; |
|
67
|
|
|
$cmd = [$command => $arg]; |
|
68
|
3 |
|
if (is_array($arguments) && count($arguments)) |
|
69
|
|
|
{ |
|
70
|
3 |
|
$cmd = array_merge($cmd, $arguments); |
|
71
|
|
|
} |
|
72
|
1 |
|
$result = $this->mn->getDbInstance()->command($cmd); |
|
73
|
|
|
|
|
74
|
|
|
if (array_key_exists('errmsg', $result) && array_key_exists('ok', $result) && $result['ok'] == 0) |
|
75
|
|
|
{ |
|
76
|
|
|
if (array_key_exists('bad cmd', $result)) |
|
77
|
|
|
{ |
|
78
|
|
|
$badCmd = key($result['bad cmd']); |
|
79
|
|
|
if ($badCmd == $command) |
|
80
|
1 |
|
{ |
|
81
|
|
|
throw new CommandNotFoundException(sprintf('Command `%s` not found', $command)); |
|
82
|
1 |
|
} |
|
83
|
|
|
} |
|
84
|
|
|
elseif (strpos($result['errmsg'], 'no such command') !== false) |
|
85
|
|
|
{ |
|
86
|
2 |
|
throw new CommandNotFoundException(sprintf('Command `%s` not found', $command)); |
|
87
|
|
|
} |
|
88
|
|
|
throw new CommandException(sprintf('Could not execute command `%s`, mongo returned: "%s"', $command, $result['errmsg'])); |
|
89
|
|
|
} |
|
90
|
|
|
return $result; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function __call($name, $arguments) |
|
94
|
|
|
{ |
|
95
|
|
|
if (count($arguments)) |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->call($name, $arguments[0]); |
|
98
|
|
|
} |
|
99
|
|
|
return $this->call($name); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Explicitly creates a collection or view. |
|
104
|
|
|
* |
|
105
|
|
|
* Parameter `$params` depends on MongoDB version, |
|
106
|
|
|
* see (official documentation)[https://docs.mongodb.com/manual/reference/command/create/] for details |
|
107
|
|
|
* |
|
108
|
1 |
|
* @param string $collectionName The name of the new collection |
|
109
|
|
|
* @param array $params |
|
110
|
|
|
* @return array |
|
111
|
1 |
|
*/ |
|
112
|
|
|
public function create($collectionName, $params = []) |
|
113
|
1 |
|
{ |
|
114
|
|
|
$cmd = [ |
|
115
|
|
|
'create' => $collectionName |
|
116
|
2 |
|
]; |
|
117
|
|
|
return $this->mn->getDbInstance()->command(array_merge($cmd, $params)); |
|
118
|
|
|
} |
|
119
|
2 |
|
|
|
120
|
2 |
|
public function createUser(User $user, $writeConcerns = []) |
|
|
|
|
|
|
121
|
|
|
{ |
|
122
|
2 |
|
$cmd = [ |
|
123
|
|
|
'createUser' => $user->user, |
|
124
|
|
|
'pwd' => $user->pwd, |
|
125
|
|
|
]; |
|
126
|
|
|
if (!empty($user->customData)) |
|
127
|
2 |
|
{ |
|
128
|
2 |
|
assert(is_object($user->customData)); |
|
129
|
|
|
$cmd['customData'] = $user->customData; |
|
130
|
|
|
} |
|
131
|
2 |
|
$cmd = array_merge($cmd, $user->toArray(['user', 'customData'])); |
|
132
|
|
|
return $this->mn->getDbInstance()->command($cmd); |
|
133
|
2 |
|
} |
|
134
|
|
|
|
|
135
|
2 |
|
public function dropUser($username, $writeConcerns = []) |
|
136
|
|
|
{ |
|
137
|
|
|
if ($username instanceof User) |
|
138
|
2 |
|
{ |
|
139
|
|
|
$username = $username->user; |
|
140
|
2 |
|
} |
|
141
|
|
|
$cmd = [ |
|
142
|
|
|
'dropUser' => $username |
|
143
|
|
|
]; |
|
144
|
|
|
return $this->mn->getDbInstance()->command(array_merge($cmd, $writeConcerns)); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
public function createIndex($keys, $options = []) |
|
148
|
|
|
{ |
|
149
|
|
|
// Ensure array |
|
150
|
|
|
if(empty($options)) |
|
151
|
1 |
|
{ |
|
152
|
|
|
$options = []; |
|
153
|
|
|
} |
|
154
|
1 |
|
return $this->mn->getDbInstance()->selectCollection($this->collection)->createIndex($keys, $options); |
|
155
|
1 |
|
} |
|
156
|
1 |
|
|
|
157
|
|
|
public function getIndexes() |
|
158
|
1 |
|
{ |
|
159
|
|
|
$cmd = [ |
|
160
|
|
|
'getIndexes' => true |
|
161
|
|
|
]; |
|
162
|
|
|
return $this->mn->getDbInstance()->command($cmd); |
|
163
|
|
|
return $this->mn->getDbInstance()->selectCollection($this->collection)->getIndexInfo(); |
|
|
|
|
|
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* The `collStats` command returns a variety of storage statistics for a given collection. |
|
168
|
|
|
* |
|
169
|
|
|
* @param string $collectionName The name of the target collection. If the collection does not exist, collStats returns an error message. |
|
170
|
|
|
* @param int $scale Optional. The scale used in the output to display the sizes of items. By default, output displays sizes in bytes. To display kilobytes rather than bytes, specify a scale value of 1024. The scale factor rounds values to whole numbers. |
|
171
|
|
|
* @param boolean $verbose Optional. When true, collStats increases reporting for the MMAPv1 Storage Engine. Defaults to false. |
|
172
|
|
|
* @return array |
|
173
|
|
|
*/ |
|
174
|
|
|
public function collStats($collectionName, $scale = 1, $verbose = false) |
|
175
|
|
|
{ |
|
176
|
|
|
$cmd = [ |
|
177
|
|
|
'collStats' => $collectionName, |
|
178
|
|
|
'scale' => $scale, |
|
179
|
|
|
'verbose' => $verbose |
|
180
|
|
|
]; |
|
181
|
|
|
return $this->mn->getDbInstance()->command($cmd); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
} |
|
185
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.