Skip to content

Instantly share code, notes, and snippets.

@TimothyLoyer
Created October 2, 2014 16:03
Show Gist options
  • Select an option

  • Save TimothyLoyer/09cc944c7d01f9cc9886 to your computer and use it in GitHub Desktop.

Select an option

Save TimothyLoyer/09cc944c7d01f9cc9886 to your computer and use it in GitHub Desktop.
QueueInterface Depency Injection fails with "BindingResolutionException"
<?php namespace Acme\V1\Handlers;
use Acme\V1\Eventing\EventListener;
use Acme\V1\Eventing\Events\AssetHasBeenUploaded;
use Acme\V1\Eventing\Events\ProductionHasBeenUploaded;
use Illuminate\Queue\QueueInterface as Queue;
class EncodeListener extends EventListener {
protected $queue;
/**
* @param Queue $queue
*/
function __construct( Queue $queue )
{
$this->queue = $queue;
}
/**
* @param AssetHasBeenUploaded $upload
*/
public function whenAssetHasBeenUploaded( AssetHasBeenUploaded $upload )
{
$assetId = $upload->asset->id;
$resolution = 480;
$this->queue->push('Acme\V1\Job\EncodeAsset', compact('assetId', 'resolution'));
}
/**
* @param ProductionHasBeenUploaded $upload
*/
public function whenProductionHasBeenUploaded( ProductionHasBeenUploaded $upload )
{
$assetId = $upload->asset->id;
$resolution = 720;
$this->queue->push('Acme\V1\Job\EncodeAsset', compact('assetId', 'resolution'));
}
}
<?php namespace Acme\V1\Eventing;
use ReflectionClass;
class EventListener {
/**
* @param $event
* @return mixed
*/
public function handle( $event )
{
$eventName = $this->getEventName($event);
if ( $this->listenerIsRegistered($eventName) )
{
return call_user_func([$this, 'when' . $eventName], $event);
}
}
/**
* @param $event
* @return string
*/
private function getEventName( $event )
{
$eventName = (new ReflectionClass($event))->getShortName();
return $eventName;
}
/**
* @param $eventName
* @return bool
*/
private function listenerIsRegistered( $eventName )
{
$method = "when{$eventName}";
return method_exists($this, $method);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment