Skip to content

[5.x] Pass event instance to event listeners tag() method #1361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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