Skip to content

Commit acb9fde

Browse files
authored
Added PHP8 support (#7)
- Fixed event configuration in testing
1 parent 90f046f commit acb9fde

17 files changed

+99
-79
lines changed

.circleci/config.yml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,26 @@ jobs:
1515
command: |
1616
composer update -n --prefer-dist --no-suggest
1717
rm -Rf var/*
18-
php vendor/bin/phpunit
18+
sh .circleci/start.sh
1919
20+
test-php80:
21+
docker:
22+
- image: circleci/php:8.0-cli
23+
- image: rabbitmq
24+
25+
working_directory: ~/project
26+
steps:
27+
- checkout
28+
29+
- run:
30+
name: Run tests / Symfony 5^0
31+
command: |
32+
composer update -n --prefer-dist --no-suggest
33+
rm -Rf var/*
34+
sh .circleci/start.sh
2035
workflows:
2136
version: 2
2237
test:
2338
jobs:
24-
- test-php74
39+
- test-php74
40+
- test-php80

.circleci/start.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
3+
while ! nc -z localhost 5672; do sleep 1; done
4+
php vendor/bin/phpunit

.php_cs.cache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"php":"7.4.3","version":"2.16.1","indent":" ","lineEnding":"\n","rules":{"blank_line_after_namespace":true,"braces":{"allow_single_line_closure":true},"class_definition":{"single_line":true},"constant_case":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":true,"no_break_comment":true,"no_closing_tag":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":true,"single_import_per_statement":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"visibility_required":true,"encoding":true,"full_opening_tag":true,"array_syntax":{"syntax":"short"},"binary_operator_spaces":true,"blank_line_after_opening_tag":true,"blank_line_before_statement":{"statements":["return"]},"cast_spaces":true,"class_attributes_separation":{"elements":["method"]},"concat_space":true,"declare_equal_normalize":true,"function_typehint_space":true,"include":true,"increment_style":true,"lowercase_cast":true,"lowercase_static_reference":true,"magic_constant_casing":true,"magic_method_casing":true,"native_function_casing":true,"native_function_type_declaration_casing":true,"new_with_braces":true,"no_blank_lines_after_class_opening":true,"no_blank_lines_after_phpdoc":true,"no_empty_comment":true,"no_empty_phpdoc":true,"no_empty_statement":true,"no_extra_blank_lines":{"tokens":["curly_brace_block","extra","parenthesis_brace_block","square_brace_block","throw","use"]},"no_leading_import_slash":true,"no_leading_namespace_whitespace":true,"no_mixed_echo_print":true,"no_multiline_whitespace_around_double_arrow":true,"no_short_bool_cast":true,"no_singleline_whitespace_before_semicolons":true,"no_spaces_around_offset":true,"no_trailing_comma_in_list_call":true,"no_trailing_comma_in_singleline_array":true,"no_unneeded_control_parentheses":true,"no_unneeded_curly_braces":true,"no_unneeded_final_method":true,"no_unused_imports":true,"no_whitespace_before_comma_in_array":true,"no_whitespace_in_blank_line":true,"normalize_index_brace":true,"object_operator_without_whitespace":true,"ordered_imports":true,"php_unit_fqcn_annotation":true,"phpdoc_align":{"tags":["method","param","property","return","throws","type","var"]},"phpdoc_annotation_without_dot":true,"phpdoc_indent":true,"phpdoc_inline_tag":true,"phpdoc_no_access":true,"phpdoc_no_alias_tag":true,"phpdoc_no_package":true,"phpdoc_no_useless_inheritdoc":true,"phpdoc_return_self_reference":true,"phpdoc_scalar":true,"phpdoc_separation":true,"phpdoc_single_line_var_spacing":true,"phpdoc_summary":true,"phpdoc_to_comment":true,"phpdoc_trim":true,"phpdoc_trim_consecutive_blank_line_separation":true,"phpdoc_types":true,"phpdoc_types_order":{"null_adjustment":"always_last","sort_algorithm":"none"},"phpdoc_var_without_name":true,"return_type_declaration":true,"semicolon_after_instruction":true,"short_scalar_cast":true,"single_blank_line_before_namespace":true,"single_line_comment_style":{"comment_types":["hash"]},"single_line_throw":true,"single_quote":true,"single_trait_insert_per_statement":true,"space_after_semicolon":{"remove_in_empty_for_expressions":true},"standardize_increment":true,"standardize_not_equals":true,"ternary_operator_spaces":true,"trailing_comma_in_multiline_array":true,"trim_array_spaces":true,"unary_operator_spaces":true,"whitespace_after_comma_in_array":true,"yoda_style":true},"hashes":{"Connection\/WebsocketApps.php":2317183475,"Connection\/Connections.php":84316399,"Connection\/WebsocketServer.php":3342442655,"Connection\/WebsocketApp.php":2545711206,"DependencyInjection\/WebsocketConfiguration.php":2953772139,"DependencyInjection\/CompilerPass\/WebsocketCompilerPass.php":1543538877,"DependencyInjection\/WebsocketExtension.php":1899471205,"WebsocketBundle.php":1472971440,"Event\/WebsocketConnectionClosed.php":2222190510,"Event\/WebsocketMessageReceived.php":3197828197,"Event\/WebsocketConnectionError.php":3011051078,"Event\/WebsocketConnectionOpened.php":695964194,"Event\/WebsocketEvent.php":2053024357,"Tests\/RouteFunctionalTest.php":2473681876,"Tests\/WebsocketFunctionalTest.php":3469799083,"Tests\/WebsocketEventSubscriber.php":2248726942,"Tests\/TestEvent.php":4131591015,"Console\/WebsocketHeaderPrinter.php":3710664822,"Console\/ConnectToWebsocket.php":2680932720,"Console\/RunWebsocket.php":3757557141,"Console\/ConsoleWebsocketMessage.php":2669861452}}
1+
{"php":"7.4.15","version":"2.16.1","indent":" ","lineEnding":"\n","rules":{"blank_line_after_namespace":true,"braces":{"allow_single_line_closure":true},"class_definition":{"single_line":true},"constant_case":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":true,"no_break_comment":true,"no_closing_tag":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":true,"single_import_per_statement":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"visibility_required":true,"encoding":true,"full_opening_tag":true,"array_syntax":{"syntax":"short"},"binary_operator_spaces":true,"blank_line_after_opening_tag":true,"blank_line_before_statement":{"statements":["return"]},"cast_spaces":true,"class_attributes_separation":{"elements":["method"]},"concat_space":true,"declare_equal_normalize":true,"function_typehint_space":true,"include":true,"increment_style":true,"lowercase_cast":true,"lowercase_static_reference":true,"magic_constant_casing":true,"magic_method_casing":true,"native_function_casing":true,"native_function_type_declaration_casing":true,"new_with_braces":true,"no_blank_lines_after_class_opening":true,"no_blank_lines_after_phpdoc":true,"no_empty_comment":true,"no_empty_phpdoc":true,"no_empty_statement":true,"no_extra_blank_lines":{"tokens":["curly_brace_block","extra","parenthesis_brace_block","square_brace_block","throw","use"]},"no_leading_import_slash":true,"no_leading_namespace_whitespace":true,"no_mixed_echo_print":true,"no_multiline_whitespace_around_double_arrow":true,"no_short_bool_cast":true,"no_singleline_whitespace_before_semicolons":true,"no_spaces_around_offset":true,"no_trailing_comma_in_list_call":true,"no_trailing_comma_in_singleline_array":true,"no_unneeded_control_parentheses":true,"no_unneeded_curly_braces":true,"no_unneeded_final_method":true,"no_unused_imports":true,"no_whitespace_before_comma_in_array":true,"no_whitespace_in_blank_line":true,"normalize_index_brace":true,"object_operator_without_whitespace":true,"ordered_imports":true,"php_unit_fqcn_annotation":true,"phpdoc_align":{"tags":["method","param","property","return","throws","type","var"]},"phpdoc_annotation_without_dot":true,"phpdoc_indent":true,"phpdoc_inline_tag":true,"phpdoc_no_access":true,"phpdoc_no_alias_tag":true,"phpdoc_no_package":true,"phpdoc_no_useless_inheritdoc":true,"phpdoc_return_self_reference":true,"phpdoc_scalar":true,"phpdoc_separation":true,"phpdoc_single_line_var_spacing":true,"phpdoc_summary":true,"phpdoc_to_comment":true,"phpdoc_trim":true,"phpdoc_trim_consecutive_blank_line_separation":true,"phpdoc_types":true,"phpdoc_types_order":{"null_adjustment":"always_last","sort_algorithm":"none"},"phpdoc_var_without_name":true,"return_type_declaration":true,"semicolon_after_instruction":true,"short_scalar_cast":true,"single_blank_line_before_namespace":true,"single_line_comment_style":{"comment_types":["hash"]},"single_line_throw":true,"single_quote":true,"single_trait_insert_per_statement":true,"space_after_semicolon":{"remove_in_empty_for_expressions":true},"standardize_increment":true,"standardize_not_equals":true,"ternary_operator_spaces":true,"trailing_comma_in_multiline_array":true,"trim_array_spaces":true,"unary_operator_spaces":true,"whitespace_after_comma_in_array":true,"yoda_style":true},"hashes":{"Connection\/WebsocketApps.php":4157201624,"Connection\/Connections.php":1889335883,"Connection\/WebsocketServer.php":103965827,"Connection\/WebsocketApp.php":3711384802,"Connection\/Connection.php":1455972305,"Tests\/RouteFunctionalTest.php":3650438628,"Tests\/WebsocketFunctionalTest.php":2593048220,"Tests\/WebsocketEventSubscriber.php":956909013,"Tests\/TestEvent.php":4131591015,"DependencyInjection\/WebsocketConfiguration.php":2953772139,"DependencyInjection\/CompilerPass\/WebsocketCompilerPass.php":1543538877,"DependencyInjection\/WebsocketExtension.php":1899471205,"WebsocketBundle.php":1472971440,"Console\/ConsoleWebsocketMessage.php":4073404464,"Console\/WebsocketHeaderPrinter.php":3577760015,"Console\/ConnectToWebsocket.php":1389340276,"Console\/RunWebsocket.php":2245222042,"Event\/WebsocketConnectionClosed.php":2222190510,"Event\/WebsocketMessageReceived.php":3197828197,"Event\/WebsocketConnectionError.php":3371276194,"Event\/WebsocketConnectionOpened.php":695964194,"Event\/WebsocketEvent.php":2053024357}}

.phpunit.result.cache

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"version":1,"defects":{"Drift\\Websocket\\Tests\\RouteFunctionalTest::testRouteConnection":3},"times":{"Drift\\Websocket\\Tests\\RouteFunctionalTest::testRouteConnection":7.141}}

Connection/Connection.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,36 @@
11
<?php
22

3+
/*
4+
* This file is part of the DriftPHP Project
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*
9+
* Feel free to edit as you please, and have fun.
10+
*
11+
* @author Marc Morera <[email protected]>
12+
*/
13+
14+
declare(strict_types=1);
315

416
namespace Drift\Websocket\Connection;
517

618
use Ratchet\ConnectionInterface;
719

820
/**
9-
* Class Connection
21+
* Class Connection.
1022
*/
1123
class Connection
1224
{
1325
/**
14-
* Get connection hash
26+
* Get connection hash.
1527
*
1628
* @param ConnectionInterface $connection
1729
*
1830
* @return string
1931
*/
20-
public static function getConnectionHash(ConnectionInterface $connection) : string
32+
public static function getConnectionHash(ConnectionInterface $connection): string
2133
{
2234
return substr(md5(spl_object_hash($connection)), 0, 13);
2335
}
24-
}
36+
}

