Skip to content

Commit

Permalink
Added support for server standalone creation (#14)
Browse files Browse the repository at this point in the history
* Added support for server standalone creation

* QA fix

* Added class alias for older nette support

* Updating GH action configuration
  • Loading branch information
akadlec committed Apr 19, 2023
1 parent 88b7e4f commit 1db1a3c
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 57 deletions.
24 changes: 12 additions & 12 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:

steps:
- name: "Checkout"
uses: "actions/checkout@v2"
uses: "actions/checkout@v3"

- name: "Setup PHP cache environment"
id: "extcache"
Expand All @@ -41,7 +41,7 @@ jobs:
key: "${{ env.cache-version }}"

- name: "Cache PHP extensions"
uses: "actions/cache@v2"
uses: "actions/cache@v3"
with:
path: "${{ steps.extcache.outputs.dir }}"
key: "${{ steps.extcache.outputs.key }}"
Expand All @@ -63,7 +63,7 @@ jobs:
run: 'echo "::set-output name=dir::$(composer config cache-files-dir)"'

- name: "Cache PHP dependencies"
uses: "actions/cache@v2"
uses: "actions/cache@v3"
with:
path: "${{ steps.composercache.outputs.dir }}"
key: "${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}"
Expand All @@ -90,7 +90,7 @@ jobs:

steps:
- name: "Checkout"
uses: "actions/checkout@v2"
uses: "actions/checkout@v3"

- name: "Setup PHP cache environment"
id: "extcache"
Expand All @@ -101,7 +101,7 @@ jobs:
key: "${{ env.cache-version }}"

- name: "Cache PHP extensions"
uses: "actions/cache@v2"
uses: "actions/cache@v3"
with:
path: "${{ steps.extcache.outputs.dir }}"
key: "${{ steps.extcache.outputs.key }}"
Expand All @@ -123,7 +123,7 @@ jobs:
run: 'echo "::set-output name=dir::$(composer config cache-files-dir)"'

- name: "Cache PHP dependencies"
uses: "actions/cache@v2"
uses: "actions/cache@v3"
with:
path: "${{ steps.composercache.outputs.dir }}"
key: "${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}"
Expand All @@ -147,7 +147,7 @@ jobs:

steps:
- name: "Checkout"
uses: "actions/checkout@v2"
uses: "actions/checkout@v3"

- name: "Setup PHP cache environment"
id: "extcache"
Expand All @@ -158,7 +158,7 @@ jobs:
key: "${{ env.cache-version }}"

- name: "Cache PHP extensions"
uses: "actions/cache@v2"
uses: "actions/cache@v3"
with:
path: "${{ steps.extcache.outputs.dir }}"
key: "${{ steps.extcache.outputs.key }}"
Expand All @@ -180,7 +180,7 @@ jobs:
run: 'echo "::set-output name=dir::$(composer config cache-files-dir)"'

- name: "Cache PHP dependencies"
uses: "actions/cache@v2"
uses: "actions/cache@v3"
with:
path: "${{ steps.composercache.outputs.dir }}"
key: "${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}"
Expand Down Expand Up @@ -213,7 +213,7 @@ jobs:

steps:
- name: "Checkout"
uses: "actions/checkout@v2"
uses: "actions/checkout@v3"

- name: "Setup PHP cache environment"
id: "extcache"
Expand All @@ -224,7 +224,7 @@ jobs:
key: "${{ env.cache-version }}"

- name: "Cache PHP extensions"
uses: "actions/cache@v2"
uses: "actions/cache@v3"
with:
path: "${{ steps.extcache.outputs.dir }}"
key: "${{ steps.extcache.outputs.key }}"
Expand All @@ -245,7 +245,7 @@ jobs:
run: 'echo "::set-output name=dir::$(composer config cache-files-dir)"'

- name: "Cache PHP dependencies"
uses: "actions/cache@v2"
uses: "actions/cache@v3"
with:
path: "${{ steps.composercache.outputs.dir }}"
key: "${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}"
Expand Down
11 changes: 7 additions & 4 deletions bin/websockets-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

declare(strict_types = 1);

use Nette\Configurator;
use IPub\WebSockets;

$rootDir = getcwd();
Expand All @@ -33,18 +34,20 @@

require_once $libsDir . DIRECTORY_SEPARATOR . 'autoload.php';

$configurator = new Nette\Configurator;
$configurator = new Configurator;
$configurator->addParameters([
'appDir' => $appDir,
'wwwDir' => $wwwDir,
]);

//$configurator->setDebugMode(TRUE); // Debug mode HAVE TO BE enabled on production server
$configurator->enableDebugger($packagesDir . DIRECTORY_SEPARATOR . 'log');
$configurator->enableDebugger($rootDir . DIRECTORY_SEPARATOR . 'log');

$configurator->setTempDirectory($packagesDir . DIRECTORY_SEPARATOR . 'temp');
$configurator->setTempDirectory($rootDir . DIRECTORY_SEPARATOR . 'temp');
$configurator->addConfig(dirname(__DIR__) . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'config.neon');

$container = $configurator->createContainer();

$container->getByType(WebSockets\Server\Server::class)->run();
$server = $container->getByType(WebSockets\Server\Server::class);
$server->create();
$server->run();
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@
},

