Skip to content
This repository has been archived by the owner on Nov 11, 2020. It is now read-only.

Commit

Permalink
Merge pull request #106 from doctrine/events
Browse files Browse the repository at this point in the history
Events refactoring and support for modifying data in EventArgs
  • Loading branch information
jmikola committed May 30, 2013
2 parents 5af8ab9 + 161be2d commit 379852c
Show file tree
Hide file tree
Showing 25 changed files with 996 additions and 134 deletions.
89 changes: 60 additions & 29 deletions lib/Doctrine/MongoDB/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
use Doctrine\MongoDB\Event\AggregateEventArgs;
use Doctrine\MongoDB\Event\DistinctEventArgs;
use Doctrine\MongoDB\Event\EventArgs;
use Doctrine\MongoDB\Event\FindEventArgs;
use Doctrine\MongoDB\Event\GroupEventArgs;
use Doctrine\MongoDB\Event\MapReduceEventArgs;
use Doctrine\MongoDB\Event\MutableEventArgs;
use Doctrine\MongoDB\Event\NearEventArgs;
use Doctrine\MongoDB\Event\UpdateEventArgs;
use Doctrine\MongoDB\Util\ReadPreference;
Expand Down Expand Up @@ -178,7 +180,9 @@ public function aggregate(array $pipeline /* , array $op, ... */)
$result = $this->doAggregate($pipeline);

if ($this->eventManager->hasListeners(Events::postAggregate)) {
$this->eventManager->dispatchEvent(Events::postAggregate, new EventArgs($this, $result));
$eventArgs = new MutableEventArgs($this, $result);
$this->eventManager->dispatchEvent(Events::postAggregate, $eventArgs);
$result = $eventArgs->getData();
}

