-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make it possible to broadcast events to (web) clients
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
- Loading branch information
1 parent
908f13e
commit 98f6bfd
Showing
6 changed files
with
242 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php declare(strict_types=1); | ||
|
||
/** | ||
* @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> | ||
* | ||
* @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> | ||
* | ||
* @license GNU AGPL version 3 or any later version | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
namespace OC\Core\Command\Broadcast; | ||
|
||
use OCP\EventDispatcher\ABroadcastedEvent; | ||
use OCP\EventDispatcher\IEventDispatcher; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class Test extends Command { | ||
|
||
/** @var IEventDispatcher */ | ||
private $eventDispatcher; | ||
|
||
public function __construct(IEventDispatcher $eventDispatcher) { | ||
parent::__construct(); | ||
$this->eventDispatcher = $eventDispatcher; | ||
} | ||
|
||
protected function configure(): void { | ||
$this | ||
->setName('broadcast:test') | ||
->setDescription('test the SSE broadcaster') | ||
->addArgument( | ||
'uid', | ||
InputArgument::REQUIRED, | ||
'the UID of the users to receive the event' | ||
) | ||
->addArgument( | ||
'name', | ||
InputArgument::OPTIONAL, | ||
'the event name', | ||
'test' | ||
); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output) { | ||
$name = $input->getArgument('name'); | ||
$uid = $input->getArgument('uid'); | ||
|
||
$event = new class($name, $uid) extends ABroadcastedEvent { | ||
/** @var string */ | ||
private $name; | ||
/** @var string */ | ||
private $uid; | ||
|
||
public function __construct(string $name, | ||
string $uid) { | ||
parent::__construct(); | ||
$this->name = $name; | ||
$this->uid = $uid; | ||
} | ||
|
||
public function broadcastAs(): string { | ||
return $this->name; | ||
} | ||
|
||
public function getUids(): array { | ||
return [ | ||
$this->uid, | ||
]; | ||
} | ||
|
||
public function serialize(): array { | ||
return [ | ||
'description' => 'this is a test event', | ||
]; | ||
} | ||
}; | ||
|
||
$this->eventDispatcher->dispatch('broadcasttest', $event); | ||
|
||
return 0; | ||
} | ||
|
||
} |
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,37 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace OC\Broadcast\Events; | ||
|
||
use JsonSerializable; | ||
use OCP\Broadcast\Events\IBroadcastEvent; | ||
use OCP\EventDispatcher\ABroadcastedEvent; | ||
use OCP\EventDispatcher\Event; | ||
|
||
class BroadcastEvent extends Event implements IBroadcastEvent { | ||
|
||
/** @var ABroadcastedEvent */ | ||
private $event; | ||
|
||
public function __construct(ABroadcastedEvent $event) { | ||
parent::__construct(); | ||
|
||
$this->event = $event; | ||
} | ||
|
||
public function getName(): string { | ||
return $this->event->broadcastAs(); | ||
} | ||
|
||
public function getUids(): array { | ||
return $this->event->getUids(); | ||
} | ||
|
||
public function getPayload(): JsonSerializable { | ||
return $this->event; | ||
} | ||
|
||
public function setBroadcasted(): void { | ||
$this->event->setBroadcasted(); | ||
} | ||
|
||
} |
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,35 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace OCP\Broadcast\Events; | ||
|
||
use JsonSerializable; | ||
|
||
/** | ||
* @since 18.0.0 | ||
*/ | ||
interface IBroadcastEvent { | ||
|
||
/** | ||
* @return string the name of the event | ||
* @since 18.0.0 | ||
*/ | ||
public function getName(): string; | ||
|
||
/** | ||
* @return string[] | ||
* @since 18.0.0 | ||
*/ | ||
public function getUids(): array; | ||
|
||
/** | ||
* @return JsonSerializable the data to be sent to the client | ||
* @since 18.0.0 | ||
*/ | ||
public function getPayload(): JsonSerializable; | ||
|
||
/** | ||
* @since 18.0.0 | ||
*/ | ||
public function setBroadcasted(): void; | ||
|
||
} |
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,57 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace OCP\EventDispatcher; | ||
|
||
use JsonSerializable; | ||
|
||
/** | ||
* @since 18.0.0 | ||
*/ | ||
abstract class ABroadcastedEvent extends Event implements JsonSerializable { | ||
|
||
private $broadcasted = false; | ||
|
||
/** | ||
* Get the name of the event, as received on the client-side | ||
* | ||
* Uses the fully qualified event class name by default | ||
* | ||
* @return string | ||
* @since 18.0.0 | ||
*/ | ||
public function broadcastAs(): string { | ||
return get_class($this); | ||
} | ||
|
||
/** | ||
* @return string[] | ||
* @since 18.0.0 | ||
*/ | ||
abstract public function getUids(): array; | ||
|
||
/** | ||
* @since 18.0.0 | ||
*/ | ||
public function setBroadcasted(): void { | ||
$this->broadcasted = true; | ||
} | ||
|
||
/** | ||
* @since 18.0.0 | ||
*/ | ||
public function isBroadcasted(): bool { | ||
return $this->broadcasted; | ||
} | ||
|
||
abstract public function serialize(): array; | ||
|
||
public final function jsonSerialize() { | ||
return array_merge( | ||
$this->serialize(), | ||
[ | ||
'name' => $this->broadcastAs(), | ||
] | ||
); | ||
} | ||
|
||
} |