Skip to content

Commit 54ccc16

Browse files
Broadcasting Utilities (#55967)
* work on broadcasting helpers and rescue contract * Apply fixes from StyleCI * work on broadcasting helpers and rescue contract * Apply fixes from StyleCI * test broadcast_if * more tests * Rename var --------- Co-authored-by: StyleCI Bot <bot@styleci.io>
1 parent 78cf4ee commit 54ccc16

File tree

6 files changed

+172
-17
lines changed

6 files changed

+172
-17
lines changed

src/Illuminate/Broadcasting/BroadcastManager.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Illuminate\Contracts\Broadcasting\Factory as FactoryContract;
1515
use Illuminate\Contracts\Broadcasting\ShouldBeUnique;
1616
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
17+
use Illuminate\Contracts\Broadcasting\ShouldRescue;
1718
use Illuminate\Contracts\Bus\Dispatcher as BusDispatcherContract;
1819
use Illuminate\Contracts\Cache\Repository as Cache;
1920
use Illuminate\Contracts\Foundation\CachesRoutes;
@@ -178,7 +179,12 @@ public function queue($event)
178179
(is_object($event) &&
179180
method_exists($event, 'shouldBroadcastNow') &&
180181
$event->shouldBroadcastNow())) {
181-
return $this->app->make(BusDispatcherContract::class)->dispatchNow(new BroadcastEvent(clone $event));
182+
$dispatch = fn () => $this->app->make(BusDispatcherContract::class)
183+
->dispatchNow(new BroadcastEvent(clone $event));
184+
185+
return $event instanceof ShouldRescue
186+
? $this->rescue($dispatch)
187+
: $dispatch();
182188
}
183189

184190
$queue = null;
@@ -201,9 +207,13 @@ public function queue($event)
201207
}
202208
}
203209

204-
$this->app->make('queue')
210+
$push = fn () => $this->app->make('queue')
205211
->connection($event->connection ?? null)
206212
->pushOn($queue, $broadcastEvent);
213+
214+
$event instanceof ShouldRescue
215+
? $this->rescue($push)
216+
: $push();
207217
}
208218

209219
/**
@@ -475,6 +485,21 @@ public function extend($driver, Closure $callback)
475485
return $this;
476486
}
477487

488+
/**
489+
* Execute the given callback using "rescue" if possible.
490+
*
491+
* @param \Closure $callback
492+
* @return mixed
493+
*/
494+
protected function rescue(Closure $callback)
495+
{
496+
if (function_exists('rescue')) {
497+
return rescue($callback);
498+
}
499+
500+
return $callback();
501+
}
502+
478503
/**
479504
* Get the application instance used by the manager.
480505
*
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Illuminate\Broadcasting;
4+
5+
class FakePendingBroadcast extends PendingBroadcast
6+
{
7+
/**
8+
* Create a new pending broadcast instance.
9+
*/
10+
public function __construct()
11+
{
12+
//
13+
}
14+
15+
/**
16+
* Broadcast the event using a specific broadcaster.
17+
*
18+
* @param string|null $connection
19+
* @return $this
20+
*/
21+
public function via($connection = null)
22+
{
23+
return $this;
24+
}
25+
26+
/**
27+
* Broadcast the event to everyone except the current user.
28+
*
29+
* @return $this
30+
*/
31+
public function toOthers()
32+
{
33+
return $this;
34+
}
35+
36+
/**
37+
* Handle the object's destruction.
38+
*
39+
* @return void
40+
*/
41+
public function __destruct()
42+
{
43+
//
44+
}
45+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Illuminate\Contracts\Broadcasting;
4+
5+
interface ShouldRescue
6+
{
7+
//
8+
}

