diff --git a/src/Tags.php b/src/Tags.php index dc83d99a..a1807c4a 100644 --- a/src/Tags.php +++ b/src/Tags.php @@ -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. * @@ -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(); } @@ -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; + } } diff --git a/tests/Feature/Fixtures/FakeListenerWithDynamicTags.php b/tests/Feature/Fixtures/FakeListenerWithDynamicTags.php new file mode 100644 index 00000000..172130d8 --- /dev/null +++ b/tests/Feature/Fixtures/FakeListenerWithDynamicTags.php @@ -0,0 +1,14 @@ +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]));