|
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
|
3 |
|
public function __construct(AnnotatedInterface $model = null) |
|
45
|
|
|
{ |
|
46
|
3 |
|
$this->model = $model; |
|
47
|
3 |
|
if (empty($model)) |
|
48
|
|
|
{ |
|
49
|
1 |
|
$this->mn = Mangan::fly(); |
|
50
|
1 |
|
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
|
|
|
|