Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

broadcast package #42

Open
wants to merge 31 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"Illuminate\\Events\\": "src/event/illuminate/",
"SwooleTW\\Hyperf\\": "src/framework/src/",
"SwooleTW\\Hyperf\\Auth\\": "src/auth/src/",
"SwooleTW\\Hyperf\\Broadcasting\\": "src/broadcasting/src/",
"SwooleTW\\Hyperf\\Bus\\": "src/bus/src/",
"SwooleTW\\Hyperf\\Cache\\": "src/cache/src/",
"SwooleTW\\Hyperf\\Config\\": "src/config/src/",
Expand Down Expand Up @@ -110,6 +111,7 @@
},
"replace": {
"swooletw/hyperf-auth": "self.version",
"swooletw/hyperf-broadcasting": "self.version",
"swooletw/hyperf-bus": "self.version",
"swooletw/hyperf-cache": "self.version",
"swooletw/hyperf-config": "self.version",
Expand Down Expand Up @@ -142,9 +144,12 @@
"aws/aws-sdk-php": "Required to use the SES mail driver (^3.235.5).",
"symfony/http-client": "Required to use the Symfony API mail transports (^6.2).",
"symfony/mailgun-mailer": "Required to enable support for the Mailgun mail transport (^6.2).",
"symfony/postmark-mailer": "Required to enable support for the Postmark mail transport (^6.2)."
"symfony/postmark-mailer": "Required to enable support for the Postmark mail transport (^6.2).",
"ably/ably-php": "Required to use the Ably broadcast driver (^1.0).",
"pusher/pusher-php-server": "Required to use the Pusher broadcast driver (^6.0|^7.0)."
},
"require-dev": {
"ably/ably-php": "^1.0",
"fakerphp/faker": "^2.0",
"filp/whoops": "^2.15",
"friendsofphp/php-cs-fixer": "^3.57.2",
Expand All @@ -161,6 +166,7 @@
"pda/pheanstalk": "v5.x-dev",
"phpstan/phpstan": "^1.11.5",
"phpunit/phpunit": "^10.0.7",
"pusher/pusher-php-server": "^7.2",
"swoole/ide-helper": "~5.1.0"
},
"config": {
Expand All @@ -171,6 +177,7 @@
"config": [
"SwooleTW\\Hyperf\\ConfigProvider",
"SwooleTW\\Hyperf\\Auth\\ConfigProvider",
"SwooleTW\\Hyperf\\Broadcasting\\ConfigProvider",
"SwooleTW\\Hyperf\\Bus\\ConfigProvider",
"SwooleTW\\Hyperf\\Cache\\ConfigProvider",
"SwooleTW\\Hyperf\\Cookie\\ConfigProvider",
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ parameters:
- '#Access to an undefined property SwooleTW\\Hyperf\\Queue\\Jobs\\DatabaseJobRecord::\$.*#'
- '#Access to an undefined property SwooleTW\\Hyperf\\Queue\\Contracts\\Job::\$.*#'
- '#Call to an undefined method Hyperf\\Database\\Query\\Builder::where[a-zA-Z0-9\\\\_]+#'
- '#Call to an undefined method Hyperf\\Database\\Query\\Builder::firstOrFail\(\)#'
- '#Call to an undefined method Hyperf\\Database\\Query\\Builder::firstOrFail\(\)#'
8 changes: 8 additions & 0 deletions src/auth/src/Authenticatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ public function getAuthIdentifier(): mixed
return $this->{$this->getAuthIdentifierName()};
}

/**
* Get the unique broadcast identifier for the user.
*/
public function getAuthIdentifierForBroadcasting(): mixed
{
return $this->getAuthIdentifier();
}

/**
* Get the password for the user.
*/
Expand Down
25 changes: 25 additions & 0 deletions src/broadcasting/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
The MIT License (MIT)

Copyright (c) Taylor Otwell

Copyright (c) Hyperf

Copyright (c) Laravel Hyperf

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
2 changes: 2 additions & 0 deletions src/broadcasting/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Broadcasting for Laravel Hyperf
===
64 changes: 64 additions & 0 deletions src/broadcasting/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"name": "swooletw/hyperf-filesystem",
"type": "library",
"description": "The filesystem package for Hyperf.",
"license": "MIT",
"keywords": [
"php",
"hyperf",
"swoole",
"broadcasting",
"laravel-hyperf"
],
"authors": [
{
"name": "Albert Chen",
"email": "[email protected]",
"homepage": "https://albert-chen.com"
}
],
"support": {
"issues": "https://github.com/swooletw/hyperf-packages/issues",
"source": "https://github.com/swooletw/hyperf-packages"
},
"autoload": {
"psr-4": {
"SwooleTW\\Hyperf\\Broadcasting": "src/"
}
},
"require": {
"php": "^8.2",
"hyperf/collection": "~3.1.0",
"hyperf/contract": "~3.1.0",
"hyperf/http-server": "~3.1.0",
"hyperf/pool": "~3.1.0",
"hyperf/redis": "~3.1.0",
"hyperf/stringable": "~3.1.0",
"swooletw/hyperf-auth": "dev-master",
"swooletw/hyperf-bus": "dev-master",
"swooletw/hyperf-cache": "dev-master",
"swooletw/hyperf-foundation": "dev-master",
"swooletw/hyperf-framework": "dev-master",
"swooletw/hyperf-queue": "dev-master",
"swooletw/hyperf-router": "dev-master",
"swooletw/hyperf-support": "dev-master",
"swooletw/object-pool": "dev-master"
},
"suggest": {
"ext-hash": "Required to use the Ably and Pusher broadcast drivers.",
"ext-redis": "Required to use the Redis cache and queue drivers (^4.0|^5.0|^6.0).",
"ably/ably-php": "Required to use the Ably broadcast driver (^1.0).",
"pusher/pusher-php-server": "Required to use the Pusher broadcast driver (^6.0|^7.0)."
},
"config": {
"sort-packages": true
},
"extra": {
"hyperf": {
"config": "SwooleTW\\Hyperf\\Broadcasting\\ConfigProvider"
},
"branch-alias": {
"dev-main": "3.1-dev"
}
}
}
152 changes: 152 additions & 0 deletions src/broadcasting/src/AnonymousEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<?php

