Skip to content

Commit d397e57

Browse files
committed
wip
1 parent 326a7b6 commit d397e57

File tree

7 files changed

+149
-27
lines changed

7 files changed

+149
-27
lines changed

src/Illuminate/Foundation/Bus/PendingDispatch.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
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;
10+
use Illuminate\Foundation\Queue\InteractsWithQueueAndConnection;
1211
use Illuminate\Foundation\Queue\InteractsWithUniqueJobs;
1312

1413
class PendingDispatch
1514
{
15+
use InteractsWithQueueAndConnection;
1616
use InteractsWithUniqueJobs;
1717

1818
/**
@@ -209,16 +209,14 @@ protected function setQueueAndConnectionFromAttributesIfNotSet(): void
209209

210210
$reflectionClass = new \ReflectionClass($this->job);
211211
if (! $hasQueueSet) {
212-
$onQueue = $reflectionClass->getAttributes(OnQueue::class);
213-
if ($onQueue !== []) {
214-
$this->onQueue($onQueue[0]->newInstance()->queue);
212+
if ($queue = $this->getQueueFromOnConnectionAttribute($reflectionClass)) {
213+
$this->onQueue($queue);
215214
}
216215
}
217216

218217
if (! $hasConnectionSet) {
219-
$onConnection = $reflectionClass->getAttributes(OnConnection::class);
220-
if ($onConnection !== []) {
221-
$this->onConnection($onConnection[0]->newInstance()->connection);
218+
if ($connection = $this->getConnectionFromOnConnectionAttribute($reflectionClass)) {
219+
$this->onConnection($connection);
222220
}
223221
}
224222
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Illuminate\Foundation\Queue;
4+
5+
use ReflectionClass;
6+
7+
use function Illuminate\Support\enum_value;
8+
9+
trait InteractsWithQueueAndConnection
10+
{
11+
/**
12+
* Extract the connection from OnConnection attribute if present.
13+
*
14+
* @param \ReflectionClass $reflectionClass
15+
* @return string|\UnitEnum|null
16+
*/
17+
protected function getConnectionFromOnConnectionAttribute(ReflectionClass $reflectionClass)
18+
{
19+
$onConnection = $reflectionClass->getAttributes(OnConnection::class);
20+
if ($onConnection === []) {
21+
return null;
22+
}
23+
24+
return enum_value($onConnection[0]->newInstance()->connection);
25+
}
26+
27+
/**
28+
* Extract the queue from OnQueue attribute if present.
29+
*
30+
* @param \ReflectionClass $reflectionClass
31+
* @return string|\UnitEnum|null
32+
*/
33+
protected function getQueueFromOnConnectionAttribute(ReflectionClass $reflectionClass)
34+
{
35+
$onQueue = $reflectionClass->getAttributes(OnQueue::class);
36+
if ($onQueue === []) {
37+
return null;
38+
}
39+
40+
return enum_value($onQueue[0]->newInstance()->queue);
41+
}
42+
}

src/Illuminate/Foundation/Bus/Attributes/OnConnection.php renamed to src/Illuminate/Foundation/Queue/OnConnection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Illuminate\Foundation\Bus\Attributes;
3+
namespace Illuminate\Foundation\Queue;
44

55
use Attribute;
66

src/Illuminate/Foundation/Bus/Attributes/OnQueue.php renamed to src/Illuminate/Foundation/Queue/OnQueue.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Illuminate\Foundation\Bus\Attributes;
3+
namespace Illuminate\Foundation\Queue;
44

55
use Attribute;
66

src/Illuminate/Mail/Mailable.php

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Illuminate\Contracts\Support\Htmlable;
1313
use Illuminate\Contracts\Support\Renderable;
1414
use Illuminate\Contracts\Translation\HasLocalePreference;
15+
use Illuminate\Foundation\Queue\InteractsWithQueueAndConnection;
1516
use Illuminate\Support\Collection;
1617
use Illuminate\Support\HtmlString;
1718
use Illuminate\Support\Str;
@@ -30,7 +31,7 @@
3031

