Skip to content

Commit

Permalink
Merge pull request #4 from urbanindo/add-deferred-event-trait
Browse files Browse the repository at this point in the history
Add deferred event trait
  • Loading branch information
petrabarus committed Jun 12, 2015
2 parents e79662b + 2e77143 commit 02971d4
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`.
Expand Down
96 changes: 96 additions & 0 deletions src/Behaviors/DeferredEventTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

/**
* ActiveRecordDeferredEventTrait trait
* @author Petra Barus <petra.barus@gmail.com>
* @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 <petra.barus@gmail.com>
* @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);
}
]);
}

}

0 comments on commit 02971d4

Please sign in to comment.