diff --git a/composer.json b/composer.json index 81bd469..13f7baa 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/src/Document.php b/src/Document.php index 7d2793d..3acc85e 100644 --- a/src/Document.php +++ b/src/Document.php @@ -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. @@ -136,7 +148,7 @@ public function __construct( } // execute after construct event handlers - $this->triggerEvent('afterConstruct'); + $this->triggerEvent('afterConstruct', new ConstructAfterEvent()); } public function getOptions() @@ -211,7 +223,7 @@ private function initDelegates() { // start event dispatching $this->eventDispatcher = new EventDispatcher; - + // create operator $this->operator = $this->getCollection()->operator(); @@ -256,7 +268,7 @@ public function __call($name, $arguments) array($this->eventDispatcher, 'addListener'), $addListenerArguments ); - + return $this; } @@ -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); } @@ -646,7 +662,7 @@ 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; } @@ -654,12 +670,12 @@ public function validate() $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; } @@ -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; } @@ -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()); } /** @@ -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; } @@ -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()); } /** @@ -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; } @@ -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; } @@ -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; } @@ -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); diff --git a/src/Event.php b/src/Event.php index 1697f82..b566a08 100644 --- a/src/Event.php +++ b/src/Event.php @@ -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; + } } } diff --git a/src/Event/ConstructAfterEvent.php b/src/Event/ConstructAfterEvent.php new file mode 100644 index 0000000..75e062a --- /dev/null +++ b/src/Event/ConstructAfterEvent.php @@ -0,0 +1,10 @@ +