"config" : {
"sort-packages" : true
"sort-packages" : true,
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true,
"phpstan/extension-installer": true
}
},

"bin" : [
Expand Down
4 changes: 3 additions & 1 deletion docs/en/ExampleBootstrap.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ $configurator->addParameters([
$container = $configurator->createContainer();

// Run server
$container->getByType(WebSockets\Server\Server::class)->run();
$server = $container->getByType(WebSockets\Server\Server::class);
$server->create();
$server->run();
```

NOTE: This is only example, you have to change it according your server/application configuration.
2 changes: 2 additions & 0 deletions docs/en/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ IPub\WebSockets\Server\Wrapper::onClientDisconnected(IPub\WebSockets\Entities\Cl
IPub\WebSockets\Server\Wrapper::onIncomingMessage(IPub\WebSockets\Entities\Clients\IClient $client, IPub\WebSockets\Http\IRequest $httpRequest, string $message)
IPub\WebSockets\Server\Wrapper::onAfterIncomingMessage(IPub\WebSockets\Entities\Clients\IClient $client, IPub\WebSockets\Http\IRequest $httpRequest)
IPub\WebSockets\Server\Wrapper::onClientError(IPub\WebSockets\Entities\Clients\IClient $client, IPub\WebSockets\Http\IRequest $httpRequest)
IPub\WebSockets\Server\Server::onCreate(IPub\WebSockets\Server\Server $server)
IPub\WebSockets\Server\Server::onStart(React\EventLoop\LoopInterface $eventLoop, IPub\WebSockets\Server\Server $server)
IPub\WebSockets\Server\Server::onStop(React\EventLoop\LoopInterface $eventLoop, IPub\WebSockets\Server\Server $server)
```

Where:
Expand Down
4 changes: 3 additions & 1 deletion docs/en/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ $configurator = new \Nette\Configurator;

$container = $configurator->createContainer();

$container->getByType(\IPub\WebSockets\Server\Server::class)->run();
$server = $container->getByType(\IPub\WebSockets\Server\Server::class);
$server->create();
$server->run();
```

More info in [example](https://github.com/ipublikuj/websockets/blob/master/docs/en/ExampleBootstrap.md)
Expand Down
1 change: 1 addition & 0 deletions src/Commands/ServerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ protected function execute(Input\InputInterface $input, Output\OutputInterface $
}

try {
$this->server->create();
$this->server->run();

} catch (Exceptions\TerminateException $ex) {
Expand Down
33 changes: 21 additions & 12 deletions src/DI/WebSocketsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
use React;
use Symfony\Component\EventDispatcher;

if (!class_exists('Nette\PhpGenerator\Literal')) {
class_alias('Nette\PhpGenerator\PhpLiteral', 'Nette\PhpGenerator\Literal');
}

/**
* WebSockets extension container
*
Expand Down Expand Up @@ -251,7 +255,7 @@ public function beforeCompile(): void
}

foreach ($allControllers as $def) {
$def->addTag(Nette\DI\Extensions\InjectExtension::TAG_INJECT)
$def->addTag('nette.inject')
->addTag('ipub.websockets.controller', $def->getType());
}

Expand All @@ -271,36 +275,41 @@ interface_exists('Symfony\Component\EventDispatcher\EventDispatcherInterface')
$application->addSetup('?->onOpen[] = function() {?->dispatch(new ?(...func_get_args()));}', [
'@self',
$dispatcher,
new Nette\PhpGenerator\PhpLiteral(Events\Application\OpenEvent::class),
new Nette\PhpGenerator\Literal(Events\Application\OpenEvent::class),
]);
$application->addSetup('?->onClose[] = function() {?->dispatch(new ?(...func_get_args()));}', [
'@self',
$dispatcher,
new Nette\PhpGenerator\PhpLiteral(Events\Application\CloseEvent::class),
new Nette\PhpGenerator\Literal(Events\Application\CloseEvent::class),
]);
$application->addSetup('?->onMessage[] = function() {?->dispatch(new ?(...func_get_args()));}', [
'@self',
$dispatcher,
new Nette\PhpGenerator\PhpLiteral(Events\Application\MessageEvent::class),
new Nette\PhpGenerator\Literal(Events\Application\MessageEvent::class),
]);
$application->addSetup('?->onError[] = function() {?->dispatch(new ?(...func_get_args()));}', [
'@self',
$dispatcher,
new Nette\PhpGenerator\PhpLiteral(Events\Application\ErrorEvent::class),
new Nette\PhpGenerator\Literal(Events\Application\ErrorEvent::class),
]);

$server = $builder->getDefinition($builder->getByType(Server\Server::class));
assert($server instanceof DI\Definitions\ServiceDefinition);

$server->addSetup('?->onCreate[] = function() {?->dispatch(new ?(...func_get_args()));}', [
'@self',
$dispatcher,
new Nette\PhpGenerator\Literal(Events\Server\CreateEvent::class),
]);
$server->addSetup('?->onStart[] = function() {?->dispatch(new ?(...func_get_args()));}', [
'@self',
$dispatcher,
new Nette\PhpGenerator\PhpLiteral(Events\Server\StartEvent::class),
new Nette\PhpGenerator\Literal(Events\Server\StartEvent::class),
]);
$server->addSetup('?->onStop[] = function() {?->dispatch(new ?(...func_get_args()));}', [
'@self',
$dispatcher,
new Nette\PhpGenerator\PhpLiteral(Events\Server\StopEvent::class),
new Nette\PhpGenerator\Literal(Events\Server\StopEvent::class),
]);

$serverWrapper = $builder->getDefinition($builder->getByType(Server\Wrapper::class));
Expand All @@ -309,27 +318,27 @@ interface_exists('Symfony\Component\EventDispatcher\EventDispatcherInterface')
$serverWrapper->addSetup('?->onClientConnected[] = function() {?->dispatch(new ?(...func_get_args()));}', [
'@self',
$dispatcher,
new Nette\PhpGenerator\PhpLiteral(Events\Wrapper\ClientConnectEvent::class),
new Nette\PhpGenerator\Literal(Events\Wrapper\ClientConnectEvent::class),
]);
$serverWrapper->addSetup('?->onClientDisconnected[] = function() {?->dispatch(new ?(...func_get_args()));}', [
'@self',
$dispatcher,
new Nette\PhpGenerator\PhpLiteral(Events\Wrapper\ClientDisconnectEvent::class),
new Nette\PhpGenerator\Literal(Events\Wrapper\ClientDisconnectEvent::class),
]);
$serverWrapper->addSetup('?->onClientError[] = function() {?->dispatch(new ?(...func_get_args()));}', [
'@self',
$dispatcher,
new Nette\PhpGenerator\PhpLiteral(Events\Wrapper\ClientErrorEvent::class),
new Nette\PhpGenerator\Literal(Events\Wrapper\ClientErrorEvent::class),
]);
$serverWrapper->addSetup('?->onIncomingMessage[] = function() {?->dispatch(new ?(...func_get_args()));}', [
'@self',
$dispatcher,
new Nette\PhpGenerator\PhpLiteral(Events\Wrapper\IncommingMessageEvent::class),
new Nette\PhpGenerator\Literal(Events\Wrapper\IncommingMessageEvent::class),
]);
$serverWrapper->addSetup('?->onAfterIncomingMessage[] = function() {?->dispatch(new ?(...func_get_args()));}', [
'@self',
$dispatcher,
new Nette\PhpGenerator\PhpLiteral(Events\Wrapper\AfterIncommingMessageEvent::class),
new Nette\PhpGenerator\Literal(Events\Wrapper\AfterIncommingMessageEvent::class),
]);
}
}
Expand Down
52 changes: 52 additions & 0 deletions src/Events/Server/CreateEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php declare(strict_types = 1);

/**
* StartEvent.php
*
* @copyright More in LICENSE.md
* @license https://www.ipublikuj.eu
* @author Adam Kadlec <adam.kadlec@ipublikuj.eu>
* @package iPublikuj:WebSockets!
* @subpackage Events
* @since 1.0.0
*
* @date 15.11.19
*/

namespace IPub\WebSockets\Events\Server;

use IPub\WebSockets\Server;
use Symfony\Contracts\EventDispatcher;

/**
* Server start event
*
* @package iPublikuj:WebSockets!
* @subpackage Events
*
* @author Adam Kadlec <adam.kadlec@ipublikuj.eu>
*/
final class CreateEvent extends EventDispatcher\Event
{

/** @var Server\Server */
private $server;

/**
* @param Server\Server $server
*/
public function __construct(
Server\Server $server
) {
$this->server = $server;
}

/**
* @return Server\Server
*/
public function getServer(): Server\Server
{
return $this->server;
}

}
Loading

0 comments on commit 1db1a3c

Please sign in to comment.