-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from urbanindo/add-routing-behavior
Add routing behavior
- Loading branch information
Showing
8 changed files
with
330 additions
and
178 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/Behaviors/ActiveRecordDeferredEventRoutingBehavior.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
/** | ||
* ActiveRecordDeferredRoutingBehavior extends | ||
* @author Petra Barus <[email protected]> | ||
* @since 2015.02.25 | ||
*/ | ||
|
||
namespace UrbanIndo\Yii2\Queue\Behaviors; | ||
|
||
use yii\db\ActiveRecord; | ||
|
||
/** | ||
* ActiveRecordDeferredRoutingBehavior provides matching between controller in | ||
* task worker with the appropriate event. | ||
* | ||
* @property-read ActiveRecord $owner the owner. | ||
* | ||
* @author Petra Barus <[email protected]> | ||
* @since 2015.02.25 | ||
*/ | ||
class ActiveRecordDeferredEventRoutingBehavior extends DeferredEventRoutingBehavior { | ||
|
||
/** | ||
* The attribute name. | ||
* @var type | ||
*/ | ||
public $pkAttribute = 'id'; | ||
|
||
/** | ||
* Whether to add the primary key to the data. | ||
* @var boolean | ||
*/ | ||
public $addPkToData = true; | ||
|
||
public function routeEvent($event) { | ||
/* @var $owner ActiveRecord */ | ||
|
||
$eventName = $event->name; | ||
$handler = $this->events[$eventName]; | ||
if (is_callable($handler)) { | ||
$handler = call_user_func($handler, $this->owner); | ||
} else if ($this->addPkToData) { | ||
$pk = $this->owner->getPrimaryKey(); | ||
if (is_array($pk)) { | ||
$handler = array_merge($handler, $pk); | ||
} else { | ||
$handler[$this->pkAttribute] = $pk; | ||
} | ||
} | ||
$route = $handler[0]; | ||
unset($handler[0]); | ||
$handler['scenario'] = $this->owner->getScenario(); | ||
$data = $handler; | ||
$this->queue->post(new \UrbanIndo\Yii2\Queue\Job([ | ||
'route' => $route, | ||
'data' => $data | ||
])); | ||
} | ||
} |
Oops, something went wrong.