Skip to content

Commit

Permalink
Changed interface of Conveyor Server - so it becomes easier integrati…
Browse files Browse the repository at this point in the history
…ng it with other projects.
  • Loading branch information
lotharthesavior committed Sep 25, 2024
1 parent 236992e commit 5fd9f09
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 93 deletions.
92 changes: 33 additions & 59 deletions src/ConveyorServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Conveyor\SubProtocols\Conveyor\ConveyorWorker;
use Conveyor\SubProtocols\Conveyor\Persistence\Interfaces\GenericPersistenceInterface;
use Conveyor\Traits\HasHandlers;
use Conveyor\Traits\HasProperties;
use Exception;
use OpenSwoole\Constant;
use OpenSwoole\Http\Request;
Expand All @@ -22,46 +23,53 @@
class ConveyorServer implements ConveyorServerInterface
{
use HasHandlers;

/**
* Conveyor Parts
*/
use HasProperties;

protected Server $server;

protected EventDispatcher $eventDispatcher;

protected string $host = '0.0.0.0';

protected int $port = 8989;

protected int $mode = OpenSwooleBaseServer::POOL_MODE;

protected int $socketType = Constant::SOCK_TCP;

/**
* Reference for Server Options:
* https://openswoole.com/docs/modules/swoole-server/configuration
*
* @param string $host
* @param int $port
* @param int $mode
* @param int $ssl
* @param array<array-key, mixed> $serverOptions
* @param ConveyorOptions|array<array-key, mixed> $conveyorOptions
* @param array<array-key, callable> $eventListeners
* @param array<array-key, GenericPersistenceInterface> $persistence
* @throws Exception
* @var array<mixed> $serverOptions
*/
protected array $serverOptions = [];

/**
* @var array<mixed>|ConveyorOptions
*/
protected array|ConveyorOptions $conveyorOptions = [];

/**
* @var array<callable|array<string>> $eventListeners
*/
public function __construct(
protected string $host = '0.0.0.0',
protected int $port = 8989,
protected int $mode = OpenSwooleBaseServer::POOL_MODE,
protected int $ssl = Constant::SOCK_TCP,
protected array $serverOptions = [],
protected array|ConveyorOptions $conveyorOptions = [],
protected array $eventListeners = [],
protected array $persistence = [],
) {
protected array $eventListeners = [];

/**
* @var array<GenericPersistenceInterface> $persistence
*/
protected array $persistence = [];

public function start(): void
{
if (is_array($this->conveyorOptions)) {
$this->conveyorOptions = ConveyorOptions::fromArray(array_merge(
Constants::DEFAULT_OPTIONS,
$this->conveyorOptions,
));
}

$this->startPersistence($persistence);
$this->startPersistence($this->persistence);

$this->startListener();

Expand All @@ -72,40 +80,6 @@ public function __construct(
$this->startServer();
}

/**
* @param string $host
* @param int $port
* @param int $mode
* @param int $ssl
* @param array<array-key, mixed> $serverOptions
* @param ConveyorOptions|array<array-key, mixed> $conveyorOptions
* @param array<array-key, callable> $eventListeners
* @param array<array-key, GenericPersistenceInterface> $persistence
* @return ConveyorServer
* @throws Exception
*/
public static function start(
string $host = '0.0.0.0',
int $port = 8989,
int $mode = OpenSwooleBaseServer::POOL_MODE,
int $ssl = Constant::SOCK_TCP,
array $serverOptions = [],
ConveyorOptions|array $conveyorOptions = [],
array $eventListeners = [],
array $persistence = [],
): ConveyorServer {
return new self(
host: $host,
port: $port,
mode: $mode,
ssl: $ssl,
serverOptions: $serverOptions,
conveyorOptions: $conveyorOptions,
eventListeners: $eventListeners,
persistence: $persistence,
);
}

private function startListener(): void
{
$this->eventDispatcher = new EventDispatcher();
Expand All @@ -129,7 +103,7 @@ private function startPersistence(array $persistence): void

private function initializeServer(): void
{
$this->server = new Server($this->host, $this->port, $this->mode, $this->ssl);
$this->server = new Server($this->host, $this->port, $this->mode, $this->socketType);

$this->server->set(array_merge([
'worker_num' => 10,
Expand Down
30 changes: 1 addition & 29 deletions src/Interfaces/ConveyorServerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,7 @@

namespace Conveyor\Interfaces;

use Conveyor\Config\ConveyorOptions;
use Conveyor\ConveyorServer;
use Conveyor\SubProtocols\Conveyor\Persistence\Interfaces\GenericPersistenceInterface;
use Exception;
use OpenSwoole\Constant;
use OpenSwoole\Server as OpenSwooleBaseServer;

interface ConveyorServerInterface
{
/**
* @param string $host
* @param int $port
* @param int $mode
* @param int $ssl
* @param array<array-key, mixed> $serverOptions
* @param ConveyorOptions|array<array-key, mixed> $conveyorOptions
* @param array<array-key, callable> $eventListeners
* @param array<array-key, GenericPersistenceInterface> $persistence
* @return ConveyorServer
* @throws Exception
*/
public static function start(
string $host = '0.0.0.0',
int $port = 8989,
int $mode = OpenSwooleBaseServer::POOL_MODE,
int $ssl = Constant::SOCK_TCP,
array $serverOptions = [],
ConveyorOptions|array $conveyorOptions = [],
array $eventListeners = [],
array $persistence = [],
): ConveyorServer;
public function start(): void;
}
81 changes: 81 additions & 0 deletions src/Traits/HasProperties.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace Conveyor\Traits;

use Conveyor\Config\ConveyorOptions;
use Conveyor\SubProtocols\Conveyor\Persistence\Interfaces\GenericPersistenceInterface;

trait HasProperties
{
public function host(string $host): static
{
$this->host = $host;

return $this;
}

public function port(int $port): static
{
$this->port = $port;

return $this;
}

public function mode(int $mode): static
{
$this->mode = $mode;

return $this;
}

public function socketType(int $socketType): static
{
$this->socketType = $socketType;

return $this;
}

/**
* @param array<mixed> $serverOptions
* @return $this
*/
public function serverOptions(array $serverOptions): static
{
$this->serverOptions = $serverOptions;

return $this;
}

/**
* @param array<mixed>|ConveyorOptions $conveyorOptions
* @return $this
*/
public function conveyorOptions(array|ConveyorOptions $conveyorOptions): static
{
$this->conveyorOptions = $conveyorOptions;

return $this;
}

/**
* @param array<callable|array<string>> $eventListeners
* @return $this
*/
public function eventListeners(array $eventListeners): static
{
$this->eventListeners = $eventListeners;

return $this;
}

/**
* @param array<GenericPersistenceInterface> $persistence
* @return $this
*/
public function persistence(array $persistence): static
{
$this->persistence = $persistence;

return $this;
}
}
10 changes: 5 additions & 5 deletions tests/Feature/OpenSwooleSocketTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ protected function startServer(
$actions,
$usePresence
) {
ConveyorServer::start(
port: $this->port,
conveyorOptions: [
(new ConveyorServer())
->port($this->port)
->conveyorOptions([
Constants::ACTIONS => $actions,
Constants::USE_PRESENCE => $usePresence,
],
);
])
->start();
});

$pid = $httpServer->start();
Expand Down

0 comments on commit 5fd9f09

Please sign in to comment.