Skip to content

Commit

Permalink
Merge pull request #7 from itouzigithub/master
Browse files Browse the repository at this point in the history
fixed same class definition load error in PHP 7.0.7
  • Loading branch information
Sammaye authored Jun 13, 2016
2 parents a02ec92 + 0a89b60 commit 8eeee96
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 55 deletions.
82 changes: 41 additions & 41 deletions Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use CApplicationComponent;
use CValidator;

use MongoDB\Client;
use MongoDB\Client as DriverClient;
use MongoDB\Database as DriverDatabase;
use MongoDB\Driver\WriteConcern;
use MongoDB\Driver\ReadPreference;
Expand Down Expand Up @@ -36,13 +36,13 @@ class Client extends CApplicationComponent
* @var string
*/
public $uri;

/**
* Additional options for the connection constructor
* @var array
*/
public $options = [];

public $driverOptions = [];

/**
Expand All @@ -56,7 +56,7 @@ class Client extends CApplicationComponent
* @var boolean
*/
public $enableProfiling = false;

/**
* @var integer number of seconds that query results can remain valid in cache.
* Use 0 or negative value to indicate not caching query results (the default behavior).
Expand All @@ -72,49 +72,49 @@ class Client extends CApplicationComponent
* @see queryCacheID
*/
public $queryCachingDuration = 0;

/**
* @var CCacheDependency|ICacheDependency the dependency that will be used when saving query results into cache.
* @see queryCachingDuration
*/
public $queryCachingDependency;

/**
* @var integer the number of SQL statements that need to be cached next.
* If this is 0, then even if query caching is enabled, no query will be cached.
* Note that each time after executing a SQL statement (whether executed on DB server or fetched from
* query cache), this property will be reduced by 1 until 0.
*/
public $queryCachingCount = 0;

/**
* @var string the ID of the cache application component that is used for query caching.
* Defaults to 'cache' which refers to the primary cache application component.
* Set this property to false if you want to disable query caching.
*/
public $queryCacheID = 'cache';

/**
* The Mongo Connection instance
* @var Mongo MongoClient
*/
private $client;

/**
* The database instance
* @var MongoDB
*/
private $dbs;

private $activeDb;

/**
* Caches reflection properties for our objects so we don't have
* to keep getting them
* @var array
*/
private $meta = array();

