Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

module is added mongodb #69

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/Cache.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ First you'll need to include the cache module and specify which caching engine y
The available methods are `get`, `set` and `delete`.

get($name);
set($name, $value[, $ttl]);
set($name, $value, $ttl);
delete($name);

The default value for `$ttl` is 0 which means it will be stored forever. For the Memcached engine the `$ttl` can be seconds from the current time as long as it is less than `60*60*24*30` (seconds in 30 days) otherwise it needs to be a Unix timestamp.
Expand Down
3 changes: 3 additions & 0 deletions src/Epi.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Epi
'cache-memcached' => array('base', 'EpiCache.php', 'EpiCache_Memcached.php'),
'config' => array('base', 'EpiConfig.php'),
'database' => array('base', 'EpiDatabase.php'),
'mongo' => array('EpiMongo.php'),
'debug' => array('EpiDebug.php'),
'route' => array('base', 'EpiRoute.php'),
'session' => array('base', 'EpiSession.php', 'session-php', 'session-apc', 'session-memcached'),
Expand All @@ -31,6 +32,7 @@ class Epi

public static function init()
{

$args = func_get_args();
if(!empty($args))
{
Expand Down Expand Up @@ -61,6 +63,7 @@ public static function getSetting($name)

private static function loadDependency($dep)
{

$value = isset(self::$manifest[$dep]) ? self::$manifest[$dep] : $dep;
if(!is_array($value))
{
Expand Down
2 changes: 1 addition & 1 deletion src/EpiDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private function init()

try
{
$this->dbh = new PDO($this->_type . ':host=' . $this->_host . ';dbname=' . $this->_name, $this->_user, $this->_pass);
$this->dbh = new PDO($this->_type . ':host=' . $this->_host . ';dbname=' . $this->_name, $this->_user, $this->_pass, array(\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
$this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(Exception $e)
Expand Down
107 changes: 107 additions & 0 deletions src/EpiMongo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php
class EpiMongo
{
const MONGO = 'mongo';

private static $instances = array(), $type, $dbname, $host, $port, $pass;
private $_type, $_dbname, $_host, $_port, $_pass;
public $mongo, $db;


private function __construct(){}

public static function getInstance($type, $dbname, $host = 'localhost', $port = '27017')
{
$args = func_get_args();
$hash = md5(implode('~', $args));

if(isset(self::$instances[$hash]))
return self::$instances[$hash];

self::$instances[$hash] = new EpiMongo();
self::$instances[$hash]->_type = $type;
self::$instances[$hash]->_dbname = $dbname;
self::$instances[$hash]->_host = $host;
self::$instances[$hash]->_port = $port;
self::$instances[$hash]->_port = $port;

return self::$instances[$hash];
}

public static function employ($type = null, $dbname = null, $host = 'localhost', $port = '27017')
{
if(!empty($type) && !empty($dbname))
{
self::$type = $type;
self::$dbname = $dbname;
self::$host = $host;
self::$port = $port;
}

return array('type' => self::$type, 'dbname' => self::$dbname, 'host' => self::$host, 'port' => self::$port);
}


public function all($dbcollection = '', $params = array())
{

$this->init();
$return = array();

$collection = $this->mongo->selectCollection($this->_dbname, $dbcollection);
$cursor = $collection->find($params);

foreach ($cursor as $document) {
unset($document['_id']);
array_push($return, $document);
}

return $return;
}

public function create($dbcollection = '', $params = array())
{

$this->init();
$return = array();

$collection = $this->mongo->selectCollection($this->_dbname, $dbcollection);
$cursor = $collection->insert($params);

return $cursor;
}

public function count($dbcollection = '', $params = array())
{

$this->init();
$result = $this->db->command(array("distinct" => $dbcollection,"query" => $params));

return $result['stats']['n'];
}



private function init()
{

if(!isset($this->mongo)){
$this->mongo = new MongoClient('mongodb://'.$this->_host.':'.$this->_port);
$this->db = $this->mongo->selectDB($this->_dbname);
}

}



}

function getMongo()
{
$employ = extract(EpiMongo::employ());

if(empty($type) || empty($dbname) || empty($host) || empty($port))
EpiException::raise(new EpiCacheTypeDoesNotExistException('Could not determine which database module to load', 404));
else
return EpiMongo::getInstance($type, $dbname, $host, $port);
}