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

Commit

Permalink
Merge branch 'feature-allowEventManagerToModifyQueries'
Browse files Browse the repository at this point in the history
Closes #233.

* feature-allowEventManagerToModifyQueries:
  Use MutableEventArgs for postRemove and postUpdate events
  Keep EventArgs immutable and selectively use MutableEventArgs
  Ran file through php-cs-fixer
  Cast $field to string.
  Corrected since tags.
  Added since tags
  Added dockblocks to functions
  Added remainder of tests.
  Fixed class name to avoid namespace clash
  Example of how I think the rest of my tests should be written.
  Added unit test to ensure the setters work.
  Allow for "pre" hooks to have the ability to modify the context information of the event.
  • Loading branch information
alcaeus committed Mar 17, 2016
2 parents f5a0c76 + a9fae13 commit 2ffb37a
Show file tree
Hide file tree
Showing 19 changed files with 979 additions and 22 deletions.
90 changes: 73 additions & 17 deletions lib/Doctrine/MongoDB/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@ public function aggregate(array $pipeline, array $options = array() /* , array $
}

if ($this->eventManager->hasListeners(Events::preAggregate)) {
$this->eventManager->dispatchEvent(Events::preAggregate, new AggregateEventArgs($this, $pipeline, $options));
$aggregateEventArgs = new AggregateEventArgs($this, $pipeline, $options);
$this->eventManager->dispatchEvent(Events::preAggregate, $aggregateEventArgs);
$pipeline = $aggregateEventArgs->getPipeline();
$options = $aggregateEventArgs->getOptions();
}

$result = $this->doAggregate($pipeline, $options);
Expand All @@ -146,7 +149,10 @@ public function aggregate(array $pipeline, array $options = array() /* , array $
public function batchInsert(array &$a, array $options = array())
{
if ($this->eventManager->hasListeners(Events::preBatchInsert)) {
$this->eventManager->dispatchEvent(Events::preBatchInsert, new EventArgs($this, $a, $options));
$eventArgs = new EventArgs($this, $a, $options);
$this->eventManager->dispatchEvent(Events::preBatchInsert, $eventArgs);
$a = $eventArgs->getData();
$options = $eventArgs->getOptions();
}

$result = $this->doBatchInsert($a, $options);
Expand Down Expand Up @@ -256,7 +262,10 @@ public function distinct($field, array $query = array(), array $options = array(
/* 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));
$distinctEventArgs = new DistinctEventArgs($this, $field, $query);
$this->eventManager->dispatchEvent(Events::preDistinct, $distinctEventArgs);
$query = $distinctEventArgs->getQuery();
$field = $distinctEventArgs->getField();
}

$result = $this->doDistinct($field, $query, $options);
Expand Down Expand Up @@ -324,7 +333,10 @@ public function ensureIndex(array $keys, array $options = array())
public function find(array $query = array(), array $fields = array())
{
if ($this->eventManager->hasListeners(Events::preFind)) {
$this->eventManager->dispatchEvent(Events::preFind, new FindEventArgs($this, $query, $fields));
$findEventArgs = new FindEventArgs($this, $query, $fields);
$this->eventManager->dispatchEvent(Events::preFind, $findEventArgs);
$query = $findEventArgs->getQuery();
$fields = $findEventArgs->getFields();
}

$result = $this->doFind($query, $fields);
Expand Down Expand Up @@ -352,7 +364,10 @@ public function find(array $query = array(), array $fields = array())
public function findAndRemove(array $query, array $options = array())
{
if ($this->eventManager->hasListeners(Events::preFindAndRemove)) {
$this->eventManager->dispatchEvent(Events::preFindAndRemove, new EventArgs($this, $query, $options));
$eventArgs = new MutableEventArgs($this, $query, $options);
$this->eventManager->dispatchEvent(Events::preFindAndRemove, $eventArgs);
$query = $eventArgs->getData();
$options = $eventArgs->getOptions();
}

$result = $this->doFindAndRemove($query, $options);
Expand Down Expand Up @@ -381,7 +396,11 @@ public function findAndRemove(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, $newObj, $options));
$updateEventArgs = new UpdateEventArgs($this, $query, $newObj, $options);
$this->eventManager->dispatchEvent(Events::preFindAndUpdate, $updateEventArgs);
$query = $updateEventArgs->getQuery();
$newObj = $updateEventArgs->getNewObj();
$options = $updateEventArgs->getOptions();
}

$result = $this->doFindAndUpdate($query, $newObj, $options);
Expand All @@ -408,7 +427,10 @@ public function findAndUpdate(array $query, array $newObj, array $options = arra
public function findOne(array $query = array(), array $fields = array())
{
if ($this->eventManager->hasListeners(Events::preFindOne)) {
$this->eventManager->dispatchEvent(Events::preFindOne, new FindEventArgs($this, $query, $fields));
$findEventArgs = new FindEventArgs($this, $query, $fields);
$this->eventManager->dispatchEvent(Events::preFindOne, $findEventArgs);
$query = $findEventArgs->getQuery();
$fields = $findEventArgs->getFields();
}

$result = $this->doFindOne($query, $fields);
Expand Down Expand Up @@ -444,7 +466,9 @@ public function getDatabase()
public function getDBRef(array $reference)
{
if ($this->eventManager->hasListeners(Events::preGetDBRef)) {
$this->eventManager->dispatchEvent(Events::preGetDBRef, new EventArgs($this, $reference));
$eventArgs = new EventArgs($this, $reference);
$this->eventManager->dispatchEvent(Events::preGetDBRef, $eventArgs);
$reference = $eventArgs->getData();
}

$result = $this->doGetDBRef($reference);
Expand Down Expand Up @@ -584,7 +608,12 @@ public function setSlaveOkay($ok = true)
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, $options));
$groupEventArgs = new GroupEventArgs($this, $keys, $initial, $reduce, $options);
$this->eventManager->dispatchEvent(Events::preGroup, $groupEventArgs);
$keys = $groupEventArgs->getKeys();
$initial = $groupEventArgs->getInitial();
$reduce = $groupEventArgs->getReduce();
$options = $groupEventArgs->getOptions();
}

$result = $this->doGroup($keys, $initial, $reduce, $options);
Expand All @@ -611,7 +640,10 @@ public function group($keys, array $initial, $reduce, array $options = array())
public function insert(array &$a, array $options = array())
{
if ($this->eventManager->hasListeners(Events::preInsert)) {
$this->eventManager->dispatchEvent(Events::preInsert, new EventArgs($this, $a, $options));
$eventArgs = new EventArgs($this, $a, $options);
$this->eventManager->dispatchEvent(Events::preInsert, $eventArgs);
$a = $eventArgs->getData();
$options = $eventArgs->getOptions();
}

$result = $this->doInsert($a, $options);
Expand Down Expand Up @@ -662,7 +694,13 @@ public function isFieldIndexed($fieldName)
public function mapReduce($map, $reduce, $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, $options));
$mapReduceEventArgs = new MapReduceEventArgs($this, $map, $reduce, $out, $query, $options);
$this->eventManager->dispatchEvent(Events::preMapReduce, $mapReduceEventArgs);
$map = $mapReduceEventArgs->getMap();
$reduce = $mapReduceEventArgs->getReduce();
$out = $mapReduceEventArgs->getOut();
$query = $mapReduceEventArgs->getQuery();
$options = $mapReduceEventArgs->getOptions();
}

$result = $this->doMapReduce($map, $reduce, $out, $query, $options);
Expand Down Expand Up @@ -697,7 +735,11 @@ public function mapReduce($map, $reduce, $out = array('inline' => true), array $
public function near($near, array $query = array(), array $options = array())
{
if ($this->eventManager->hasListeners(Events::preNear)) {
$this->eventManager->dispatchEvent(Events::preNear, new NearEventArgs($this, $query, $near, $options));
$nearEventArgs = new NearEventArgs($this, $query, $near, $options);
$this->eventManager->dispatchEvent(Events::preNear, $nearEventArgs);
$query = $nearEventArgs->getQuery();
$near = $nearEventArgs->getNear();
$options = $nearEventArgs->getOptions();
}

$result = $this->doNear($near, $query, $options);
Expand Down Expand Up @@ -742,13 +784,18 @@ public function parallelCollectionScan($numCursors)
public function remove(array $query, array $options = array())
{
if ($this->eventManager->hasListeners(Events::preRemove)) {
$this->eventManager->dispatchEvent(Events::preRemove, new EventArgs($this, $query, $options));
$eventArgs = new MutableEventArgs($this, $query, $options);
$this->eventManager->dispatchEvent(Events::preRemove, $eventArgs);
$query = $eventArgs->getData();
$options = $eventArgs->getOptions();
}

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

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

return $result;
Expand All @@ -767,7 +814,10 @@ public function remove(array $query, array $options = array())
public function save(array &$a, array $options = array())
{
if ($this->eventManager->hasListeners(Events::preSave)) {
$this->eventManager->dispatchEvent(Events::preSave, new EventArgs($this, $a, $options));
$eventArgs = new EventArgs($this, $a, $options);
$this->eventManager->dispatchEvent(Events::preSave, $eventArgs);
$a = $eventArgs->getData();
$options = $eventArgs->getOptions();
}

$result = $this->doSave($a, $options);
Expand Down Expand Up @@ -800,13 +850,19 @@ 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, $options));
$updateEventArgs = new UpdateEventArgs($this, $query, $newObj, $options);
$this->eventManager->dispatchEvent(Events::preUpdate, $updateEventArgs);
$query = $updateEventArgs->getQuery();
$newObj = $updateEventArgs->getNewObj();
$options = $updateEventArgs->getOptions();
}

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

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

return $result;
Expand Down
25 changes: 25 additions & 0 deletions lib/Doctrine/MongoDB/Event/AggregateEventArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,46 @@ public function __construct($invoker, array $pipeline, array $options = array())
$this->options = $options;
}

/**
* @return object
*/
public function getInvoker()
{
return $this->invoker;
}

/**
* @return array
*/
public function getPipeline()
{
return $this->pipeline;
}

/**
* @since 1.2
* @return array
*/
public function getOptions()
{
return $this->options;
}

/**
* @param array $pipeline
* @since 1.3
*/
public function setPipeline(array $pipeline)
{
$this->pipeline = $pipeline;
}

/**
* @param array $options
* @since 1.3
*/
public function setOptions(array $options)
{
$this->options = $options;
}
}
12 changes: 12 additions & 0 deletions lib/Doctrine/MongoDB/Event/CreateCollectionEventArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,33 @@ public function __construct($invoker, $name, $cappedOrOptions, $size = 0, $max =
$this->options = $options;
}