return $result;
Expand Down Expand Up @@ -206,13 +210,15 @@ protected function doAggregate(array $pipeline)
public function batchInsert(array &$a, array $options = array())
{
if ($this->eventManager->hasListeners(Events::preBatchInsert)) {
$this->eventManager->dispatchEvent(Events::preBatchInsert, new EventArgs($this, $a));
$this->eventManager->dispatchEvent(Events::preBatchInsert, new EventArgs($this, $a, $options));
}

$result = $this->doBatchInsert($a, $options);

if ($this->eventManager->hasListeners(Events::postBatchInsert)) {
$this->eventManager->dispatchEvent(Events::postBatchInsert, new EventArgs($this, $result));
$eventArgs = new MutableEventArgs($this, $result);
$this->eventManager->dispatchEvent(Events::postBatchInsert, $eventArgs);
$result = $eventArgs->getData();
}

return $result;
Expand All @@ -227,7 +233,7 @@ protected function doBatchInsert(array &$a, array $options = array())
public function update($query, array $newObj, array $options = array())
{
if ($this->eventManager->hasListeners(Events::preUpdate)) {
$this->eventManager->dispatchEvent(Events::preUpdate, new UpdateEventArgs($this, $query, $newObj));
$this->eventManager->dispatchEvent(Events::preUpdate, new UpdateEventArgs($this, $query, $newObj, $options));
}

$result = $this->doUpdate($query, $newObj, $options);
Expand Down Expand Up @@ -257,13 +263,15 @@ public function upsert($query, array $newObj, array $options = array())
public function find(array $query = array(), array $fields = array())
{
if ($this->eventManager->hasListeners(Events::preFind)) {
$this->eventManager->dispatchEvent(Events::preFind, new EventArgs($this, $query));
$this->eventManager->dispatchEvent(Events::preFind, new FindEventArgs($this, $query, $fields));
}

$result = $this->doFind($query, $fields);

if ($this->eventManager->hasListeners(Events::postFind)) {
$this->eventManager->dispatchEvent(Events::postFind, new EventArgs($this, $result));
$eventArgs = new MutableEventArgs($this, $result);
$this->eventManager->dispatchEvent(Events::postFind, $eventArgs);
$result = $eventArgs->getData();
}

return $result;
Expand All @@ -287,13 +295,15 @@ protected function wrapCursor(\MongoCursor $cursor, $query, $fields)
public function findOne(array $query = array(), array $fields = array())
{
if ($this->eventManager->hasListeners(Events::preFindOne)) {
$this->eventManager->dispatchEvent(Events::preFindOne, new EventArgs($this, $query));
$this->eventManager->dispatchEvent(Events::preFindOne, new FindEventArgs($this, $query, $fields));
}

$result = $this->doFindOne($query, $fields);

if ($this->eventManager->hasListeners(Events::postFindOne)) {
$this->eventManager->dispatchEvent(Events::postFindOne, new EventArgs($this, $result));
$eventArgs = new MutableEventArgs($this, $result);
$this->eventManager->dispatchEvent(Events::postFindOne, $eventArgs);
$result = $eventArgs->getData();
}

return $result;
Expand All @@ -310,16 +320,18 @@ protected function doFindOne(array $query, array $fields)
public function findAndRemove(array $query, array $options = array())
{
if ($this->eventManager->hasListeners(Events::preFindAndRemove)) {
$this->eventManager->dispatchEvent(Events::preFindAndRemove, new EventArgs($this, $query));
$this->eventManager->dispatchEvent(Events::preFindAndRemove, new EventArgs($this, $query, $options));
}

$document = $this->doFindAndRemove($query, $options);
$result = $this->doFindAndRemove($query, $options);

if ($this->eventManager->hasListeners(Events::postFindAndRemove)) {
$this->eventManager->dispatchEvent(Events::postFindAndRemove, new EventArgs($this, $document));
$eventArgs = new MutableEventArgs($this, $result);
$this->eventManager->dispatchEvent(Events::postFindAndRemove, $eventArgs);
$result = $eventArgs->getData();
}

return $document;
return $result;
}

protected function doFindAndRemove(array $query, array $options = array())
Expand All @@ -338,16 +350,18 @@ protected function doFindAndRemove(array $query, array $options = array())
public function findAndUpdate(array $query, array $newObj, array $options = array())
{
if ($this->eventManager->hasListeners(Events::preFindAndUpdate)) {
$this->eventManager->dispatchEvent(Events::preFindAndUpdate, new UpdateEventArgs($this, $query, $query));
$this->eventManager->dispatchEvent(Events::preFindAndUpdate, new UpdateEventArgs($this, $query, $newObj, $options));
}

$document = $this->doFindAndUpdate($query, $newObj, $options);
$result = $this->doFindAndUpdate($query, $newObj, $options);

if ($this->eventManager->hasListeners(Events::postFindAndUpdate)) {
$this->eventManager->dispatchEvent(Events::postFindAndUpdate, new EventArgs($this, $document));
$eventArgs = new MutableEventArgs($this, $result);
$this->eventManager->dispatchEvent(Events::postFindAndUpdate, $eventArgs);
$result = $eventArgs->getData();
}

return $document;
return $result;
}

protected function doFindAndUpdate(array $query, array $newObj, array $options)
Expand All @@ -365,13 +379,15 @@ protected function doFindAndUpdate(array $query, array $newObj, array $options)
public function near(array $near, array $query = array(), array $options = array())
{
if ($this->eventManager->hasListeners(Events::preNear)) {
$this->eventManager->dispatchEvent(Events::preNear, new NearEventArgs($this, $query, $near));
$this->eventManager->dispatchEvent(Events::preNear, new NearEventArgs($this, $query, $near, $options));
}

$result = $this->doNear($near, $query, $options);

if ($this->eventManager->hasListeners(Events::postNear)) {
$this->eventManager->dispatchEvent(Events::postNear, new EventArgs($this, $result));
$eventArgs = new MutableEventArgs($this, $result);
$this->eventManager->dispatchEvent(Events::postNear, $eventArgs);
$result = $eventArgs->getData();
}

return $result;
Expand All @@ -395,13 +411,18 @@ protected function doNear(array $near, array $query, array $options)
public function distinct($field, array $query = array(), array $options = array())
{
if ($this->eventManager->hasListeners(Events::preDistinct)) {
/* The distinct command currently does not have options beyond field
* and query, so do not include it in the event args.
*/
$this->eventManager->dispatchEvent(Events::preDistinct, new DistinctEventArgs($this, $field, $query));
}

$result = $this->doDistinct($field, $query, $options);

if ($this->eventManager->hasListeners(Events::postDistinct)) {
$this->eventManager->dispatchEvent(Events::postDistinct, new EventArgs($this, $result));
$eventArgs = new MutableEventArgs($this, $result);
$this->eventManager->dispatchEvent(Events::postDistinct, $eventArgs);
$result = $eventArgs->getData();
}

return $result;
Expand All @@ -425,13 +446,15 @@ protected function doDistinct($field, array $query, array $options)
public function mapReduce($map, $reduce, array $out = array('inline' => true), array $query = array(), array $options = array())
{
if ($this->eventManager->hasListeners(Events::preMapReduce)) {
$this->eventManager->dispatchEvent(Events::preMapReduce, new MapReduceEventArgs($this, $map, $reduce, $out, $query));
$this->eventManager->dispatchEvent(Events::preMapReduce, new MapReduceEventArgs($this, $map, $reduce, $out, $query, $options));
}

$result = $this->doMapReduce($map, $reduce, $out, $query, $options);

if ($this->eventManager->hasListeners(Events::postMapReduce)) {
$this->eventManager->dispatchEvent(Events::postMapReduce, new EventArgs($this, $result));
$eventArgs = new MutableEventArgs($this, $result);
$this->eventManager->dispatchEvent(Events::postMapReduce, $eventArgs);
$result = $eventArgs->getData();
}

return $result;
Expand Down Expand Up @@ -543,7 +566,9 @@ public function getDBRef(array $reference)
$result = $this->doGetDBRef($reference);

if ($this->eventManager->hasListeners(Events::postGetDBRef)) {
$this->eventManager->dispatchEvent(Events::postGetDBRef, new EventArgs($this, $result));
$eventArgs = new MutableEventArgs($this, $result);
$this->eventManager->dispatchEvent(Events::postGetDBRef, $eventArgs);
$result = $eventArgs->getData();
}

return $result;
Expand All @@ -560,13 +585,15 @@ protected function doGetDBRef(array $reference)
public function group($keys, array $initial, $reduce, array $options = array())
{
if ($this->eventManager->hasListeners(Events::preGroup)) {
$this->eventManager->dispatchEvent(Events::preGroup, new GroupEventArgs($this, $keys, $initial, $reduce));
$this->eventManager->dispatchEvent(Events::preGroup, new GroupEventArgs($this, $keys, $initial, $reduce, $options));
}

$result = $this->doGroup($keys, $initial, $reduce, $options);

if ($this->eventManager->hasListeners(Events::postGroup)) {
$this->eventManager->dispatchEvent(Events::postGroup, new EventArgs($this, $result));
$eventArgs = new MutableEventArgs($this, $result);
$this->eventManager->dispatchEvent(Events::postGroup, $eventArgs);
$result = $eventArgs->getData();
}

return $result;
Expand Down Expand Up @@ -599,13 +626,15 @@ protected function doGroup($keys, array $initial, $reduce, array $options)
public function insert(array &$a, array $options = array())
{
if ($this->eventManager->hasListeners(Events::preInsert)) {
$this->eventManager->dispatchEvent(Events::preInsert, new EventArgs($this, $a));
$this->eventManager->dispatchEvent(Events::preInsert, new EventArgs($this, $a, $options));
}

$result = $this->doInsert($a, $options);

if ($this->eventManager->hasListeners(Events::postInsert)) {
$this->eventManager->dispatchEvent(Events::postInsert, new EventArgs($this, $result));
$eventArgs = new MutableEventArgs($this, $result);
$this->eventManager->dispatchEvent(Events::postInsert, $eventArgs);
$result = $eventArgs->getData();
}
return $result;
}
Expand All @@ -623,7 +652,7 @@ protected function doInsert(array &$a, array $options)
public function remove(array $query, array $options = array())
{
if ($this->eventManager->hasListeners(Events::preRemove)) {
$this->eventManager->dispatchEvent(Events::preRemove, new EventArgs($this, $query));
$this->eventManager->dispatchEvent(Events::preRemove, new EventArgs($this, $query, $options));
}

$result = $this->doRemove($query, $options);
Expand All @@ -643,13 +672,15 @@ protected function doRemove(array $query, array $options)
public function save(array &$a, array $options = array())
{
if ($this->eventManager->hasListeners(Events::preSave)) {
$this->eventManager->dispatchEvent(Events::preSave, new EventArgs($this, $a));
$this->eventManager->dispatchEvent(Events::preSave, new EventArgs($this, $a, $options));
}

$result = $this->doSave($a, $options);

if ($this->eventManager->hasListeners(Events::postSave)) {
$this->eventManager->dispatchEvent(Events::postSave, new EventArgs($this, $result));
$eventArgs = new MutableEventArgs($this, $result);
$this->eventManager->dispatchEvent(Events::postSave, $eventArgs);
$result = $eventArgs->getData();
}

return $result;
Expand Down
26 changes: 23 additions & 3 deletions lib/Doctrine/MongoDB/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,33 @@ public function command(array $data, array $options = array())
return $this->getMongoDB()->command($data, $options);
}

public function createCollection($name, $capped = false, $size = 0, $max = 0)
/**
* Create a collection.
*
* @param string $name Collection name
* @param boolean|array $cappedOrOptions Capped collection indicator or an
* options array (for driver 1.4+)
* @param integer $size Storage size for fixed collections
* (ignored if options array is used)
* @param integer $max Max documents for fixed collections
* (ignored if options array is used)
* @return Collection
*/
public function createCollection($name, $cappedOrOptions = false, $size = 0, $max = 0)
{
$options = is_array($cappedOrOptions)
? array_merge(array('capped' => false, 'size' => 0, 'max' => 0), $cappedOrOptions)
: array('capped' => $cappedOrOptions, 'size' => $size, 'max' => $max);

if ($this->eventManager->hasListeners(Events::preCreateCollection)) {
$this->eventManager->dispatchEvent(Events::preCreateCollection, new CreateCollectionEventArgs($this, $name, $capped, $size, $max));
$this->eventManager->dispatchEvent(Events::preCreateCollection, new CreateCollectionEventArgs($this, $name, $options));
}

$this->getMongoDB()->createCollection($name, $capped, $size, $max);
if (version_compare(phpversion('mongo'), '1.4.0', '>=')) {
$this->getMongoDB()->createCollection($name, $options);
} else {
$this->getMongoDB()->createCollection($name, $options['capped'], $options['size'], $options['max']);
}

$result = $this->selectCollection($name);

Expand Down
9 changes: 7 additions & 2 deletions lib/Doctrine/MongoDB/Event/AggregateEventArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
use Doctrine\Common\EventArgs as BaseEventArgs;

/**
* Aggregation event args.
* Event args for the aggregate command.
*
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link www.doctrine-project.com
Expand All @@ -34,12 +34,17 @@ class AggregateEventArgs extends BaseEventArgs
private $invoker;
private $pipeline;

public function __construct($invoker, array &$pipeline)
public function __construct($invoker, array $pipeline)
{
$this->invoker = $invoker;
$this->pipeline = $pipeline;
}

public function getInvoker()
{
return $this->invoker;
}

public function getPipeline()
{
return $this->pipeline;
Expand Down
Loading

0 comments on commit 379852c

Please sign in to comment.