From ac78935f3a2e0d92064bba00f1a3b1d646d18e1c Mon Sep 17 00:00:00 2001 From: Petra Barus Date: Fri, 12 Jun 2015 12:13:35 +0700 Subject: [PATCH 1/2] Added DeferredEventTrait. --- src/Behaviors/DeferredEventTrait.php | 96 ++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 src/Behaviors/DeferredEventTrait.php diff --git a/src/Behaviors/DeferredEventTrait.php b/src/Behaviors/DeferredEventTrait.php new file mode 100644 index 0000000..81f808f --- /dev/null +++ b/src/Behaviors/DeferredEventTrait.php @@ -0,0 +1,96 @@ + + * @since 2015.06.12 + */ + +namespace UrbanIndo\Yii2\Queue\Behaviors; + +/** + * ActiveRecordDeferredEventBehavior is deferred event function for active record. + * + * Due to SuperClosure limitation to serialize classes like PDO, this will + * only pass the class, primary key. + * + * @author Petra Barus + * @since 2015.06.12 + */ +trait DeferredEventTrait { + + /** + * @return \UrbanIndo\Yii2\Queue\Queue + */ + public function getQueue() { + return \Yii::$app->queue; + } + + + /** + * Defer event + * + * To use this, attach the behavior and call + * + * $model->deferAction(function($model) { + * $model->doSomething(); + * }); + * @param callable $callback + */ + public function deferAction($callback) { + if ($this instanceof ActiveRecord) { + $job = $this->deferActiveRecordAction($callback); + } else if ($this instanceof \yii\base\Model) { + $job = $this->deferModelAction($callback); + } else { + $job = $this->deferObjectAction($callback); + } + $queue = $this->getQueue(); + $queue->post($job); + } + + private function serializeCallback($callback) { + $serializer = new \SuperClosure\Serializer(); + $serialized = $serializer->serialize($callback); + return [$serializer, $serialized]; + } + + private function deferActiveRecordAction($callback) { + $class = get_class($this); + $pk = $this->getPrimaryKey(); + list($serializer, $serialized) = $this->serializeCallback($callback); + return new \UrbanIndo\Yii2\Queue\Job([ + 'route' => function() use ($class, $pk, $serialized, $serializer) { + $model = $class::findOne($pk); + $unserialized = $serializer->unserialize($serialized); + call_user_func($unserialized, $model); + } + ]); + } + + private function deferModelAction($callback) { + $class = get_class($this); + $attributes = $this->getAttributes(); + list($serializer, $serialized) = $this->serializeCallback($callback); + return new \UrbanIndo\Yii2\Queue\Job([ + 'route' => function() use ($class, $attributes, $serialized, $serializer) { + $model = new $class; + $model->setAttributes($attributes, false); + $unserialized = $serializer->unserialize($serialized); + call_user_func($unserialized, $model); + } + ]); + } + + private function deferObject($callback) { + $object = $this; + list($serializer, $serialized) = $this->serializeCallback($callback); + return new \UrbanIndo\Yii2\Queue\Job([ + 'route' => function() use ($object, $serialized, $serializer) { + $unserialized = $serializer->unserialize($serialized); + call_user_func($unserialized, $object); + } + ]); + } + +} From 2e77143ca1fc10383400c6d81c40894ed056a67b Mon Sep 17 00:00:00 2001 From: Petra Barus Date: Fri, 12 Jun 2015 12:14:30 +0700 Subject: [PATCH 2/2] Added changelog. --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 469deaa..bbbc591 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ # Change Log All notable changes to this project will be documented in this file. +## 1.2.1 +- Added `DeferredEventTrait` + ## 1.2.0 - Added tests - Added `MemoryQueue`, `DeferredEventHandler`, and `ActiveRecordDeferredEventHandler`.