Connection/Connections.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@
2323
*/
2424
class Connections
2525
{
26-
/**
27-
* @var SplObjectStorage
28-
*/
29-
private $connections;
26+
private SplObjectStorage $connections;
3027

3128
/**
3229
* Connections constructor.

Connection/WebsocketApp.php

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,11 @@
3131
*/
3232
class WebsocketApp implements MessageComponentInterface
3333
{
34-
/**
35-
* @var string
36-
*/
37-
private $name;
38-
39-
/**
40-
* @var Connections
41-
*/
42-
private $connections;
43-
44-
/**
45-
* @var InlineEventBus
46-
*/
47-
private $eventBus;
48-
49-
/**
50-
* @var OutputPrinter
51-
*/
52-
private $outputPrinter;
34+
private string $name;
35+
private Connections $connections;
36+
private InlineEventBus $eventBus;
37+
private OutputPrinter $outputPrinter;
38+
private bool $broadcast = false;
5339

5440
/**
5541
* WebsocketsApp constructor.
@@ -76,6 +62,11 @@ public function setOutputPrinter(OutputPrinter $outputPrinter): void
7662
$this->outputPrinter = $outputPrinter;
7763
}
7864

65+
public function broadcast(): void
66+
{
67+
$this->broadcast = true;
68+
}
69+
7970
/**
8071
* {@inheritdoc}
8172
*/
@@ -136,6 +127,7 @@ public function onMessage(ConnectionInterface $from, $message)
136127
{
137128
$event = new WebsocketMessageReceived($this->name, $this->connections, $from, $message);
138129
$this->eventBus->dispatch($event);
130+
$message = trim($message, " \ \t\n\r\0\x0B");
139131

140132
if ($this->outputPrinter) {
141133
(new ConsoleWebsocketMessage(sprintf(
@@ -144,6 +136,10 @@ public function onMessage(ConnectionInterface $from, $message)
144136
$this->name,
145137
trim($message, " \ \t\n\r\0\x0B")
146138
), '~', true))->print($this->outputPrinter);
139+
140+
if ($this->broadcast) {
141+
$this->connections->broadcast($message, $from);
142+
}
147143
}
148144
}
149145
}

Connection/WebsocketApps.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@
2020
*/
2121
class WebsocketApps
2222
{
23-
/**
24-
* @var array
25-
*/
26-
private $apps = [];
23+
private array $apps = [];
2724

2825
/**
2926
* Add app.

Connection/WebsocketServer.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,8 @@
2424
*/
2525
class WebsocketServer
2626
{
27-
/**
28-
* @var LoopInterface
29-
*/
30-
private $loop;
31-
32-
/**
33-
* @var WebsocketApps
34-
*/
35-
private $apps;
27+
private LoopInterface $loop;
28+
private WebsocketApps $apps;
3629

3730
/**
3831
* WebsocketServer constructor.
@@ -53,13 +46,15 @@ public function __construct(LoopInterface $loop, WebsocketApps $apps)
5346
* @param int $port
5447
* @param OutputPrinter $outputPrinter
5548
* @param string[] $connectionsName
49+
* @param bool $broadcast
5650
* @param string $address
5751
*/
5852
public function createServer(
5953
string $httpHost,
6054
int $port,
6155
array $connectionsName,
6256
OutputPrinter $outputPrinter,
57+
bool $broadcast,
6358
string $address = '0.0.0.0'
6459
) {
6560
$server = new App($httpHost, $port, $address, $this->loop);
@@ -69,6 +64,10 @@ public function createServer(
6964

7065
foreach ($apps as list($app, $configuration)) {
7166
$app->setOutputPrinter($outputPrinter);
67+
if ($broadcast) {
68+
$app->broadcast();
69+
}
70+
7271
$server->route(
7372
$configuration['path'],
7473
$app,

Console/ConnectToWebsocket.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
use Symfony\Component\Console\Command\Command;
2424
use Symfony\Component\Console\Input\InputArgument;
2525
use Symfony\Component\Console\Input\InputInterface;
26-
use Symfony\Component\Console\Input\InputOption;
2726
use Symfony\Component\Console\Output\OutputInterface;
2827

2928
/**
@@ -35,11 +34,7 @@ class ConnectToWebsocket extends Command
3534
* @var string
3635
*/
3736
protected static $defaultName = 'websocket:connect';
38-
39-
/**
40-
* @var LoopInterface
41-
*/
42-
private $loop;
37+
private LoopInterface $loop;
4338

4439
/**
4540
* RunWebsocket constructor.
@@ -60,8 +55,7 @@ protected function configure()
6055
{
6156
$this
6257
->setDescription('Run the websocket')
63-
->addArgument('path', InputArgument::REQUIRED, 'The server will start listening to this address')
64-
->addOption('route', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'Routes to listen');
58+
->addArgument('path', InputArgument::REQUIRED, 'The server will start listening to this address');
6559
}
6660

6761
/**

0 commit comments

Comments
 (0)