Skip to content
Closed
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 composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"psr/log": "~1.0",
"psr/simple-cache": "^1.0",
"jmikola/geojson": "~1.0",
"symfony/event-dispatcher": ">=2.0 <5.0",
"symfony/event-dispatcher": ">=2.0 <6.0",
"symfony/polyfill-php55": "~1.0"
},
"require-dev": {
Expand Down
46 changes: 31 additions & 15 deletions src/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@
use Sokil\Mongo\Exception\WriteException;
use Symfony\Component\EventDispatcher\EventDispatcher;
use GeoJson\Geometry\Geometry;
use Sokil\Mongo\Event\ValidateErrorEvent;
use Sokil\Mongo\Event\DeleteAfterEvent;
use Sokil\Mongo\Event\DeleteBeforeEvent;
use Sokil\Mongo\Event\ValidateAfterEvent;
use Sokil\Mongo\Event\ValidateBeforeEvent;
use Sokil\Mongo\Event\SaveAfterEvent;
use Sokil\Mongo\Event\SaveBeforeEvent;
use Sokil\Mongo\Event\UpdateBeforeEvent;
use Sokil\Mongo\Event\UpdateAfterEvent;
use Sokil\Mongo\Event\InsertAfterEvent;
use Sokil\Mongo\Event\InsertBeforeEvent;
use Sokil\Mongo\Event\ConstructAfterEvent;

/**
* Instance of this class is a representation of one document from collection.
Expand Down Expand Up @@ -136,7 +148,7 @@ public function __construct(
}

// execute after construct event handlers
$this->triggerEvent('afterConstruct');
$this->triggerEvent('afterConstruct', new ConstructAfterEvent());
}

public function getOptions()
Expand Down Expand Up @@ -211,7 +223,7 @@ private function initDelegates()
{
// start event dispatching
$this->eventDispatcher = new EventDispatcher;

// create operator
$this->operator = $this->getCollection()->operator();

Expand Down Expand Up @@ -256,7 +268,7 @@ public function __call($name, $arguments)
array($this->eventDispatcher, 'addListener'),
$addListenerArguments
);

return $this;
}

Expand Down Expand Up @@ -549,6 +561,10 @@ public function triggerEvent($eventName, Event $event = null)

$event->setTarget($this);

if (interface_exists('Psr\EventDispatcher\EventDispatcherInterface')) {
return $this->eventDispatcher->dispatch($event);
}

return $this->eventDispatcher->dispatch($eventName, $event);
}

Expand Down Expand Up @@ -646,20 +662,20 @@ public function isStored()
*/
public function validate()
{
if ($this->triggerEvent(self::EVENT_NAME_BEFORE_VALIDATE)->isCancelled()) {
if ($this->triggerEvent(self::EVENT_NAME_BEFORE_VALIDATE, new ValidateBeforeEvent())->isCancelled()) {
return $this;
}

if (!$this->isValid()) {
$exception = new InvalidDocumentException('Document not valid');
$exception->setDocument($this);

$this->triggerEvent(self::EVENT_NAME_VALIDATE_ERROR);
$this->triggerEvent(self::EVENT_NAME_VALIDATE_ERROR, new ValidateErrorEvent());

throw $exception;
}

$this->triggerEvent(self::EVENT_NAME_AFTER_VALIDATE);
$this->triggerEvent(self::EVENT_NAME_AFTER_VALIDATE, new ValidateAfterEvent());

return $this;
}
Expand Down Expand Up @@ -1158,7 +1174,7 @@ public function bitwiceXor($field, $value)
*/
private function internalInsert()
{
if ($this->triggerEvent(self::EVENT_NAME_BEFORE_INSERT)->isCancelled()) {
if ($this->triggerEvent(self::EVENT_NAME_BEFORE_INSERT, new InsertBeforeEvent())->isCancelled()) {
return;
}

Expand All @@ -1182,7 +1198,7 @@ private function internalInsert()
$this->defineId($document['_id']);

// after insert event
$this->triggerEvent(self::EVENT_NAME_AFTER_INSERT);
$this->triggerEvent(self::EVENT_NAME_AFTER_INSERT, new InsertAfterEvent());
}

/**
Expand All @@ -1193,7 +1209,7 @@ private function internalInsert()
*/
private function internalUpdate()
{
if ($this->triggerEvent(self::EVENT_NAME_BEFORE_UPDATE)->isCancelled()) {
if ($this->triggerEvent(self::EVENT_NAME_BEFORE_UPDATE, new UpdateBeforeEvent())->isCancelled()) {
return;
}

Expand Down Expand Up @@ -1245,7 +1261,7 @@ private function internalUpdate()
$this->getOperator()->reset();
}

$this->triggerEvent(self::EVENT_NAME_AFTER_UPDATE);
$this->triggerEvent(self::EVENT_NAME_AFTER_UPDATE, new UpdateAfterEvent());
}

/**
Expand All @@ -1270,7 +1286,7 @@ public function save($validate = true)
}

// handle beforeSave event
if ($this->triggerEvent(self::EVENT_NAME_BEFORE_SAVE)->isCancelled()) {
if ($this->triggerEvent(self::EVENT_NAME_BEFORE_SAVE, new SaveBeforeEvent())->isCancelled()) {
return $this;
}

Expand All @@ -1282,11 +1298,11 @@ public function save($validate = true)
}

// handle afterSave event
$this->triggerEvent(self::EVENT_NAME_AFTER_SAVE);
$this->triggerEvent(self::EVENT_NAME_AFTER_SAVE, new SaveAfterEvent());

// set document unmodified
$this->apply();

return $this;
}

Expand All @@ -1309,7 +1325,7 @@ public function isSaveRequired()
*/
public function delete()
{
if ($this->triggerEvent(self::EVENT_NAME_BEFORE_DELETE)->isCancelled()) {
if ($this->triggerEvent(self::EVENT_NAME_BEFORE_DELETE, new DeleteBeforeEvent())->isCancelled()) {
return $this;
}

Expand All @@ -1321,7 +1337,7 @@ public function delete()
throw new Exception(sprintf('Delete document error: %s', $status['err']));
}

$this->triggerEvent(self::EVENT_NAME_AFTER_DELETE);
$this->triggerEvent(self::EVENT_NAME_AFTER_DELETE, new DeleteAfterEvent());

// drop from document's pool
$this->getCollection()->removeDocumentFromDocumentPool($this);
Expand Down
144 changes: 100 additions & 44 deletions src/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,54 +11,110 @@

namespace Sokil\Mongo;

class Event extends \Symfony\Component\EventDispatcher\Event
if (class_exists('Symfony\Contracts\EventDispatcher\Event'))
{
/**
* @var mixed $target target object, on which event is fired
*/
private $target;

private $cancelled = false;

/**
* Set target object, on which event is fired
* @param mixed $target
* @return \Sokil\Mongo\Event
*/
public function setTarget($target)
class Event extends \Symfony\Contracts\EventDispatcher\Event
{
$this->target = $target;
return $this;
}
/**
* @var mixed $target target object, on which event is fired
*/
private $target;

/**
* Get target object, on which event is fired
* @return mixed
*/
public function getTarget()
{
return $this->target;
}

/**
* Check if operation execution cancelled
*/
public function isCancelled()
{
return $this->cancelled;
private $cancelled = false;

/**
* Set target object, on which event is fired
* @param mixed $target
* @return \Sokil\Mongo\Event
*/
public function setTarget($target)
{
$this->target = $target;
return $this;
}

/**
* Get target object, on which event is fired
* @return mixed
*/
public function getTarget()
{
return $this->target;
}

/**
* Check if operation execution cancelled
*/
public function isCancelled()
{
return $this->cancelled;
}

/**
* Cancel related operation execution. If called as beforeInsert
* handler, than insert will be cancelled.
*/
public function cancel()
{
$this->cancelled = true;

// propagation also not need
$this->stopPropagation();

return $this;
}
}

/**
* Cancel related operation execution. If called as beforeInsert
* handler, than insert will be cancelled.
*/
public function cancel()
}
else {
class Event extends \Symfony\Component\EventDispatcher\Event
{
$this->cancelled = true;

// propagation also not need
$this->stopPropagation();

return $this;
/**
* @var mixed $target target object, on which event is fired
*/
private $target;

private $cancelled = false;

/**
* Set target object, on which event is fired
* @param mixed $target
* @return \Sokil\Mongo\Event
*/
public function setTarget($target)
{
$this->target = $target;
return $this;
}

/**
* Get target object, on which event is fired
* @return mixed
*/
public function getTarget()
{
return $this->target;
}

/**
* Check if operation execution cancelled
*/
public function isCancelled()
{
return $this->cancelled;
}

/**
* Cancel related operation execution. If called as beforeInsert
* handler, than insert will be cancelled.
*/
public function cancel()
{
$this->cancelled = true;

// propagation also not need
$this->stopPropagation();

return $this;
}
}
}
10 changes: 10 additions & 0 deletions src/Event/ConstructAfterEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Sokil\Mongo\Event;

use Sokil\Mongo\Event;

class ConstructAfterEvent extends Event
{
const NAME = 'afterConstruct';
}
10 changes: 10 additions & 0 deletions src/Event/DeleteAfterEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Sokil\Mongo\Event;

use Sokil\Mongo\Event;

class DeleteAfterEvent extends Event
{
const NAME = 'afterDelete';
}
10 changes: 10 additions & 0 deletions src/Event/DeleteBeforeEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Sokil\Mongo\Event;

use Sokil\Mongo\Event;

class DeleteBeforeEvent extends Event
{
const NAME = 'beforeDelete';
}
10 changes: 10 additions & 0 deletions src/Event/InsertAfterEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Sokil\Mongo\Event;

use Sokil\Mongo\Event;

class InsertAfterEvent extends Event
{
const NAME = 'afterInsert';
}
10 changes: 10 additions & 0 deletions src/Event/InsertBeforeEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Sokil\Mongo\Event;

use Sokil\Mongo\Event;

class InsertBeforeEvent extends Event
{
const NAME = 'beforeInsert';
}
10 changes: 10 additions & 0 deletions src/Event/SaveAfterEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Sokil\Mongo\Event;

use Sokil\Mongo\Event;

class SaveAfterEvent extends Event
{
const NAME = 'afterSave';
}
10 changes: 10 additions & 0 deletions src/Event/SaveBeforeEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Sokil\Mongo\Event;

use Sokil\Mongo\Event;

class SaveBeforeEvent extends Event
{
const NAME = 'beforeSave';
}
Loading