From e79662b625f7cd2a994af69b3318d4508220d7f3 Mon Sep 17 00:00:00 2001 From: Petra Barus Date: Mon, 1 Jun 2015 14:51:51 +0700 Subject: [PATCH] Not send the attributes of active record to the queue if event is not delete. --- .../ActiveRecordDeferredEventBehavior.php | 68 ++++++++++++------- .../ActiveRecordDeferredEventHandler.php | 22 +++--- src/Queue.php | 67 +++++++++++++++--- 3 files changed, 117 insertions(+), 40 deletions(-) diff --git a/src/Behaviors/ActiveRecordDeferredEventBehavior.php b/src/Behaviors/ActiveRecordDeferredEventBehavior.php index cccbbb1..313419e 100644 --- a/src/Behaviors/ActiveRecordDeferredEventBehavior.php +++ b/src/Behaviors/ActiveRecordDeferredEventBehavior.php @@ -55,8 +55,6 @@ public static function getDefaultEvents() { */ public function postDeferredEvent($event) { $class = get_class($this->owner); - $pk = $this->owner->getPrimaryKey(); - $attributes = $this->owner->getAttributes(); $eventName = $event->name; $handlers = ($this->_hasEventHandlers) ? $this->events : false; if (isset($this->_serializer)) { @@ -64,39 +62,63 @@ public function postDeferredEvent($event) { } else { $serializer = null; } - $this->queue->post(new \UrbanIndo\Yii2\Queue\Job([ - 'route' => function() use ($class, $pk, $attributes, $handlers, $eventName, $serializer) { - if ($eventName == ActiveRecord::EVENT_AFTER_DELETE) { + + if ($eventName == ActiveRecord::EVENT_AFTER_DELETE) { + $attributes = $this->owner->getAttributes(); + $this->queue->post(new \UrbanIndo\Yii2\Queue\Job([ + 'route' => function() use ($class, $attributes, $handlers, $eventName, $serializer) { $object = \Yii::createObject($class); /* @var $object ActiveRecord */ $object->setAttributes($attributes, false); - } else { + if ($handlers) { + $handler = $handlers[$eventName]; + if ($serializer !== null) { + try { + $unserialized = $serializer->unserialize($handler); + $unserialized($object); + } catch (Exception $exc) { + return call_user_method($handler, $object); + } + } else { + return call_user_method($handler, $object); + } + } else if ($object instanceof DeferredEventInterface) { + /* @var $object DeferredEventInterface */ + return $object->handleDeferredEvent($eventName); + } else { + throw new Exception("Model is not instance of DeferredEventInterface"); + } + } + ])); + } else { + $pk = $this->owner->getPrimaryKey(); + $this->queue->post(new \UrbanIndo\Yii2\Queue\Job([ + 'route' => function() use ($class, $pk, $handlers, $eventName, $serializer) { $object = $class::findOne($pk); if ($object === null) { throw new Exception("Model is not found"); } - } - - if ($handlers) { - $handler = $handlers[$eventName]; - if ($serializer !== null) { - try { - $unserialized = $serializer->unserialize($handler); - $unserialized($object); - } catch (Exception $exc) { + if ($handlers) { + $handler = $handlers[$eventName]; + if ($serializer !== null) { + try { + $unserialized = $serializer->unserialize($handler); + $unserialized($object); + } catch (Exception $exc) { + return call_user_method($handler, $object); + } + } else { return call_user_method($handler, $object); } + } else if ($object instanceof DeferredEventInterface) { + /* @var $object DeferredEventInterface */ + return $object->handleDeferredEvent($eventName); } else { - return call_user_method($handler, $object); + throw new Exception("Model is not instance of DeferredEventInterface"); } - } else if ($object instanceof DeferredEventInterface) { - /* @var $object DeferredEventInterface */ - return $object->handleDeferredEvent($eventName); - } else { - throw new Exception("Model is not instance of DeferredEventInterface"); } - } - ])); + ])); + } } } diff --git a/src/Behaviors/ActiveRecordDeferredEventHandler.php b/src/Behaviors/ActiveRecordDeferredEventHandler.php index 8964432..0b144fe 100644 --- a/src/Behaviors/ActiveRecordDeferredEventHandler.php +++ b/src/Behaviors/ActiveRecordDeferredEventHandler.php @@ -29,26 +29,32 @@ public function deferEvent($event) { $pk = $this->owner->getPrimaryKey(); $attributes = $this->owner->getAttributes(); $eventName = $event->name; - $queue = $this->queue; $handler = clone $this; $handler->queue = null; $handler->owner = null; /* @var $queue Queue */ - $queue->post(new \UrbanIndo\Yii2\Queue\Job([ - 'route' => function() use ($class, $pk, $attributes, $handler, $eventName) { - if ($eventName == ActiveRecord::EVENT_AFTER_DELETE) { + if ($eventName == ActiveRecord::EVENT_AFTER_DELETE) { + $queue->post(new \UrbanIndo\Yii2\Queue\Job([ + 'route' => function() use ($class, $pk, $attributes, $handler, $eventName) { $object = \Yii::createObject($class); /* @var $object ActiveRecord */ $object->setAttributes($attributes, false); - } else { + $handler->handleEvent($object); + } + ])); + } else { + $queue->post(new \UrbanIndo\Yii2\Queue\Job([ + 'route' => function() use ($class, $pk, $attributes, $handler, $eventName) { $object = $class::findOne($pk); if ($object === null) { throw new Exception("Model is not found"); } + /* @var $object ActiveRecord */ + $handler->handleEvent($object); } - $handler->handleEvent($object); - } - ])); + ])); + } + } } diff --git a/src/Queue.php b/src/Queue.php index e644a68..a381932 100644 --- a/src/Queue.php +++ b/src/Queue.php @@ -38,6 +38,16 @@ abstract class Queue extends \yii\base\Component { * @var \yii\base\Module */ public $module; + + const SERIALIZER_JSON = 'json'; + + const SERIALIZER_PHP = 'php'; + + /** + * Choose the serializer. + * @var string + */ + public $serializer = 'json'; /** * Initializes the module. @@ -90,30 +100,50 @@ public abstract function delete(Job $job); /** * Deserialize job to be executed. * - * @param string $json the json string + * @param string $message the json string * @return \UrbanIndo\Yii2\Queue\Job the job * @throws \yii\base\Exception if there is no route detected. */ - protected function deserialize($json) { - $message = \yii\helpers\Json::decode($json); - if (!isset($message['route'])) { + protected function deserialize($message) { + $job = $this->deserializeMessage($message); + if (!isset($job['route'])) { throw new \yii\base\Exception('No route detected'); } - $route = $message['route']; - if (isset($message['type']) && $message['type'] == Job::TYPE_CALLABLE) { + $route = $job['route']; + if (isset($job['type']) && $job['type'] == Job::TYPE_CALLABLE) { $type = Job::TYPE_CALLABLE; $serializer = new \SuperClosure\Serializer(); $route = $serializer->unserialize($route); } else { $type = Job::TYPE_REGULAR; } - $data = \yii\helpers\ArrayHelper::getValue($message, 'data', []); + $data = \yii\helpers\ArrayHelper::getValue($job, 'data', []); return new Job([ 'route' => $route, 'data' => $data, ]); } - + + /** + * + * @param type $array + * @return type + */ + protected function deserializeMessage($array) { + switch($this->serializer) { + case self::SERIALIZER_PHP: + $data = unserialize($array); + break; + case self::SERIALIZER_JSON: + $data = \yii\helpers\Json::decode($array); + break; + } + if (empty($data)) { + throw new Exception('Can not deserialize message'); + } + return $data; + } + /** * Pack job so that it can be send. * @@ -131,7 +161,26 @@ protected function serialize(Job $job) { $return['route'] = $job->route; } $return['data'] = $job->data; - return \yii\helpers\Json::encode($return); + return $this->serializeMessage($return); } + /** + * + * @param type $array + * @return type + */ + protected function serializeMessage($array) { + switch($this->serializer) { + case self::SERIALIZER_PHP: + $data = serialize($array); + break; + case self::SERIALIZER_JSON: + $data = \yii\helpers\Json::encode($array); + break; + } + if (empty($data)) { + throw new Exception('Can not deserialize message'); + } + return $data; + } }