src/Illuminate/Foundation/helpers.php

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use Illuminate\Broadcasting\FakePendingBroadcast;
34
use Illuminate\Container\Container;
45
use Illuminate\Contracts\Auth\Access\Gate;
56
use Illuminate\Contracts\Auth\Factory as AuthFactory;
@@ -32,7 +33,6 @@
3233
*
3334
* @param \Symfony\Component\HttpFoundation\Response|\Illuminate\Contracts\Support\Responsable|int $code
3435
* @param string $message
35-
* @param array $headers
3636
* @return never
3737
*
3838
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
@@ -58,7 +58,6 @@ function abort($code, $message = '', array $headers = [])
5858
* @param bool $boolean
5959
* @param \Symfony\Component\HttpFoundation\Response|\Illuminate\Contracts\Support\Responsable|int $code
6060
* @param string $message
61-
* @param array $headers
6261
* @return void
6362
*
6463
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
@@ -79,7 +78,6 @@ function abort_if($boolean, $code, $message = '', array $headers = [])
7978
* @param bool $boolean
8079
* @param \Symfony\Component\HttpFoundation\Response|\Illuminate\Contracts\Support\Responsable|int $code
8180
* @param string $message
82-
* @param array $headers
8381
* @return void
8482
*
8583
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
@@ -115,7 +113,6 @@ function action($name, $parameters = [], $absolute = true)
115113
* @template TClass of object
116114
*
117115
* @param string|class-string<TClass>|null $abstract
118-
* @param array $parameters
119116
* @return ($abstract is class-string<TClass> ? TClass : ($abstract is null ? \Illuminate\Foundation\Application : mixed))
120117
*/
121118
function app($abstract = null, array $parameters = [])
@@ -227,6 +224,42 @@ function broadcast($event = null)
227224
}
228225
}
229226

227+
if (! function_exists('broadcast_if')) {
228+
/**
229+
* Begin broadcasting an event if the given condition is true.
230+
*
231+
* @param bool $boolean
232+
* @param mixed|null $event
233+
* @return \Illuminate\Broadcasting\PendingBroadcast
234+
*/
235+
function broadcast_if($boolean, $event = null)
236+
{
237+
if ($boolean) {
238+
return app(BroadcastFactory::class)->event($event);
239+
} else {
240+
return new FakePendingBroadcast;
241+
}
242+
}
243+
}
244+
245+
if (! function_exists('broadcast_unless')) {
246+
/**
247+
* Begin broadcasting an event unless the given condition is true.
248+
*
249+
* @param bool $boolean
250+
* @param mixed|null $event
251+
* @return \Illuminate\Broadcasting\PendingBroadcast
252+
*/
253+
function broadcast_unless($boolean, $event = null)
254+
{
255+
if (! $boolean) {
256+
return app(BroadcastFactory::class)->event($event);
257+
} else {
258+
return new FakePendingBroadcast;
259+
}
260+
}
261+
}
262+
230263
if (! function_exists('cache')) {
231264
/**
232265
* Get / set the specified cache value.
@@ -406,9 +439,6 @@ function decrypt($value, $unserialize = true)
406439
/**
407440
* Defer execution of the given callback.
408441
*
409-
* @param callable|null $callback
410-
* @param string|null $name
411-
* @param bool $always
412442
* @return \Illuminate\Support\Defer\DeferredCallback
413443
*/
414444
function defer(?callable $callback = null, ?string $name = null, bool $always = false)
@@ -521,7 +551,6 @@ function info($message, $context = [])
521551
* Log a debug message to the logs.
522552
*
523553
* @param string|null $message
524-
* @param array $context
525554
* @return ($message is null ? \Illuminate\Log\LogManager : null)
526555
*/
527556
function logger($message = null, array $context = [])
@@ -799,7 +828,6 @@ function rescue(callable $callback, $rescue = null, $report = true)
799828
* @template TClass of object
800829
*
801830
* @param string|class-string<TClass> $name
802-
* @param array $parameters
803831
* @return ($name is class-string<TClass> ? TClass : mixed)
804832
*/
805833
function resolve($name, array $parameters = [])
@@ -827,7 +855,6 @@ function resource_path($path = '')
827855
*
828856
* @param \Illuminate\Contracts\View\View|string|array|null $content
829857
* @param int $status
830-
* @param array $headers
831858
* @return ($content is null ? \Illuminate\Contracts\Routing\ResponseFactory : \Illuminate\Http\Response)
832859
*/
833860
function response($content = null, $status = 200, array $headers = [])
@@ -975,7 +1002,6 @@ function trans($key = null, $replace = [], $locale = null)
9751002
*
9761003
* @param string $key
9771004
* @param \Countable|int|float|array $number
978-
* @param array $replace
9791005
* @param string|null $locale
9801006
* @return string
9811007
*/
@@ -1041,10 +1067,6 @@ function url($path = null, $parameters = [], $secure = null)
10411067
/**
10421068
* Create a new Validator instance.
10431069
*
1044-
* @param array|null $data
1045-
* @param array $rules
1046-
* @param array $messages
1047-
* @param array $attributes
10481070
* @return ($data is null ? \Illuminate\Contracts\Validation\Factory : \Illuminate\Contracts\Validation\Validator)
10491071
*/
10501072
function validator(?array $data = null, array $rules = [], array $messages = [], array $attributes = [])

tests/Foundation/FoundationHelpersTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Illuminate\Tests\Foundation;
44

55
use Exception;
6+
use Illuminate\Broadcasting\FakePendingBroadcast;
67
use Illuminate\Container\Container;
78
use Illuminate\Contracts\Cache\Repository as CacheRepository;
89
use Illuminate\Contracts\Config\Repository;
@@ -295,4 +296,9 @@ public function testAbortReceivesCodeAsInteger()
295296

296297
abort($code, $message, $headers);
297298
}
299+
300+
public function testBroadcastIfReturnsFakeOnFalse()
301+
{
302+
$this->assertInstanceOf(FakePendingBroadcast::class, broadcast_if(false, 'foo'));
303+
}
298304
}