/**
* The default action is to find a getX whereby X is the $k param
* you input.
Expand All @@ -128,7 +128,7 @@ public function __get($k)
}
return $this->selectDatabase($k);
}

/**
* Will call a function on the database or error out stating that the function does not exist
* @param string $name
Expand All @@ -142,13 +142,13 @@ public function __call($name, $parameters = array())
}
return call_user_func_array(array($this->getClient(), $name), $parameters);
}

public function init()
{
if(!extension_loaded('mongodb')){
throw new EMongoException(
Yii::t(
'yii',
'yii',
'We could not find the MongoDB extension ( http://php.net/manual/en/mongodb.installation.php ), please install it'
)
);
Expand All @@ -157,28 +157,28 @@ public function init()
// We copy this function to add the subdocument validator as a built in validator
CValidator::$builtInValidators['subdocument'] = 'sammaye\mongoyii\validators\SubdocumentValidator';

$this->client = new Client($this->uri, $this->options, $this->driverOptions);
$this->client = new DriverClient($this->uri, $this->options, $this->driverOptions);

if(!is_array($this->db)){
throw new Exception(Yii::t(
'yii',
'yii',
'The db option needs to be an array of databases indexed by name'
));
}else{
foreach($this->db as $k => $v){

$options = [];
if(is_numeric($k)){
$name = $v;
}else{
$name = $k;
$options = $v;
}

$this->selectDatabase($name, $options);
}
}

parent::init();
}

Expand All @@ -202,7 +202,7 @@ public function selectDatabase($name = null, $options = [])
if(isset($options['active'])){
$this->activeDb = $name;
}

if($name){
if(isset($this->dbs[$name])){
return $this->dbs[$name];
Expand All @@ -213,20 +213,20 @@ public function selectDatabase($name = null, $options = [])
);
return $db;
}

// If we have a default database set let's go looking for it
if($this->activeDb && isset($this->dbs[$this->activeDb])){
return $this->dbs[$this->activeDb];
}elseif($this->activeDb){
throw new Exception($name . ' is default but does not exist');
}

// By default let's return the first in the list
foreach($this->dbs as $db){
return $db;
}
}

public function dropDatabase($databaseName, $options = [])
{
if($this->client->dropDatabase($databaseName, $options)){
Expand All @@ -246,7 +246,7 @@ public function selectCollection($collectionName, $options = [], $databaseName =
}
return $this->getClient()->selectCollection($databaseName, $collectionName, $options);
}

/**
* Sets the document cache for any particular document (EMongoDocument/EMongoModel)
* sent in as the first parameter of this function.
Expand All @@ -261,22 +261,22 @@ public function setDocumentCache($o)
(get_class($o) != 'Document' && get_class($o) != 'Model') /* We can't cache the model */
){
$_meta = array();

$reflect = new ReflectionClass(get_class($o));
$class_vars = $reflect->getProperties(\ReflectionProperty::IS_PUBLIC); // Pre-defined doc attributes

foreach($class_vars as $prop){

if($prop->isStatic()){
continue;
}

$docBlock = $prop->getDocComment();
$field_meta = array(
'name' => $prop->getName(),
'virtual' => $prop->isProtected() || preg_match('/@virtual/i', $docBlock) <= 0 ? false : true
'virtual' => $prop->isProtected() || preg_match('/@virtual/i', $docBlock) <= 0 ? false : true
);

// Lets fetch the data type for this field
// Since we always fetch the data type for this field we make a regex that will only pick out the first
if(preg_match('/@var ([a-zA-Z]+)/', $docBlock, $matches) > 0){
Expand All @@ -287,7 +287,7 @@ public function setDocumentCache($o)
$this->meta[get_class($o)] = $_meta;
}
}

/**
* Get a list of the fields (attributes) for a document from cache
* @param string $name
Expand All @@ -298,15 +298,15 @@ public function getFieldCache($name, $include_virtual = false)
{
$doc = isset($this->meta[$name]) ? $this->meta[$name] : array();
$fields = array();

foreach($doc as $name => $opts){
if($include_virtual || !$opts['virtual']){
$fields[] = $name;
}
}
return $fields;
}

/**
* Just gets the document cache for a model
* @param string $name
Expand All @@ -327,14 +327,14 @@ public function getDocumentCache($name)
public function createMongoIdFromTimestamp($yourTimestamp)
{
static $inc = 0;

$ts = pack('N', $yourTimestamp);
$m = substr(md5(gethostname ()), 0, 3);
$pid = pack('n', getmypid());
$trail = substr(pack('N', $inc ++), 1, 3);

$bin = sprintf("%s%s%s%s", $ts, $m, $pid, $trail);

$id = '';
for($i = 0; $i < 12; $i ++){
$id .= sprintf("%02X", ord($bin[$i]));
Expand All @@ -350,7 +350,7 @@ public function createMongoIdFromTimestamp($yourTimestamp)
* and remain valid for the specified duration.
* If the same query is executed again, the result may be fetched from cache directly
* without actually executing the SQL statement.
*
*
* @param integer $duration
* the number of seconds that query results may remain valid in cache.
* If this is 0, the caching will be disabled.
Expand All @@ -370,7 +370,7 @@ public function cache($duration, $dependency = null, $queryCount = 1)
$this->queryCachingCount = $queryCount;
return $this;
}

public function getSerialisedQuery($criteria = array(), $fields = array(), $sort = array(), $skip = 0, $limit = null)
{
$query = array(
Expand All @@ -382,7 +382,7 @@ public function getSerialisedQuery($criteria = array(), $fields = array(), $sort
);
return json_encode($query);
}

/**
*
* @return array the first element indicates the number of query statements executed,
Expand Down Expand Up @@ -412,7 +412,7 @@ public function getStats()
$timings = $logger->getProfilingResults(null, 'extensions.MongoYii.EMongoDocument.deleteAll');
$count += count($timings);
$time += array_sum($timings);

return array($count, $time);
}
}
28 changes: 14 additions & 14 deletions Cursor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace sammaye\mongoyii;

use MongoDB\Driver\Cursor;
use MongoDB\Driver\Cursor as DriverCursor;

use IteratorIterator;
use Iterator;
Expand All @@ -29,19 +29,19 @@ class Cursor implements Iterator, Countable
* @var string
*/
public $modelClass;

/**
* @var EMongoDocument
*/
public $model;

public $partial;

/**
* @var array|MongoCursor|EMongoDocument[]
*/
private $cursor = array();

/**
* @var EMongoDocument
*/
Expand All @@ -62,11 +62,11 @@ public function __construct($modelClass, $cursor, $options = [])
$this->modelClass = get_class($modelClass);
$this->model = $modelClass;
}

$it = new IteratorIterator($cursor);
$it->rewind();
$this->cursor = $it;

if($options['partial']){
$this->partial = true;
}
Expand All @@ -83,16 +83,16 @@ public function __construct($modelClass, $cursor, $options = [])
*/
public function __call($method, $params = [])
{
if($this->cursor instanceof Cursor && method_exists($this->cursor, $method)){
if($this->cursor instanceof DriverCursor && method_exists($this->cursor, $method)){
return call_user_func_array(array($this->cursor, $method), $params);
}
throw new Exception(Yii::t(
'yii',
'Call to undefined function {method} on the cursor',
'yii',
'Call to undefined function {method} on the cursor',
['{method}' => $method]
));
}

public function count()
{
throw new Exception('Count can no longer be done on the cursor!!');
Expand Down Expand Up @@ -121,8 +121,8 @@ public function current()
return $this->current = $this->cursor->current();
}else{
return $this->current = $this->model->populateRecord(
$this->cursor->current(),
true,
$this->cursor->current(),
true,
$this->partial
);
}
Expand Down Expand Up @@ -163,4 +163,4 @@ public function valid()
{
return $this->cursor->valid();
}
}
}

0 comments on commit 8eeee96

Please sign in to comment.