Skip to content

Commit

Permalink
[5.x] Pass event instance to event listeners tag() method (#1361)
Browse files Browse the repository at this point in the history
* first pass

* Store the event as a property? seems dirty...

* Possibly better implementation

* Remove unneeded parameters

* formatting

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
mateusjatenee and taylorotwell authored Dec 29, 2023
1 parent 9971f41 commit 87a36e9
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 4 deletions.
42 changes: 38 additions & 4 deletions src/Tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@

class Tags
{
/**
* The event that was last handled.
*
* @var object|null
*/
protected static $event;

/**
* Determine the tags for the given job.
*
Expand Down Expand Up @@ -52,23 +59,29 @@ public static function extractExplicitTags($job)
*/
protected static function tagsForListener($job)
{
$event = static::extractEvent($job);

static::setEvent($event);

return collect(
[static::extractListener($job), static::extractEvent($job)]
[static::extractListener($job), $event]
)->map(function ($job) {
return static::for($job);
})->collapse()->unique()->toArray();
})->collapse()->unique()->tap(function () {
static::flushEventState();
})->toArray();
}

/**
* Determine tags for the given job.
*
* @param array $jobs
* @return mixed
* @return array
*/
protected static function explicitTags(array $jobs)
{
return collect($jobs)->map(function ($job) {
return method_exists($job, 'tags') ? $job->tags() : [];
return method_exists($job, 'tags') ? $job->tags(static::$event) : [];
})->collapse()->unique()->all();
}

Expand Down Expand Up @@ -162,4 +175,25 @@ protected static function extractEvent($job)
? $job->data[0]
: new stdClass;
}

/**
* Set the event currently being handled.
*
* @param object $event
* @return void
*/
protected static function setEvent($event)
{
static::$event = $event;
}

/**
* Flush the event currently being handled.
*
* @return void
*/
protected static function flushEventState()
{
static::$event = null;
}
}
14 changes: 14 additions & 0 deletions tests/Feature/Fixtures/FakeListenerWithDynamicTags.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Laravel\Horizon\Tests\Feature\Fixtures;

class FakeListenerWithDynamicTags
{
public function tags(FakeEvent $event)
{
return [
'listenerTag1',
get_class($event),
];
}
}
14 changes: 14 additions & 0 deletions tests/Feature/RedisPayloadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Laravel\Horizon\Tests\Feature\Fixtures\FakeJobWithTagsMethod;
use Laravel\Horizon\Tests\Feature\Fixtures\FakeListener;
use Laravel\Horizon\Tests\Feature\Fixtures\FakeListenerSilenced;
use Laravel\Horizon\Tests\Feature\Fixtures\FakeListenerWithDynamicTags;
use Laravel\Horizon\Tests\Feature\Fixtures\FakeListenerWithProperties;
use Laravel\Horizon\Tests\Feature\Fixtures\FakeListenerWithTypedProperties;
use Laravel\Horizon\Tests\Feature\Fixtures\FakeModel;
Expand Down Expand Up @@ -100,6 +101,19 @@ public function test_tags_are_correctly_extracted_for_listeners()
], $JobPayload->decoded['tags']);
}

public function test_tags_are_correctly_extracted_for_listeners_with_dynamic_event_information()
{
$JobPayload = new JobPayload(json_encode(['id' => 1]));

$job = new CallQueuedListener(FakeListenerWithDynamicTags::class, 'handle', [new FakeEvent()]);

$JobPayload->prepare($job);

$this->assertEquals([
'listenerTag1', FakeEvent::class, 'eventTag1', 'eventTag2',
], $JobPayload->decoded['tags']);
}

public function test_tags_are_correctly_determined_for_listeners()
{
$JobPayload = new JobPayload(json_encode(['id' => 1]));
Expand Down

0 comments on commit 87a36e9

Please sign in to comment.