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 Maslosoft\Addendum\Interfaces\AnnotatedInterface; |
17
|
|
|
use Maslosoft\Mangan\Exceptions\CommandException; |
18
|
|
|
use Maslosoft\Mangan\Exceptions\CommandNotFoundException; |
19
|
|
|
use Maslosoft\Mangan\Helpers\CollectionNamer; |
20
|
|
|
use Maslosoft\Mangan\Meta\ManganMeta; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Command |
24
|
|
|
* |
25
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
26
|
|
|
*/ |
27
|
|
|
class Command |
28
|
|
|
{ |
29
|
|
|
|
30
|
|
|
use Traits\AvailableCommands; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* |
34
|
|
|
* @var AnnotatedInterface |
35
|
|
|
*/ |
36
|
|
|
private $model = null; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* |
40
|
|
|
* @var Mangan |
41
|
|
|
*/ |
42
|
|
|
private $mn = null; |
43
|
|
|
|
44
|
4 |
|
public function __construct(AnnotatedInterface $model = null) |
45
|
|
|
{ |
46
|
4 |
|
$this->model = $model; |
47
|
4 |
|
if (empty($model)) |
48
|
|
|
{ |
49
|
2 |
|
$this->mn = Mangan::fly(); |
50
|
2 |
|
return; |
51
|
|
|
} |
52
|
2 |
|
$this->mn = Mangan::fromModel($model); |
53
|
2 |
|
} |
54
|
|
|
|
55
|
3 |
|
public function call($command, $arguments = []) |
56
|
|
|
{ |
57
|
3 |
|
$arg = $this->model ? CollectionNamer::nameCollection($this->model) : true; |
58
|
3 |
|
$cmd = [$command => $arg]; |
59
|
3 |
|
if (is_array($arguments) && count($arguments)) |
60
|
|
|
{ |
61
|
1 |
|
$cmd = array_merge($cmd, $arguments); |
62
|
|
|
} |
63
|
3 |
|
$result = $this->mn->getDbInstance()->command($cmd); |
64
|
|
|
|
65
|
3 |
|
if (array_key_exists('errmsg', $result) && array_key_exists('ok', $result) && $result['ok'] == 0) |
66
|
|
|
{ |
67
|
1 |
|
if (array_key_exists('bad cmd', $result)) |
68
|
|
|
{ |
69
|
|
|
$badCmd = key($result['bad cmd']); |
70
|
|
|
if ($badCmd == $command) |
71
|
|
|
{ |
72
|
|
|
throw new CommandNotFoundException(sprintf('Command `%s` not found', $command)); |
73
|
|
|
} |
74
|
|
|
} |
75
|
1 |
|
elseif (strpos($result['errmsg'], 'no such command') !== false) |
76
|
|
|
{ |
77
|
1 |
|
throw new CommandNotFoundException(sprintf('Command `%s` not found', $command)); |
78
|
|
|
} |
79
|
|
|
throw new CommandException(sprintf('Could not execute command `%s`, mongo returned: "%s"', $command, $result['errmsg'])); |
80
|
|
|
} |
81
|
2 |
|
return $result; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function __call($name, $arguments) |
85
|
|
|
{ |
86
|
|
|
if (count($arguments)) |
87
|
|
|
{ |
88
|
|
|
return $this->call($name, $arguments[0]); |
89
|
|
|
} |
90
|
|
|
return $this->call($name); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Explicitly creates a collection or view. |
95
|
|
|
* |
96
|
|
|
* Parameter `$params` depends on MongoDB version, |
97
|
|
|
* see (official documentation)[https://docs.mongodb.com/manual/reference/command/create/] for details |
98
|
|
|
* |
99
|
|
|
* @param string $collectionName The name of the new collection |
100
|
|
|
* @param array $params |
101
|
|
|
* @return array |
102
|
|
|
*/ |
103
|
1 |
|
public function create($collectionName, $params = []) |
104
|
|
|
{ |
105
|
|
|
$cmd = [ |
106
|
1 |
|
'create' => $collectionName |
107
|
|
|
]; |
108
|
1 |
|
return $this->mn->getDbInstance()->command(array_merge($cmd, $params)); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* The `collStats` command returns a variety of storage statistics for a given collection. |
113
|
|
|
* |
114
|
|
|
* @param string $collectionName The name of the target collection. If the collection does not exist, collStats returns an error message. |
115
|
|
|
* @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. |
116
|
|
|
* @param boolean $verbose Optional. When true, collStats increases reporting for the MMAPv1 Storage Engine. Defaults to false. |
117
|
|
|
* @return array |
118
|
|
|
*/ |
119
|
1 |
|
public function collStats($collectionName, $scale = 1, $verbose = false) |
120
|
|
|
{ |
121
|
|
|
$cmd = [ |
122
|
1 |
|
'collStats' => $collectionName, |
123
|
1 |
|
'scale' => $scale, |
124
|
1 |
|
'verbose' => $verbose |
125
|
|
|
]; |
126
|
1 |
|
return $this->mn->getDbInstance()->command($cmd); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
} |
130
|
|
|
|