declare(strict_types=1);

namespace SwooleTW\Hyperf\Broadcasting;

use Hyperf\Collection\Arr;
use Hyperf\Contract\Arrayable;
use SwooleTW\Hyperf\Broadcasting\Contracts\ShouldBroadcast;
use SwooleTW\Hyperf\Foundation\Events\Dispatchable;

class AnonymousEvent implements ShouldBroadcast
{
use Dispatchable;
use InteractsWithBroadcasting;
use InteractsWithSockets;

/**
* The connection the event should be broadcast on.
*/
protected ?string $connection = null;

/**
* The name the event should be broadcast as.
*/
protected ?string $name = null;

/**
* The payload the event should be broadcast with.
*/
protected array $payload = [];

/**
* Should the broadcast include the current user.
*/
protected bool $includeCurrentUser = true;

/**
* Indicates if the event should be broadcast synchronously.
*/
protected bool $shouldBroadcastNow = false;

/**
* Create a new anonymous broadcastable event instance.
*/
public function __construct(protected BroadcastManager $broadcastManager, protected array|Channel|string $channels)
{
$this->channels = Arr::wrap($channels);
}

/**
* Set the connection the event should be broadcast on.
*/
public function via(string $connection): static
{
$this->connection = $connection;

return $this;
}

/**
* Set the name the event should be broadcast as.
*/
public function as(string $name): static
{
$this->name = $name;

return $this;
}

/**
* Set the payload the event should be broadcast with.
*/
public function with(array|Arrayable $payload): static
{
$this->payload = $payload instanceof Arrayable
? $payload->toArray()
: collect($payload)->map(
fn ($p) => $p instanceof Arrayable ? $p->toArray() : $p
)->all();

return $this;
}

/**
* Broadcast the event to everyone except the current user.
*/
public function toOthers(): static
{
$this->includeCurrentUser = false;

return $this;
}

/**
* Broadcast the event.
*/
public function sendNow(): void
{
$this->shouldBroadcastNow = true;

$this->send();
}

/**
* Broadcast the event.
*/
public function send(): void
{
$broadcast = $this->broadcastManager->event($this)->via($this->connection);

if (! $this->includeCurrentUser) {
$broadcast->toOthers();
}
}

/**
* Get the name the event should broadcast as.
*/
public function broadcastAs(): string
{
return $this->name ?: class_basename($this);
}

/**
* Get the payload the event should broadcast with.
*
* @return array<string, mixed>
*/
public function broadcastWith(): array
{
return $this->payload;
}

/**
* Get the channels the event should broadcast on.
*
* @return Channel[]|string[]
*/
public function broadcastOn(): array
{
return $this->channels;
}

/**
* Determine if the event should be broadcast synchronously.
*/
public function shouldBroadcastNow(): bool
{
return $this->shouldBroadcastNow;
}
}
32 changes: 32 additions & 0 deletions src/broadcasting/src/BroadcastController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace SwooleTW\Hyperf\Broadcasting;

use Hyperf\HttpServer\Contract\RequestInterface;
use SwooleTW\Hyperf\HttpMessage\Exceptions\AccessDeniedHttpException;
use SwooleTW\Hyperf\Support\Facades\Broadcast;

class BroadcastController
{
/**
* Authenticate the request for channel access.
*/
public function authenticate(RequestInterface $request): mixed
{
return Broadcast::auth($request);
}

/**
* Authenticate the current user.
*
* See: https://pusher.com/docs/channels/server_api/authenticating-users/#user-authentication.
*
* @throws AccessDeniedHttpException
*/
public function authenticateUser(RequestInterface $request): array
{
return Broadcast::resolveAuthenticatedUser($request) ?? throw new AccessDeniedHttpException();
}
}
Loading
Loading