3132
class Mailable implements MailableContract, Renderable
3233
{
33-
use Conditionable, ForwardsCalls, Localizable, Tappable, Macroable {
34+
use Conditionable, ForwardsCalls, Localizable, Tappable, InteractsWithQueueAndConnection, Macroable {
3435
__call as macroCall;
3536
}
3637

@@ -227,12 +228,8 @@ public function queue(Queue $queue)
227228
return $this->later($this->delay, $queue);
228229
}
229230

230-
$connection = property_exists($this, 'connection') ? $this->connection : null;
231-
232-
$queueName = property_exists($this, 'queue') ? $this->queue : null;
233-
234-
return $queue->connection($connection)->pushOn(
235-
$queueName ?: null, $this->newQueuedJob()
231+
return $queue->connection($this->getConnection())->pushOn(
232+
$this->getQueue(), $this->newQueuedJob()
236233
);
237234
}
238235

@@ -245,12 +242,8 @@ public function queue(Queue $queue)
245242
*/
246243
public function later($delay, Queue $queue)
247244
{
248-
$connection = property_exists($this, 'connection') ? $this->connection : null;
249-
250-
$queueName = property_exists($this, 'queue') ? $this->queue : null;
251-
252-
return $queue->connection($connection)->laterOn(
253-
$queueName ?: null, $delay, $this->newQueuedJob()
245+
return $queue->connection($this->getConnection())->laterOn(
246+
$this->getQueue(), $delay, $this->newQueuedJob()
254247
);
255248
}
256249

@@ -467,6 +460,38 @@ protected function buildSubject($message)
467460
return $this;
468461
}
469462

463+
/**
464+
* Get the queue specified on the class or from the OnQueue attribute.
465+
*
466+
* @return string|\UnitEnum|null
467+
*/
468+
protected function getQueue()
469+
{
470+
$queue = property_exists($this, 'queue') ? $this->queue : null;
471+
472+
if ($queue === null) {
473+
$queue = $this->getQueueFromOnConnectionAttribute(new ReflectionClass($this));
474+
}
475+
476+
return $queue;
477+
}
478+
479+
/**
480+
* Get the connection specified on the class or from the OnConnection attribute.
481+
*
482+
* @return string|\UnitEnum|null
483+
*/
484+
protected function getConnection()
485+
{
486+
$connection = property_exists($this, 'connection') ? $this->connection : null;
487+
488+
if ($connection === null) {
489+
$connection = $this->getConnectionFromOnConnectionAttribute(new ReflectionClass($this));
490+
}
491+
492+
return $connection;
493+
}
494+
470495
/**
471496
* Add all of the attachments to the message.
472497
*

tests/Integration/Bus/BusPendingDispatchWithAttributesTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
use Illuminate\Bus\Queueable;
66
use Illuminate\Contracts\Queue\ShouldQueue;
7-
use Illuminate\Foundation\Bus\Attributes\OnConnection;
8-
use Illuminate\Foundation\Bus\Attributes\OnQueue;
97
use Illuminate\Foundation\Bus\Dispatchable;
8+
use Illuminate\Foundation\Queue\OnConnection;
9+
use Illuminate\Foundation\Queue\OnQueue;
1010
use Illuminate\Queue\InteractsWithQueue;
1111
use Illuminate\Support\Facades\Queue;
1212
use Orchestra\Testbench\TestCase;

tests/Support/SupportMailTest.php

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@
22

33
namespace Illuminate\Tests\Support;
44

5+
use Illuminate\Contracts\Queue\ShouldQueue;
6+
use Illuminate\Foundation\Queue\OnConnection;
7+
use Illuminate\Foundation\Queue\OnQueue;
58
use Illuminate\Mail\Mailable;
9+
use Illuminate\Mail\SendQueuedMailable;
610
use Illuminate\Support\Facades\Mail;
11+
use Illuminate\Support\Facades\Queue;
712
use Orchestra\Testbench\TestCase;
13+
use PHPUnit\Framework\Attributes\TestWith;
814

915
class SupportMailTest extends TestCase
1016
{
1117
public function testItRegisterAndCallMacros()
1218
{
13-
Mail::macro('test', fn (string $str) => $str === 'foo'
19+
Mail::macro('test', fn(string $str) => $str === 'foo'
1420
? 'it works!'
1521
: 'it failed.',
1622
);
@@ -20,7 +26,7 @@ public function testItRegisterAndCallMacros()
2026

2127
public function testItRegisterAndCallMacrosWhenFaked()
2228
{
23-
Mail::macro('test', fn (string $str) => $str === 'foo'
29+
Mail::macro('test', fn(string $str) => $str === 'foo'
2430
? 'it works!'
2531
: 'it failed.',
2632
);
@@ -39,6 +45,33 @@ public function testEmailSent()
3945

4046
Mail::assertSent(TestMail::class);
4147
}
48+
49+
#[TestWith([TestMailWithOnQueue::class, 'my-queue'], 'string for queue')]
50+
#[TestWith([TestMailWithEnumOnQueue::class, 'queue-from-enum'], 'enum for queue')]
51+
public function testQueuedMailableRespectsStringOnQueueAttribute(string $mailableClass, string $queueName)
52+
{
53+
Queue::fake();
54+
55+
Mail::send(new $mailableClass());
56+
57+
Queue::assertPushedOn(
58+
$queueName,
59+
SendQueuedMailable::class,
60+
fn ($job) => is_a($job->mailable, $mailableClass, true)
61+
);
62+
}
63+
64+
public function testQueueableMailableUsesQueueIfSetAsProperty()
65+
{
66+
Queue::fake();
67+
Mail::send(new TestMailWithOnQueueAndOnConnectionSetAndBothPropertiesSet());
68+
69+
Queue::assertPushed(function(SendQueuedMailable $sendQueuedMailable) {
70+
return $sendQueuedMailable->mailable instanceof TestMailWithOnQueueAndOnConnectionSetAndBothPropertiesSet
71+
&& $sendQueuedMailable->connection === 'my-connection'
72+
&& $sendQueuedMailable->queue === 'some-other-queue';
73+
});
74+
}
4275
}
4376

4477
class TestMail extends Mailable
@@ -53,3 +86,27 @@ public function build()
5386
return $this->view('view');
5487
}
5588
}
89+
90+
#[OnQueue('my-queue')]
91+
class TestMailWithOnQueue extends Mailable implements ShouldQueue
92+
{
93+
}
94+
95+
#[OnQueue(SupportMailTestEnum::Queue)]
96+
class TestMailWithEnumOnQueue extends Mailable implements ShouldQueue
97+
{
98+
}
99+
100+
#[OnQueue(SupportMailTestEnum::Queue)]
101+
#[OnConnection(SupportMailTestEnum::Connection)]
102+
class TestMailWithOnQueueAndOnConnectionSetAndBothPropertiesSet extends Mailable implements ShouldQueue
103+
{
104+
public $queue = 'some-other-queue';
105+
public $connection = 'my-connection';
106+
}
107+
108+
enum SupportMailTestEnum: string
109+
{
110+
case Queue = 'queue-from-enum';
111+
case Connection = 'connection-from-enum';
112+
}

0 commit comments

Comments
 (0)