/**
* @return object
*/
public function getInvoker()
{
return $this->invoker;
}

/**
* @return string
*/
public function getName()
{
return $this->name;
}

/**
* @return array|bool
*/
public function getOptions()
{
return $this->options;
}

/**
* @deprecated 1.1 Replaced by options; will be removed for 2.0
* @return mixed
*/
public function getCapped()
{
Expand All @@ -80,6 +90,7 @@ public function getCapped()

/**
* @deprecated 1.1 Replaced by options; will be removed for 2.0
* @return mixed
*/
public function getSize()
{
Expand All @@ -88,6 +99,7 @@ public function getSize()

/**
* @deprecated 1.1 Replaced by options; will be removed for 2.0
* @return mixed
*/
public function getMax()
{
Expand Down
29 changes: 28 additions & 1 deletion lib/Doctrine/MongoDB/Event/DistinctEventArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,49 @@ class DistinctEventArgs extends BaseEventArgs
public function __construct($invoker, $field, array $query)
{
$this->invoker = $invoker;
$this->field = $field;
$this->field = (string) $field;
$this->query = $query;
}

/**
* @return object
*/
public function getInvoker()
{
return $this->invoker;
}

/**
* @return string
*/
public function getField()
{
return $this->field;
}

/**
* @return array
*/
public function getQuery()
{
return $this->query;
}

/**
* @param $query
* @since 1.3
*/
public function setQuery($query)
{
$this->query = $query;
}

/**
* @param string $field
* @since 1.3
*/
public function setField($field)
{
$this->field = (string) $field;
}
}
9 changes: 9 additions & 0 deletions lib/Doctrine/MongoDB/Event/EventArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,25 @@ public function __construct($invoker, $data = null, array $options = array())
$this->options = $options;
}

/**
* @return object
*/
public function getInvoker()
{
return $this->invoker;
}

/**
* @return mixed|null
*/
public function getData()
{
return $this->data;
}

/**
* @return array
*/
public function getOptions()
{
return $this->options;
Expand Down
Loading

0 comments on commit 2ffb37a

Please sign in to comment.