tests/Integration/Broadcasting/BroadcastManagerTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Illuminate\Contracts\Broadcasting\ShouldBeUnique;
1111
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
1212
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
13+
use Illuminate\Contracts\Broadcasting\ShouldRescue;
1314
use Illuminate\Contracts\Cache\Repository as Cache;
1415
use Illuminate\Support\Facades\Broadcast;
1516
use Illuminate\Support\Facades\Bus;
@@ -41,6 +42,28 @@ public function testEventsCanBeBroadcast()
4142
Queue::assertPushed(BroadcastEvent::class);
4243
}
4344

45+
public function testEventsCanBeRescued()
46+
{
47+
Bus::fake();
48+
Queue::fake();
49+
50+
Broadcast::queue(new TestEventRescue);
51+
52+
Bus::assertNotDispatched(BroadcastEvent::class);
53+
Queue::assertPushed(BroadcastEvent::class);
54+
}
55+
56+
public function testNowEventsCanBeRescued()
57+
{
58+
Bus::fake();
59+
Queue::fake();
60+
61+
Broadcast::queue(new TestEventNowRescue);
62+
63+
Bus::assertDispatched(BroadcastEvent::class);
64+
Queue::assertNotPushed(BroadcastEvent::class);
65+
}
66+
4467
public function testUniqueEventsCanBeBroadcast()
4568
{
4669
Bus::fake();
@@ -124,3 +147,29 @@ public function broadcastOn()
124147
//
125148
}
126149
}
150+
151+
class TestEventRescue implements ShouldBroadcast, ShouldRescue
152+
{
153+
/**
154+
* Get the channels the event should broadcast on.
155+
*
156+
* @return \Illuminate\Broadcasting\Channel|\Illuminate\Broadcasting\Channel[]
157+
*/
158+
public function broadcastOn()
159+
{
160+
//
161+
}
162+
}
163+
164+
class TestEventNowRescue implements ShouldBroadcastNow, ShouldRescue
165+
{
166+
/**
167+
* Get the channels the event should broadcast on.
168+
*
169+
* @return \Illuminate\Broadcasting\Channel|\Illuminate\Broadcasting\Channel[]
170+
*/
171+
public function broadcastOn()
172+
{
173+
//
174+
}
175+
}

0 commit comments

Comments
 (0)