Test.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OC\Core\Command\Broadcast;
  26. use OCP\EventDispatcher\ABroadcastedEvent;
  27. use OCP\EventDispatcher\IEventDispatcher;
  28. use Symfony\Component\Console\Command\Command;
  29. use Symfony\Component\Console\Input\InputArgument;
  30. use Symfony\Component\Console\Input\InputInterface;
  31. use Symfony\Component\Console\Output\OutputInterface;
  32. class Test extends Command {
  33. /** @var IEventDispatcher */
  34. private $eventDispatcher;
  35. public function __construct(IEventDispatcher $eventDispatcher) {
  36. parent::__construct();
  37. $this->eventDispatcher = $eventDispatcher;
  38. }
  39. protected function configure(): void {
  40. $this
  41. ->setName('broadcast:test')
  42. ->setDescription('test the SSE broadcaster')
  43. ->addArgument(
  44. 'uid',
  45. InputArgument::REQUIRED,
  46. 'the UID of the users to receive the event'
  47. )
  48. ->addArgument(
  49. 'name',
  50. InputArgument::OPTIONAL,
  51. 'the event name',
  52. 'test'
  53. );
  54. }
  55. protected function execute(InputInterface $input, OutputInterface $output): int {
  56. $name = $input->getArgument('name');
  57. $uid = $input->getArgument('uid');
  58. $event = new class($name, $uid) extends ABroadcastedEvent {
  59. /** @var string */
  60. private $name;
  61. /** @var string */
  62. private $uid;
  63. public function __construct(string $name,
  64. string $uid) {
  65. parent::__construct();
  66. $this->name = $name;
  67. $this->uid = $uid;
  68. }
  69. public function broadcastAs(): string {
  70. return $this->name;
  71. }
  72. public function getUids(): array {
  73. return [
  74. $this->uid,
  75. ];
  76. }
  77. public function jsonSerialize() {
  78. return [
  79. 'description' => 'this is a test event',
  80. ];
  81. }
  82. };
  83. $this->eventDispatcher->dispatch('broadcasttest', $event);
  84. return 0;
  85. }
  86. }