Skip to content

Instantly share code, notes, and snippets.

@acorncom
Last active August 29, 2015 14:20
Show Gist options
  • Save acorncom/a7db61d89447b86b0cbd to your computer and use it in GitHub Desktop.
Save acorncom/a7db61d89447b86b0cbd to your computer and use it in GitHub Desktop.
Prototype of using Laravel Illuminate/queue inside of a Yii2 app
{
"require": {
"php": ">=5.4.0",
"yiisoft/yii2": "*",
"yiisoft/yii2-swiftmailer": "*",
"yiisoft/yii2-redis": "*",
"illuminate/queue": "~5.0",
"illuminate/encryption": "~5.0", // used to prototype, if we had an "Encrypter" class that implemented the Encrypter contract we could eliminate this dependency
"jeremeamia/superclosure": "~2.0" // used to prototype pushing anonymous functions into a queue. Required in Laravel if we want to push closures
}
}
<?php
/* Note that the class and methods are prototype code, not copy/paste code
*
* However, the actual code in the action has been tested with a Yii2 app
*
*/
class QueueComponent extends Component {
public function actionTest() {
$sync = new \Illuminate\Queue\SyncQueue;
$container = new \Illuminate\Container\Container;
$encrypter = new \Illuminate\Encryption\Encrypter(str_random(32)); // a great place to use the Yii2 equivalent wrapped and implementing the necessary contract
$container->instance('Illuminate\Contracts\Encryption\Encrypter', $encrypter);
// would it be possible to use the Yii2 DI container? Might be if we get changes made upstream, but currently
// Container is a hard requirement within illuminate\queue (not just a Container contract)
$sync->setContainer($container);
$sync->setEncrypter($encrypter);
$container->singleton('IlluminateQueueClosure', function($container) use($encrypter)
{
return new \IlluminateQueueClosure($encrypter);
});
$sync->push(function($model) {
print "made it here 2";
return Yii::$app->mailer->compose()
->setTo('[email protected]')
->setFrom(['[email protected]'])
->setSubject("test")
->setTextBody("test body")
->send();
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment