WebSocket app

This commit is contained in:
2021-03-30 16:40:02 -03:00
parent 5d229739fe
commit 190dfd7c34
13 changed files with 314 additions and 0 deletions

45
ws/setup/app.php Normal file
View File

@ -0,0 +1,45 @@
<?php
use DI\ContainerBuilder as Builder;
include_once 'composer.php';
$builder = new Builder();
$folders = [
implode(DIRECTORY_SEPARATOR, [
dirname(__DIR__, 2),
'setup',
'env'
]),
implode(DIRECTORY_SEPARATOR, [
dirname(__DIR__, 2),
'setup',
'common'
]),
implode(DIRECTORY_SEPARATOR, [
__DIR__,
'ws'
])
];
$files = [
'settings',
'setups'
];
foreach ($files as $file) {
foreach ($folders as $folder) {
$filename = implode(DIRECTORY_SEPARATOR, [
$folder,
$file . '.php'
]);
if (!file_exists($filename)) {
continue;
}
$builder->addDefinitions($filename);
}
}
$container = $builder->build();
$app = $container->get(Ratchet\Server\IoServer::class);
include_once 'databases.php';
include_once 'router.php';

6
ws/setup/composer.php Normal file
View File

@ -0,0 +1,6 @@
<?php
include_once implode(DIRECTORY_SEPARATOR, [
dirname(__DIR__),
'vendor',
'autoload.php'
]);

36
ws/setup/databases.php Normal file
View File

@ -0,0 +1,36 @@
<?php
$databases = $app->getContainer()->get('databases');
foreach ($databases as $name => $settings) {
switch($settings->system) {
case 'mysql':
$dsn = implode(':', [
'mysql',
implode(';', [
implode('=', [
'host',
$settings->host->name
]),
implode('=', [
'dbname',
$settings->name
])
])
]);
if (isset($settings->host->port)) {
$dsn .= ';' . implode('=', [
'port',
$settings->host->port
]);
}
break;
}
ORM::configure($dsn, null, $name);
switch ($settings->system) {
case 'mysql':
ORM::configure('username', $settings->user->name, $name);
ORM::configure('password', $settings->user->password, $name);
}
}
if (isset($databases->short_names)) {
Model::$short_table_names = $databases->short_names;
}

5
ws/setup/router.php Normal file
View File

@ -0,0 +1,5 @@
<?php
include_once implode(DIRECTORY_SEPARATOR, [
$app->getContainer()->get('locations')->routes,
'ws.php'
]);

19
ws/setup/ws/settings.php Normal file
View File

@ -0,0 +1,19 @@
<?php
use Psr\Container\ContainerInterface as Container;
return [
'locations' => DI\decorate(function($prev, Container $c) {
$arr = (array) $prev;
$arr['base'] = dirname(__DIR__, 2);
$arr['resources'] = implode(DIRECTORY_SEPARATOR, [
$arr['base'],
'resources'
]);
$arr['routes'] = implode(DIRECTORY_SEPARATOR, [
$arr['resources'],
'routes'
]);
return (object) $arr;
}),
'port' => '9010'
];

36
ws/setup/ws/setups.php Normal file
View File

@ -0,0 +1,36 @@
<?php
use Psr\Container\ContainerInterface as Container;
return [
'storage' => function(Container $c) {
return new \SplObjectStorage();
},
ProVM\Common\Factory\Event\Request::class => function(Container $c) {
return new ProVM\Common\Factory\Event\Request();
},
ProVM\Common\Factory\Event\Response::class => function(Container $c) {
return new ProVM\Common\Factory\Event\Response();
},
ProVM\Common\Factory\Event\Listener::class => function(Container $c) {
return new ProVM\Common\Factory\Event\Listener($c);
},
Psr\EventDispatcher\EventDispatcherInterface::class => function(Container $c) {
return new ProVM\Common\Service\Event\Dispatcher($c);
},
Ratchet\MessageComponentInterface::class => function(Container $c) {
return (new ProVM\Common\Alias\Event\Message($c->get('storage')))
->setDispatcher($c->get(Psr\EventDispatcher\EventDispatcherInterface::class))
->setRequestBuilder($c->get(ProVM\Common\Factory\Event\Request::class));
},
Ratchet\WebSocket\WsServer::class => function(Container $c) {
return new Ratchet\WebSocket\WsServer($c->get(Ratchet\MessageComponentInterface::class));
},
Ratchet\Http\HttpServer::class => function(Container $c) {
return new Ratchet\Http\HttpServer($c->get(Ratchet\WebSocket\WsServer::class));
},
Ratchet\Server\IoServer::class => function(Container $c) {
$app = ProVM\Common\Alias\Server\App::factory($c->get(Ratchet\Http\HttpServer::class), $c->get('port'));
$app->setContainer($c);
return $app;
}
];