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