File tree Expand file tree Collapse file tree 2 files changed +41
-1
lines changed Expand file tree Collapse file tree 2 files changed +41
-1
lines changed Original file line number Diff line number Diff line change @@ -22,6 +22,13 @@ class CallQueuedClosure implements ShouldQueue
2222 */
2323 public $ closure ;
2424
25+ /**
26+ * The name assigned to the job.
27+ *
28+ * @var string|null
29+ */
30+ public $ name = null ;
31+
2532 /**
2633 * The callbacks that should be executed on failure.
2734 *
@@ -105,6 +112,21 @@ public function displayName()
105112 {
106113 $ reflection = new ReflectionFunction ($ this ->closure ->getClosure ());
107114
108- return 'Closure ( ' .basename ($ reflection ->getFileName ()).': ' .$ reflection ->getStartLine ().') ' ;
115+ $ prefix = is_null ($ this ->name ) ? '' : "{$ this ->name } - " ;
116+
117+ return $ prefix .'Closure ( ' .basename ($ reflection ->getFileName ()).': ' .$ reflection ->getStartLine ().') ' ;
118+ }
119+
120+ /**
121+ * Assign a name to the job.
122+ *
123+ * @param string $name
124+ * @return $this
125+ */
126+ public function name ($ name )
127+ {
128+ $ this ->name = $ name ;
129+
130+ return $ this ;
109131 }
110132}
Original file line number Diff line number Diff line change @@ -166,6 +166,24 @@ public function testQueueMayBeNullForJobQueueingAndJobQueuedEvent()
166166 $ this ->assertNull ($ events [3 ]->queue );
167167 }
168168
169+ public function testQueuedClosureCanBeNamed ()
170+ {
171+ Config::set ('queue.default ' , 'database ' );
172+ $ events = [];
173+ $ this ->app ['events ' ]->listen (function (JobQueued $ e ) use (&$ events ) {
174+ $ events [] = $ e ;
175+ });
176+
177+ dispatch (function () {
178+ //
179+ })->name ('custom name ' );
180+
181+ $ this ->assertCount (1 , $ events );
182+ $ this ->assertInstanceOf (JobQueued::class, $ events [0 ]);
183+ $ this ->assertSame ('custom name ' , $ events [0 ]->job ->name );
184+ $ this ->assertStringContainsString ('custom name ' , $ events [0 ]->job ->displayName ());
185+ }
186+
169187 public function testCanDisableDispatchingAfterResponse ()
170188 {
171189 Job::dispatchAfterResponse ('test ' );
You can’t perform that action at this time.
0 commit comments