-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from spiral/feature/broadcasting
Adds Broadcasting plugin support
- Loading branch information
Showing
9 changed files
with
566 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\RoadRunnerBridge\Bootloader; | ||
|
||
use Psr\Http\Message\ResponseFactoryInterface; | ||
use Spiral\Boot\Bootloader\Bootloader; | ||
use Spiral\Broadcasting\Bootloader\BroadcastingBootloader as BaseBroadcastingBootloader; | ||
use Spiral\Broadcasting\Bootloader\WebsocketsBootloader; | ||
use Spiral\Broadcasting\Config\BroadcastConfig; | ||
use Spiral\Broadcasting\TopicRegistryInterface; | ||
use Spiral\Core\InvokerInterface; | ||
use Spiral\Core\ScopeInterface; | ||
use Spiral\RoadRunner\Broadcast\Broadcast; | ||
use Spiral\RoadRunner\Broadcast\BroadcastInterface; | ||
use Spiral\RoadRunnerBridge\Broadcasting\RoadRunnerBroadcast; | ||
use Spiral\Goridge\RPC\RPCInterface; | ||
use Spiral\RoadRunnerBridge\Broadcasting\RoadRunnerGuard; | ||
|
||
final class BroadcastingBootloader extends Bootloader | ||
{ | ||
protected const DEPENDENCIES = [ | ||
RoadRunnerBootloader::class, | ||
BaseBroadcastingBootloader::class, | ||
WebsocketsBootloader::class, | ||
]; | ||
|
||
protected const SINGLETONS = [ | ||
BroadcastInterface::class => [self::class, 'initBroadcast'], | ||
RoadRunnerGuard::class => [self::class, 'initRoadRunnerGuard'], | ||
]; | ||
|
||
public function boot(BaseBroadcastingBootloader $broadcastingBootloader): void | ||
{ | ||
$broadcastingBootloader->registerDriverAlias('roadrunner', RoadRunnerBroadcast::class); | ||
} | ||
|
||
private function initBroadcast(RPCInterface $rpc): BroadcastInterface | ||
{ | ||
$broadcast = new Broadcast($rpc); | ||
|
||
if (!$broadcast->isAvailable()) { | ||
throw new \LogicException('The [broadcast] plugin not available'); | ||
} | ||
|
||
return $broadcast; | ||
} | ||
|
||
private function initRoadRunnerGuard( | ||
ResponseFactoryInterface $responseFactory, | ||
InvokerInterface $invoker, | ||
ScopeInterface $scope, | ||
TopicRegistryInterface $registry, | ||
BroadcastConfig $config | ||
): RoadRunnerGuard { | ||
return new RoadRunnerGuard( | ||
$invoker, | ||
$scope, | ||
$registry, | ||
$config['authorize']['serverCallback'] ?? null | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\RoadRunnerBridge\Broadcasting; | ||
|
||
use Psr\Http\Message\ServerRequestInterface; | ||
use Spiral\Broadcasting\AuthorizationStatus; | ||
use Spiral\Broadcasting\Driver\AbstractBroadcast; | ||
use Spiral\Broadcasting\GuardInterface; | ||
use Spiral\RoadRunner\Broadcast\BroadcastInterface; | ||
use Spiral\RoadRunner\Broadcast\TopicInterface; | ||
|
||
final class RoadRunnerBroadcast extends AbstractBroadcast implements GuardInterface | ||
{ | ||
private BroadcastInterface $broadcast; | ||
private GuardInterface $guard; | ||
|
||
public function __construct( | ||
BroadcastInterface $broadcast, | ||
GuardInterface $guard | ||
) { | ||
$this->broadcast = $broadcast; | ||
$this->guard = $guard; | ||
} | ||
|
||
/** | ||
* @param non-empty-list<string> $topics | ||
* @param non-empty-list<string> $messages | ||
* | ||
* @throws \Spiral\RoadRunner\Broadcast\Exception\BroadcastException | ||
*/ | ||
public function publish($topics, $messages): void | ||
{ | ||
$this->broadcast->publish($topics, $messages); | ||
} | ||
|
||
public function join($topics): TopicInterface | ||
{ | ||
return $this->broadcast->join($topics); | ||
} | ||
|
||
public function authorize(ServerRequestInterface $request): AuthorizationStatus | ||
{ | ||
return $this->guard->authorize($request); | ||
} | ||
} |
Oops, something went wrong.