Skip to content

Commit

Permalink
Merge pull request #2 from ghostzero/v2-develop
Browse files Browse the repository at this point in the history
  • Loading branch information
ghostzero authored Nov 1, 2020
2 parents 645ca3e + 96cf0bd commit 264a0d8
Show file tree
Hide file tree
Showing 61 changed files with 1,280 additions and 158 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/ci-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: CI Tests

on: [push]

jobs:
build-test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: php-actions/composer@v1
- name: PHPUnit Tests
uses: php-actions/phpunit@v9
with:
configuration: phpunit.xml
args: --coverage-text
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/.idea
composer.lock
.phpunit.result.cache
tmi.php
18 changes: 8 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ Also have a look at [ghostzero/tmi-cluster](https://github.com/ghostzero/tmi-clu
## Getting Started (w/o OAuth Token)

```php
use GhostZero\Tmi\Channel;
use GhostZero\Tmi\Client;
use GhostZero\Tmi\ClientOptions;
use GhostZero\Tmi\Tags;
use GhostZero\Tmi\Events\Twitch\MessageEvent;

$client = new Client(new ClientOptions([
'connection' => [
Expand All @@ -29,8 +28,8 @@ $client = new Client(new ClientOptions([
'channels' => ['ghostzero']
]));

$client->on('message', function (Channel $channel, Tags $tags, string $user, string $message, bool $self) use ($client) {
print "{$tags['display-name']}: {$message}";
$client->on(MessageEvent::class, function (MessageEvent $e) {
print "{$e->tags['display-name']}: {$e->message}";
});

$client->connect();
Expand All @@ -39,10 +38,9 @@ $client->connect();
## Getting Started (w/ OAuth Token)

```php
use GhostZero\Tmi\Channel;
use GhostZero\Tmi\Client;
use GhostZero\Tmi\ClientOptions;
use GhostZero\Tmi\Tags;
use GhostZero\Tmi\Events\Twitch\MessageEvent;

$client = new Client(new ClientOptions([
'options' => ['debug' => true],
Expand All @@ -58,11 +56,11 @@ $client = new Client(new ClientOptions([
'channels' => ['ghostzero']
]));

$client->on('message', function (Channel $channel, Tags $tags, string $user, string $message, bool $self) use ($client) {
if ($self) return;
$client->on(MessageEvent::class, function (MessageEvent $e) use ($client) {
if ($e->self) return;

if (strtolower($message) === '!hello') {
$client->say($channel->getName(), "@{$user}, heya!");
if (strtolower($e->message) === '!hello') {
$client->say($e->channel->getName(), "@{$e->user}, heya!");
}
});

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"require": {
"php": "^7.4",
"react/socket": "^1.6",
"ext-mbstring": "*"
"ext-mbstring": "*",
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "^9"
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
bootstrap="vendor/autoload.php" enforceTimeLimit="true"
colors="true">
<testsuites>
<testsuite name="Unit">
Expand Down
5 changes: 5 additions & 0 deletions src/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,9 @@ public function setUsers(array $users): void
{
$this->users = $users;
}

public function __toString()
{
return $this->getName();
}
}
47 changes: 30 additions & 17 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,14 @@ public function connect(): void
$connectorPromise->then(function (ConnectionInterface $connection) {
$this->connection = $connection;
$this->connected = true;
$this->channels = [];

// login & request all twitch Kappabilities
$identity = $this->options->getIdentity();
$this->write("PASS {$identity['password']}");
$this->write("NICK {$identity['username']}");
$this->write('CAP REQ :twitch.tv/membership twitch.tv/tags twitch.tv/commands');

$channels = $this->options->getChannels();

foreach ($channels as $channel) {
$this->join($channel);
}

$this->connection->on('data', function ($data) {
foreach ($this->ircMessageParser->parse($data) as $message) {
$this->handleIrcMessage($message);
Expand All @@ -73,11 +68,13 @@ public function connect(): void

$this->connection->on('close', function () {
$this->connected = false;
$this->reconnect('Connection closed by Twitch.');
});

$this->connection->on('end', function () {
$this->connected = false;
$this->connection->close();
});
});
}, fn($error) => $this->reconnect($error));

$this->loop->run();
}
Expand All @@ -104,21 +101,18 @@ public function write(string $rawCommand): void
$this->connection->write($rawCommand);
}

private function isConnected(): bool
public function isConnected(): bool
{
return isset($this->connection) && $this->connected;
}

private function handleIrcMessage(IrcMessage $message): void
{
if ($this->options->isDebug()) {
print $message->rawMessage . PHP_EOL;
}
$this->debug($message->rawMessage);

$message->injectChannel($this->channels);
$message->handle($this);
$events = $message->handle($this, $this->channels);

foreach ($message->getEvents() as $event) {
foreach ($events as $event) {
$this->eventHandler->invoke($event);
}
}
Expand All @@ -133,6 +127,11 @@ public function getOptions(): ClientOptions
return $this->options;
}

public function any(Closure $closure): self
{
return $this->on('*', $closure);
}

public function on(string $event, Closure $closure): self
{
$this->eventHandler->addHandler($event, $closure);
Expand All @@ -144,10 +143,24 @@ private function getConnectorPromise(DnsConnector $dnsConnector): Promise
{
if ($this->options->shouldConnectSecure()) {
return (new SecureConnector($dnsConnector, $this->loop))
->connect('irc.chat.twitch.tv:6697');
->connect(sprintf('%s:6697', $this->options->getServer()));
}

return $dnsConnector->connect('irc.chat.twitch.tv:6667');
return $dnsConnector->connect(sprintf('%s:6667', $this->options->getServer()));
}

private function reconnect($error): void
{
if ($this->options->shouldReconnect()) {
$this->debug('Initialize reconnect... Error: ' . $error);
$this->connect();
}
}

private function debug(string $message): void
{
if ($this->options->isDebug()) {
print $message . PHP_EOL;
}
}
}
10 changes: 10 additions & 0 deletions src/ClientOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public function getNickname(): string
return $this->options['identity']['username'] ?? 'justinfan1337';
}

public function getServer(): string
{
return $this->options['connection']['server'] ?? 'irc.chat.twitch.tv';
}

public function shouldAutoRejoin(): bool
{
return $this->options['connection']['rejoin'] ?? true;
Expand All @@ -51,4 +56,9 @@ public function getNameserver(): string
{
return $this->options['connection']['nameserver'] ?? '1.1.1.1';
}

public function getType(): string
{
return $this->options['identity']['type'] ?? 'verified';
}
}
19 changes: 2 additions & 17 deletions src/Events/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,8 @@

class Event
{
private string $event;

private array $arguments;

public function __construct(string $event, array $arguments = [])
{
$this->event = $event;
$this->arguments = $arguments;
}

public function getArguments(): array
{
return $this->arguments;
}

public function getEvent(): string
public function signature(): ?string
{
return $this->event;
return null;
}
}
18 changes: 10 additions & 8 deletions src/Events/EventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,8 @@ public function __construct()
$this->eventHandlers = [];
}

public function addHandler($event, ?callable $function): void
public function addHandler(string $event, callable $function): void
{
if (is_callable($event)) {
$function = $event;
$event = '*';
}

if (!array_key_exists($event, $this->eventHandlers)) {
$this->eventHandlers[$event] = [];
}
Expand All @@ -27,9 +22,16 @@ public function addHandler($event, ?callable $function): void

public function invoke(Event $event): void
{
$handlers = array_merge($this->eventHandlers['*'] ?? [], $this->eventHandlers[$event->getEvent()] ?? []);
$handlers = $this->eventHandlers['*'] ?? [];
$this->invokeHandlers($handlers, $event);
$handlers = $this->eventHandlers[get_class($event)] ?? [];
$this->invokeHandlers($handlers, $event);
}

protected function invokeHandlers(array $handlers, Event $event): void
{
foreach ($handlers as $handler) {
$handler(...$event->getArguments());
$handler($event);
}
}
}
16 changes: 16 additions & 0 deletions src/Events/Inspector/InspectorEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace GhostZero\Tmi\Events\Inspector;

use GhostZero\Tmi\Events\Event;
use stdClass;

class InspectorEvent extends Event
{
public stdClass $payload;

public function __construct(stdClass $payload)
{
$this->payload = $payload;
}
}
15 changes: 15 additions & 0 deletions src/Events/Inspector/InspectorReadyEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace GhostZero\Tmi\Events\Inspector;

use GhostZero\Tmi\Events\Event;

class InspectorReadyEvent extends Event
{
public string $url;

public function __construct(string $url)
{
$this->url = $url;
}
}
20 changes: 20 additions & 0 deletions src/Events/Irc/KickEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace GhostZero\Tmi\Events\Irc;

use GhostZero\Tmi\Channel;
use GhostZero\Tmi\Events\Event;

class KickEvent extends Event
{
public Channel $channel;
public string $user;
public string $message;

public function __construct(Channel $channel, string $user, string $message)
{
$this->channel = $channel;
$this->user = $user;
$this->message = $message;
}
}
15 changes: 15 additions & 0 deletions src/Events/Irc/MotdEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace GhostZero\Tmi\Events\Irc;

use GhostZero\Tmi\Events\Event;

class MotdEvent extends Event
{
public string $message;

public function __construct(string $message)
{
$this->message = $message;
}
}
18 changes: 18 additions & 0 deletions src/Events/Irc/NameReplyEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace GhostZero\Tmi\Events\Irc;

use GhostZero\Tmi\Channel;
use GhostZero\Tmi\Events\Event;

class NameReplyEvent extends Event
{
public Channel $channel;
public array $names;

public function __construct(Channel $channel, array $names)
{
$this->channel = $channel;
$this->names = $names;
}
}
10 changes: 10 additions & 0 deletions src/Events/Irc/PingEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace GhostZero\Tmi\Events\Irc;

use GhostZero\Tmi\Events\Event;

class PingEvent extends Event
{

}
Loading

0 comments on commit 264a0d8

Please sign in to comment.