Initial Commit
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
class App_Model_Mongo_Adapter
|
||||
{
|
||||
|
||||
const MONGO_DB_ADAPTER_REG_NAME = 'MONGO_DB_ADAPTER_REG_NAME';
|
||||
const MONGO_DB_NAME = 'MONGO_DB_NAME';
|
||||
|
||||
protected $_username = null;
|
||||
protected $_password = null;
|
||||
protected $_host = '127.0.0.1';
|
||||
protected $_port = '27017';
|
||||
|
||||
protected $_connectionUri = null;
|
||||
protected $_dbname = null;
|
||||
|
||||
protected $_mongoClient;
|
||||
|
||||
public function __construct(array $connectionData = array())
|
||||
{
|
||||
//Default Mongo Uri
|
||||
$this->_connectionUri = "mongodb://$this->_host:$this->_port";
|
||||
|
||||
//Autenticazione mongoDB
|
||||
if ($connectionData['user'] && ['password']) {
|
||||
|
||||
$this->_username = $connectionData['user'];
|
||||
$this->_password = $connectionData['password'];
|
||||
|
||||
$this->_host = ($connectionData['host']) ? $connectionData['host'] : $this->_host;
|
||||
$this->_port = ($connectionData['port']) ? $connectionData['port'] : $this->_port;
|
||||
|
||||
$mongoAuth = "$this->_username:$this->_password";
|
||||
$mongoHost = "$this->_host:$this->_port";
|
||||
|
||||
//AuthMongoUri
|
||||
$this->_connectionUri = "mongodb://$mongoAuth@$mongoHost";
|
||||
}
|
||||
|
||||
if($connectionData['dbname']){
|
||||
$this->_dbname = $connectionData['dbname'];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \MongoDB\Client
|
||||
*/
|
||||
public function getMongoClient(): \MongoDB\Client
|
||||
{
|
||||
if(!$this->_mongoClient){
|
||||
$this->_mongoClient = new MongoDB\Client($this->_connectionUri);
|
||||
}
|
||||
return $this->_mongoClient;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getDbname(): string
|
||||
{
|
||||
return $this->_dbname;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $dbname
|
||||
*/
|
||||
public function setDbname(string $dbname): void
|
||||
{
|
||||
$this->_dbname = $dbname;
|
||||
Zend_Registry::set(self::MONGO_DB_ADAPTER_REG_NAME,$this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,254 @@
|
||||
<?php
|
||||
|
||||
class App_Model_Mongo_Client
|
||||
{
|
||||
|
||||
const WHERE_MONGO_REG_NAME = 'WHERE_MONGO_REG_NAME';
|
||||
|
||||
protected $_config;
|
||||
protected $_mongoClient = null;
|
||||
protected $_db = null;
|
||||
|
||||
public function __construct($connectionUrl = 'mongodb://127.0.0.1:27017')
|
||||
{
|
||||
if (Zend_Registry::isRegistered(App_Model_Mongo_Adapter::MONGO_DB_ADAPTER_REG_NAME)) {
|
||||
$mongoAdapter = Zend_Registry::get(App_Model_Mongo_Adapter::MONGO_DB_ADAPTER_REG_NAME);
|
||||
} else {
|
||||
$mongoAdapter = new App_Model_Mongo_Adapter();
|
||||
$mongoAdapter->setDbname(Zend_Registry::get(App_Model_Mongo_Adapter::MONGO_DB_NAME));
|
||||
}
|
||||
|
||||
$this->_mongoClient = $mongoAdapter->getMongoClient();
|
||||
$dbName = $mongoAdapter->getDbname();
|
||||
|
||||
$this->_db = $this->_mongoClient->selectDatabase($dbName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @return App_Model_MongoResource_Abstract
|
||||
* @throws App_Controller_Errors_WebServiceError
|
||||
*/
|
||||
public function getResource($name)
|
||||
{
|
||||
|
||||
if (!isset($this->_resources[$name])) {
|
||||
$class = implode('_', array(
|
||||
'Application',
|
||||
'Model',
|
||||
'MongoResources',
|
||||
ucfirst($name)));
|
||||
|
||||
//Controllo l'esistenza della classe
|
||||
if (!class_exists($class)) {
|
||||
throw new App_Controller_Errors_WebServiceError("Errore generazione la classe risorsa mongo '$class' potrebbe non esistere");
|
||||
}
|
||||
|
||||
$this->_resources[$name] = new $class();
|
||||
}
|
||||
|
||||
return $this->_resources[$name];
|
||||
}
|
||||
|
||||
public function test()
|
||||
{
|
||||
return $this->_mongoClient->test;
|
||||
}
|
||||
|
||||
public function setDatabase($dbName)
|
||||
{
|
||||
return $this->_db = $this->_mongoClient->selectDatabase($dbName);
|
||||
}
|
||||
|
||||
public function listDatabases()
|
||||
{
|
||||
return $this->_mongoClient->listDatabases();
|
||||
}
|
||||
|
||||
public function getDatabase()
|
||||
{
|
||||
return $this->_db;
|
||||
}
|
||||
|
||||
public function deleteDatabase()
|
||||
{
|
||||
return $this->_mongoClient->dropDatabase($this->getDatabase());
|
||||
}
|
||||
|
||||
public function getCollection($resourceName)
|
||||
{
|
||||
$collection = $this->getResource($resourceName)->getCollectionName();
|
||||
return $this->getDatabase()->$collection;
|
||||
}
|
||||
|
||||
public function emptyCollection($resourceName)
|
||||
{
|
||||
return $this->getCollection($resourceName)->deleteMany(['id' => array('$gt'=>0)]);
|
||||
}
|
||||
|
||||
// FUNZIONI PER INTERROGARE IL DB
|
||||
|
||||
public function getById($name, $id)
|
||||
{
|
||||
$optionsParams['projection'] = ['_id' => 0];
|
||||
return $this->getCollection($name)->findOne([$this->getResource($name)->getPrimaryKey() => $id], $optionsParams);
|
||||
}
|
||||
|
||||
public function insert($name, $data)
|
||||
{
|
||||
$id = uniqid();
|
||||
$this->getCollection($name)->insertOne(array_merge([$this->getResource($name)->getPrimaryKey() => $id], $data));
|
||||
return $id;
|
||||
}
|
||||
|
||||
public function update($name, $id, $data)
|
||||
{
|
||||
return $this->getCollection($name)->updateOne([$this->getResource($name)->getPrimaryKey() => $id], ['$set' => $data]);
|
||||
}
|
||||
|
||||
public function delete($name, $id)
|
||||
{
|
||||
return $this->getCollection($name)->deleteOne([$this->getResource($name)->getPrimaryKey() => $id]);
|
||||
}
|
||||
|
||||
public function getCountTotal($name)
|
||||
{
|
||||
return $this->getCollection($name)->countDocuments();
|
||||
}
|
||||
|
||||
public function getCountFilteredTotal($name, $filter)
|
||||
{
|
||||
return $this->getCollection($name)->countDocuments($filter);
|
||||
}
|
||||
|
||||
public function getList($name, $paged = null, $count = null, $where = null, $order = null, $projection = null)
|
||||
{
|
||||
$findParams = array();
|
||||
|
||||
if (null !== $paged && null !== $count && $count > 0 && $paged > 0) {
|
||||
if ($paged > 1) $findParams['skip'] = (($paged - 1) * $count);
|
||||
$findParams['limit'] = $count;
|
||||
}
|
||||
|
||||
$order = (!$order) ? $this->getResource($name)->getDefaultOrder($order) : $order;
|
||||
if ($order && is_array($order)) {
|
||||
$findParams['sort'] = $order;
|
||||
}
|
||||
|
||||
$projection = (!$projection) ? $this->getResource($name)->getDefaultProjection($projection) : $projection;
|
||||
if ($projection && is_array($projection)) {
|
||||
$findParams['projection'] = $projection;
|
||||
}
|
||||
|
||||
if ($where === null) $where = array();
|
||||
|
||||
$cursor = $this->getCollection($name)->find($where, $findParams);
|
||||
|
||||
return $cursor->toArray();
|
||||
}
|
||||
|
||||
public function getListAggregation($name, $paged = null, $count = null, $where = null, $order = null, $projection = null, $addFields = null, $extraAggregation = null)
|
||||
{
|
||||
$aggregations = array();
|
||||
|
||||
if ($where === null) $where = array();
|
||||
if (count($where) > 0) {
|
||||
array_push($aggregations, ['$match' => $where]);
|
||||
}
|
||||
|
||||
$order = $this->getResource($name)->getDefaultOrder($order);
|
||||
if ($order && is_array($order)) {
|
||||
array_push($aggregations, ['$sort' => $order]);
|
||||
}
|
||||
|
||||
if (null !== $paged && null !== $count && $count > 0 && $paged > 0) {
|
||||
if ($paged > 1)
|
||||
array_push($aggregations, ['$skip' => (($paged - 1) * $count)]);
|
||||
array_push($aggregations, ['$limit' => $count]);
|
||||
}
|
||||
|
||||
$projection = $this->getResource($name)->getDefaultProjection($projection);
|
||||
if ($projection && is_array($projection)) {
|
||||
array_push($aggregations, ['$project' => $projection]);
|
||||
}
|
||||
|
||||
//$totElements = $this->getCountTotal($name);
|
||||
$addFields = $this->getResource($name)->getDefaultAddFields();
|
||||
if ($addFields && is_array($addFields)) {
|
||||
|
||||
/*if($totElements <= 0){
|
||||
unset($addFields['Data']);
|
||||
}*/
|
||||
|
||||
array_push($aggregations, ['$addFields' => $addFields]);
|
||||
}
|
||||
|
||||
if ($extraAggregation && is_array($extraAggregation)) {
|
||||
if(count($extraAggregation) > 1){
|
||||
$aggregations = array_merge($aggregations, $extraAggregation);
|
||||
}else{
|
||||
array_push($aggregations, $extraAggregation);
|
||||
}
|
||||
}
|
||||
|
||||
// Zend_Registry::get('log')->info(print_r($aggregations,true));
|
||||
|
||||
$cursor = $this->getCollection($name)->aggregate($aggregations);
|
||||
|
||||
return $cursor->toArray();
|
||||
}
|
||||
|
||||
public function getByColumnName($name, $columnName, $value, $op = "=", $all = false)
|
||||
{
|
||||
|
||||
switch ($op) {
|
||||
case'=':
|
||||
default:
|
||||
$params = array($columnName => $value);
|
||||
break;
|
||||
case 'LIKE':
|
||||
$params = array($columnName => ['$regex' => $value, '$options' => 'i']);
|
||||
break;
|
||||
}
|
||||
|
||||
if ($all) {
|
||||
return $this->getCollection($name)->find($params)->toArray();
|
||||
} else {
|
||||
return $this->getCollection($name)->findOne($params);
|
||||
}
|
||||
}
|
||||
|
||||
public function find($name, $params = array())
|
||||
{
|
||||
return $this->getCollection($name)->find($params)->toArray();
|
||||
}
|
||||
|
||||
public function aggregate($name, $aggregate)
|
||||
{
|
||||
return $this->getCollection($name)->aggregate($aggregate);
|
||||
}
|
||||
|
||||
// CRUD FUNCTION WITH OBJECTID
|
||||
|
||||
public function getByObjectId($name, $id)
|
||||
{
|
||||
return $this->getCollection($name)->find([$this->getResource($name)->getPrimaryKey() => new MongoDB\BSON\ObjectID($id)])->toArray();
|
||||
}
|
||||
|
||||
public function insertWithObjectId($name, $data)
|
||||
{
|
||||
unset($data['_id']);
|
||||
return $this->getCollection($name)->insertOne($data);
|
||||
}
|
||||
|
||||
public function updateByObjectId($name, $id, $data)
|
||||
{
|
||||
return $this->getCollection($name)->updateOne([$this->getResource($name)->getPrimaryKey() => new MongoDB\BSON\ObjectID($id)], ['$set' => $data]);
|
||||
}
|
||||
|
||||
public function deleteByObjectId($name, $id)
|
||||
{
|
||||
return $this->getCollection($name)->deleteOne([$this->getResource($name)->getPrimaryKey() => new MongoDB\BSON\ObjectID($id)]);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @see Zend_Paginator_Adapter_Interface
|
||||
*/
|
||||
require_once 'Zend/Paginator/Adapter/Interface.php';
|
||||
|
||||
class App_Model_Mongo_PaginatorAdapter implements Zend_Paginator_Adapter_Interface
|
||||
{
|
||||
|
||||
protected $_cursor = null;
|
||||
|
||||
|
||||
public function __construct(MongoDB\Driver\Cursor $cursor)
|
||||
{
|
||||
$this->_cursor = $cursor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an cursor limited to items for a page.
|
||||
*
|
||||
* @param integer $offset Page offset
|
||||
* @param integer $itemCountPerPage Number of items per page
|
||||
* @return MongoDB\Driver\Cursor
|
||||
*/
|
||||
public function getItems($offset, $itemCountPerPage)
|
||||
{
|
||||
$cursor = $this->_cursor->skip($offset)->limit($itemCountPerPage);
|
||||
return $cursor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the total number of rows in the cursor.
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function count()
|
||||
{
|
||||
return count($this->_cursor->toArray());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user