Skip to content

Commit 10d0ea6

Browse files
committed
introduce OnQueue and OnConnection
1 parent 2e5054a commit 10d0ea6

File tree

4 files changed

+167
-0
lines changed

4 files changed

+167
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Illuminate\Foundation\Bus\Attributes;
4+
5+
use Attribute;
6+
7+
#[Attribute(Attribute::TARGET_CLASS)]
8+
class OnConnection
9+
{
10+
/**
11+
* @param string|\BackedEnum $connection
12+
* @return void
13+
*/
14+
public function __construct(public $connection)
15+
{
16+
}
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Illuminate\Foundation\Bus\Attributes;
4+
5+
use Attribute;
6+
7+
#[Attribute(Attribute::TARGET_CLASS)]
8+
class OnQueue
9+
{
10+
/**
11+
* @param string|\BackedEnum $queue
12+
* @return void
13+
*/
14+
public function __construct(public $queue)
15+
{
16+
17+
}
18+
}

src/Illuminate/Foundation/Bus/PendingDispatch.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use Illuminate\Contracts\Bus\Dispatcher;
88
use Illuminate\Contracts\Cache\Repository as Cache;
99
use Illuminate\Contracts\Queue\ShouldBeUnique;
10+
use Illuminate\Foundation\Bus\Attributes\OnConnection;
11+
use Illuminate\Foundation\Bus\Attributes\OnQueue;
1012

1113
class PendingDispatch
1214
{
@@ -33,6 +35,31 @@ class PendingDispatch
3335
public function __construct($job)
3436
{
3537
$this->job = $job;
38+
39+
$this->setQueueAndConnectionFromAttributesIfNotSet();
40+
}
41+
42+
protected function setQueueAndConnectionFromAttributesIfNotSet(): void
43+
{
44+
if (($hasQueueSet = isset($this->job->queue))
45+
&& ($hasConnectionSet = isset($this->job->connection))) {
46+
return;
47+
}
48+
49+
$reflectionClass = new \ReflectionClass($this->job);
50+
if (!$hasQueueSet) {
51+
$onQueue = $reflectionClass->getAttributes(OnQueue::class);
52+
if ($onQueue !== []) {
53+
$this->onQueue($onQueue[0]->newInstance()->queue);
54+
}
55+
}
56+
57+
if (!$hasConnectionSet) {
58+
$onConnection = $reflectionClass->getAttributes(OnConnection::class);
59+
if ($onConnection !== []) {
60+
$this->onConnection($onConnection[0]->newInstance()->connection);
61+
}
62+
}
3663
}
3764

3865
/**
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Bus;
4+
5+
use Illuminate\Bus\Queueable;
6+
use Illuminate\Contracts\Queue\ShouldQueue;
7+
use Illuminate\Foundation\Bus\Attributes\OnConnection;
8+
use Illuminate\Foundation\Bus\Attributes\OnQueue;
9+
use Illuminate\Foundation\Bus\Dispatchable;
10+
use Illuminate\Queue\InteractsWithQueue;
11+
use Illuminate\Support\Facades\Queue;
12+
use Orchestra\Testbench\TestCase;
13+
14+
class BusPendingDispatchWithAttributesTest extends TestCase
15+
{
16+
public function testDispatchWhereQueueAndConnectionAreFromAttributes(): void
17+
{
18+
Queue::fake();
19+
FakeJobWithOnQueueAndOnConnection::dispatch(123);
20+
21+
Queue::assertPushed(function (FakeJobWithOnQueueAndOnConnection $job) {
22+
return $job->connection === "connection_from_attribute"
23+
&& $job->queue === "queue-from-attribute"
24+
&& $job->value === 123;
25+
});
26+
}
27+
28+
public function testDispatchWhereQueueIsFromAttribute(): void
29+
{
30+
Queue::fake();
31+
32+
FakeJobWithOnQueueFromAttribute::dispatch(1234)->onConnection("not-from-attribute");
33+
34+
Queue::assertPushed(function (FakeJobWithOnQueueFromAttribute $job) {
35+
return $job->connection === "not-from-attribute"
36+
&& $job->queue === "queue-from-attribute"
37+
&& $job->value === 1234;
38+
});
39+
}
40+
41+
public function testDispatchWhereConnectionIsFromAttribute(): void
42+
{
43+
Queue::fake();
44+
45+
FakeJobWithOnConnection::dispatch(999);
46+
47+
Queue::assertPushed(function (FakeJobWithOnConnection $job) {
48+
return $job->connection === "connection_from_attribute"
49+
&& !isset($job->queue)
50+
&& $job->value === 999;
51+
});
52+
}
53+
54+
public function testOverridingQueueAndConnectionDoesNotUseAttributeValues(): void
55+
{
56+
Queue::fake();
57+
58+
FakeJobWithOnQueueAndOnConnection::dispatch('abc')
59+
->onQueue('setViaMethod')
60+
->onConnection('setViaMethodToo');
61+
62+
Queue::assertPushed(function (FakeJobWithOnQueueAndOnConnection $job) {
63+
return $job->queue === "setViaMethod"
64+
&& $job->connection === "setViaMethodToo"
65+
&& $job->value === 'abc';
66+
});
67+
}
68+
}
69+
70+
#[OnQueue('queue-from-attribute')]
71+
#[OnConnection('connection_from_attribute')]
72+
class FakeJobWithOnQueueAndOnConnection implements ShouldQueue
73+
{
74+
use Dispatchable;
75+
use Queueable;
76+
use InteractsWithQueue;
77+
78+
public function __construct(public $value)
79+
{
80+
}
81+
}
82+
83+
#[OnQueue('queue-from-attribute')]
84+
class FakeJobWithOnQueueFromAttribute implements ShouldQueue
85+
{
86+
use Dispatchable;
87+
use Queueable;
88+
use InteractsWithQueue;
89+
90+
public function __construct(public $value)
91+
{
92+
}
93+
}
94+
95+
#[OnConnection('connection_from_attribute')]
96+
class FakeJobWithOnConnection implements ShouldQueue
97+
{
98+
use Dispatchable;
99+
use Queueable;
100+
use InteractsWithQueue;
101+
102+
public function __construct(public $value)
103+
{
104+
}
105+
}

0 commit comments

Comments
 (0)