@@ -23,7 +23,8 @@ discard block |
||
| 23 | 23 | public function setUp(): void |
| 24 | 24 | { |
| 25 | 25 | exec('protoc 2>&1', $out); |
| 26 | - if (strpos(join("\n", $out), '--php_out') === false) { |
|
| 26 | + if (strpos(join("\n", $out), '--php_out') === false) |
|
| 27 | + { |
|
| 27 | 28 | $this->markTestSkipped('Protoc binary is missing'); |
| 28 | 29 | } |
| 29 | 30 | |
@@ -48,11 +49,13 @@ discard block |
||
| 48 | 49 | |
| 49 | 50 | $fs = new Files(); |
| 50 | 51 | |
| 51 | - if ($fs->isDirectory($this->app->dir('app') . 'src/Service')) { |
|
| 52 | + if ($fs->isDirectory($this->app->dir('app') . 'src/Service')) |
|
| 53 | + { |
|
| 52 | 54 | $fs->deleteDirectory($this->app->dir('app') . 'src/Service'); |
| 53 | 55 | } |
| 54 | 56 | |
| 55 | - if ($fs->isDirectory($this->app->dir('app') . 'src/GPBMetadata')) { |
|
| 57 | + if ($fs->isDirectory($this->app->dir('app') . 'src/GPBMetadata')) |
|
| 58 | + { |
|
| 56 | 59 | $fs->deleteDirectory($this->app->dir('app') . 'src/GPBMetadata'); |
| 57 | 60 | } |
| 58 | 61 | } |
@@ -79,7 +82,8 @@ discard block |
||
| 79 | 82 | $msg->setMsg('hello'); |
| 80 | 83 | |
| 81 | 84 | $w->shouldReceive('receive')->once()->with( |
| 82 | - \Mockery::on(function (&$context) { |
|
| 85 | + \Mockery::on(function (&$context) |
|
| 86 | + { |
|
| 83 | 87 | $context = '{ |
| 84 | 88 | "service": "service.Echo", |
| 85 | 89 | "method": "Ping", |
@@ -91,7 +95,8 @@ discard block |
||
| 91 | 95 | )->andReturn($msg->serializeToString()); |
| 92 | 96 | |
| 93 | 97 | $w->shouldReceive('send')->once()->with( |
| 94 | - \Mockery::on(function ($out) { |
|
| 98 | + \Mockery::on(function ($out) |
|
| 99 | + { |
|
| 95 | 100 | $msg = new Message(); |
| 96 | 101 | $msg->mergeFromString($out); |
| 97 | 102 | $this->assertSame('hello', $msg->getMsg()); |
@@ -103,7 +108,8 @@ discard block |
||
| 103 | 108 | |
| 104 | 109 | // one command only |
| 105 | 110 | $w->shouldReceive('receive')->once()->with( |
| 106 | - \Mockery::on(function (&$context) { |
|
| 111 | + \Mockery::on(function (&$context) |
|
| 112 | + { |
|
| 107 | 113 | $context = null; |
| 108 | 114 | return true; |
| 109 | 115 | }) |
@@ -123,7 +129,8 @@ discard block |
||
| 123 | 129 | $msg->setMsg('hello'); |
| 124 | 130 | |
| 125 | 131 | $w->shouldReceive('receive')->once()->with( |
| 126 | - \Mockery::on(function (&$context) { |
|
| 132 | + \Mockery::on(function (&$context) |
|
| 133 | + { |
|
| 127 | 134 | $context = '{ |
| 128 | 135 | "service": "service.Echo", |
| 129 | 136 | "method": "Invalid", |
@@ -135,7 +142,8 @@ discard block |
||
| 135 | 142 | )->andReturn($msg->serializeToString()); |
| 136 | 143 | |
| 137 | 144 | $w->shouldReceive('error')->once()->with( |
| 138 | - \Mockery::on(function ($out) { |
|
| 145 | + \Mockery::on(function ($out) |
|
| 146 | + { |
|
| 139 | 147 | $this->assertContains('Method `Invalid` not found', $out); |
| 140 | 148 | return true; |
| 141 | 149 | }) |
@@ -143,7 +151,8 @@ discard block |
||
| 143 | 151 | |
| 144 | 152 | // one command only |
| 145 | 153 | $w->shouldReceive('receive')->once()->with( |
| 146 | - \Mockery::on(function (&$context) { |
|
| 154 | + \Mockery::on(function (&$context) |
|
| 155 | + { |
|
| 147 | 156 | $context = null; |
| 148 | 157 | return true; |
| 149 | 158 | }) |
@@ -21,17 +21,20 @@ |
||
| 21 | 21 | |
| 22 | 22 | public function boot(WebsocketsBootloader $ws, EnvironmentInterface $env): void |
| 23 | 23 | { |
| 24 | - if ($env->get('RR_BROADCAST_PATH') === null) { |
|
| 24 | + if ($env->get('RR_BROADCAST_PATH') === null) |
|
| 25 | + { |
|
| 25 | 26 | return; |
| 26 | 27 | } |
| 27 | 28 | |
| 28 | 29 | $ws->setServerCallback($env->get('WS_SERVER_CALLBACK')); |
| 29 | 30 | |
| 30 | - if ($env->get('WS_TOPIC_CALLBACK') !== null) { |
|
| 31 | + if ($env->get('WS_TOPIC_CALLBACK') !== null) |
|
| 32 | + { |
|
| 31 | 33 | $ws->addTopicCallback('topic', $env->get('WS_TOPIC_CALLBACK')); |
| 32 | 34 | } |
| 33 | 35 | |
| 34 | - if ($env->get('WS_TOPIC_WILDCARD_CALLBACK') !== null) { |
|
| 36 | + if ($env->get('WS_TOPIC_WILDCARD_CALLBACK') !== null) |
|
| 37 | + { |
|
| 35 | 38 | $ws->addTopicCallback('wildcard.{id}', $env->get('WS_TOPIC_WILDCARD_CALLBACK')); |
| 36 | 39 | } |
| 37 | 40 | } |
@@ -53,7 +53,8 @@ |
||
| 53 | 53 | 'authorizeTopics' => [] |
| 54 | 54 | ]); |
| 55 | 55 | |
| 56 | - if ($env->get('RR_BROADCAST_PATH', null) !== null) { |
|
| 56 | + if ($env->get('RR_BROADCAST_PATH', null) !== null) |
|
| 57 | + { |
|
| 57 | 58 | $http->addMiddleware(WebsocketsMiddleware::class); |
| 58 | 59 | } |
| 59 | 60 | } |
@@ -38,7 +38,8 @@ discard block |
||
| 38 | 38 | |
| 39 | 39 | public function testAllowServerAccess(): void |
| 40 | 40 | { |
| 41 | - $this->init(static function () { |
|
| 41 | + $this->init(static function () |
|
| 42 | + { |
|
| 42 | 43 | return true; |
| 43 | 44 | }); |
| 44 | 45 | |
@@ -50,7 +51,8 @@ discard block |
||
| 50 | 51 | |
| 51 | 52 | public function testDenyServerAccess(): void |
| 52 | 53 | { |
| 53 | - $this->init(static function () { |
|
| 54 | + $this->init(static function () |
|
| 55 | + { |
|
| 54 | 56 | return false; |
| 55 | 57 | }); |
| 56 | 58 | |
@@ -62,7 +64,8 @@ discard block |
||
| 62 | 64 | |
| 63 | 65 | public function testServerAccessWithDependency(): void |
| 64 | 66 | { |
| 65 | - $this->init(static function (ServerRequestInterface $request) { |
|
| 67 | + $this->init(static function (ServerRequestInterface $request) |
|
| 68 | + { |
|
| 66 | 69 | return $request instanceof ServerRequestInterface; |
| 67 | 70 | }); |
| 68 | 71 | |
@@ -124,7 +127,8 @@ discard block |
||
| 124 | 127 | |
| 125 | 128 | public function testTopicAccessOK(): void |
| 126 | 129 | { |
| 127 | - $this->init(null, function () { |
|
| 130 | + $this->init(null, function () |
|
| 131 | + { |
|
| 128 | 132 | return true; |
| 129 | 133 | }); |
| 130 | 134 | |
@@ -136,7 +140,8 @@ discard block |
||
| 136 | 140 | |
| 137 | 141 | public function testTopicWildcardAccessFail(): void |
| 138 | 142 | { |
| 139 | - $this->init(null, function () { |
|
| 143 | + $this->init(null, function () |
|
| 144 | + { |
|
| 140 | 145 | return true; |
| 141 | 146 | }); |
| 142 | 147 | |
@@ -151,7 +156,8 @@ discard block |
||
| 151 | 156 | $this->init( |
| 152 | 157 | null, |
| 153 | 158 | null, |
| 154 | - function () { |
|
| 159 | + function () |
|
| 160 | + { |
|
| 155 | 161 | return true; |
| 156 | 162 | } |
| 157 | 163 | ); |
@@ -167,7 +173,8 @@ discard block |
||
| 167 | 173 | $this->init( |
| 168 | 174 | null, |
| 169 | 175 | null, |
| 170 | - function ($id) { |
|
| 176 | + function ($id) |
|
| 177 | + { |
|
| 171 | 178 | return $id === '1'; |
| 172 | 179 | } |
| 173 | 180 | ); |
@@ -183,7 +190,8 @@ discard block |
||
| 183 | 190 | $this->init( |
| 184 | 191 | null, |
| 185 | 192 | null, |
| 186 | - function ($id) { |
|
| 193 | + function ($id) |
|
| 194 | + { |
|
| 187 | 195 | return $id === '1'; |
| 188 | 196 | } |
| 189 | 197 | ); |
@@ -204,7 +212,8 @@ discard block |
||
| 204 | 212 | 'CYCLE_AUTH' => true, |
| 205 | 213 | 'RR_BROADCAST_PATH' => '/ws/', |
| 206 | 214 | 'WS_SERVER_CALLBACK' => null, |
| 207 | - 'WS_TOPIC_CALLBACK' => function (AuthContextInterface $authContext) { |
|
| 215 | + 'WS_TOPIC_CALLBACK' => function (AuthContextInterface $authContext) |
|
| 216 | + { |
|
| 208 | 217 | return $authContext->getToken() !== null; |
| 209 | 218 | }, |
| 210 | 219 | 'WS_TOPIC_WILDCARD_CALLBACK' => null, |
@@ -45,7 +45,8 @@ discard block |
||
| 45 | 45 | array $headers = [] |
| 46 | 46 | ): ResponseInterface { |
| 47 | 47 | $r = $this->request($uri, 'GET', [], $headers, []); |
| 48 | - foreach ($attributes as $k => $v) { |
|
| 48 | + foreach ($attributes as $k => $v) |
|
| 49 | + { |
|
| 49 | 50 | $r = $r->withAttribute($k, $v); |
| 50 | 51 | } |
| 51 | 52 | |
@@ -86,7 +87,8 @@ discard block |
||
| 86 | 87 | protected function fetchCookies(array $header) |
| 87 | 88 | { |
| 88 | 89 | $result = []; |
| 89 | - foreach ($header as $line) { |
|
| 90 | + foreach ($header as $line) |
|
| 91 | + { |
|
| 90 | 92 | $cookie = explode('=', $line); |
| 91 | 93 | $result[$cookie[0]] = rawurldecode(substr($cookie[1], 0, strpos($cookie[1], ';'))); |
| 92 | 94 | } |
@@ -67,13 +67,16 @@ discard block |
||
| 67 | 67 | */ |
| 68 | 68 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
| 69 | 69 | { |
| 70 | - if ($request->getUri()->getPath() !== $this->config->getPath()) { |
|
| 70 | + if ($request->getUri()->getPath() !== $this->config->getPath()) |
|
| 71 | + { |
|
| 71 | 72 | return $handler->handle($request); |
| 72 | 73 | } |
| 73 | 74 | |
| 74 | 75 | // server authorization |
| 75 | - if ($request->getAttribute('ws:joinServer', null) !== null) { |
|
| 76 | - if (!$this->authorizeServer($request)) { |
|
| 76 | + if ($request->getAttribute('ws:joinServer', null) !== null) |
|
| 77 | + { |
|
| 78 | + if (!$this->authorizeServer($request)) |
|
| 79 | + { |
|
| 77 | 80 | return $this->responseFactory->createResponse(403); |
| 78 | 81 | } |
| 79 | 82 | |
@@ -81,10 +84,13 @@ discard block |
||
| 81 | 84 | } |
| 82 | 85 | |
| 83 | 86 | // topic authorization |
| 84 | - if (is_string($request->getAttribute('ws:joinTopics', null))) { |
|
| 87 | + if (is_string($request->getAttribute('ws:joinTopics', null))) |
|
| 88 | + { |
|
| 85 | 89 | $topics = explode(',', $request->getAttribute('ws:joinTopics')); |
| 86 | - foreach ($topics as $topic) { |
|
| 87 | - if (!$this->authorizeTopic($request, $topic)) { |
|
| 90 | + foreach ($topics as $topic) |
|
| 91 | + { |
|
| 92 | + if (!$this->authorizeTopic($request, $topic)) |
|
| 93 | + { |
|
| 88 | 94 | return $this->responseFactory->createResponse(403); |
| 89 | 95 | } |
| 90 | 96 | } |
@@ -104,7 +110,8 @@ discard block |
||
| 104 | 110 | private function authorizeServer(ServerRequestInterface $request): bool |
| 105 | 111 | { |
| 106 | 112 | $callback = $this->config->getServerCallback(); |
| 107 | - if ($callback === null) { |
|
| 113 | + if ($callback === null) |
|
| 114 | + { |
|
| 108 | 115 | return true; |
| 109 | 116 | } |
| 110 | 117 | |
@@ -122,7 +129,8 @@ discard block |
||
| 122 | 129 | { |
| 123 | 130 | $parameters = []; |
| 124 | 131 | $callback = $this->config->findTopicCallback($topic, $parameters); |
| 125 | - if ($callback === null) { |
|
| 132 | + if ($callback === null) |
|
| 133 | + { |
|
| 126 | 134 | return false; |
| 127 | 135 | } |
| 128 | 136 | |
@@ -141,7 +149,8 @@ discard block |
||
| 141 | 149 | { |
| 142 | 150 | /** @var \ReflectionFunctionAbstract $reflection */ |
| 143 | 151 | $reflection = null; |
| 144 | - switch (true) { |
|
| 152 | + switch (true) |
|
| 153 | + { |
|
| 145 | 154 | case $callback instanceof \Closure || is_string($callback): |
| 146 | 155 | $reflection = new \ReflectionFunction($callback); |
| 147 | 156 | break; |
@@ -159,7 +168,8 @@ discard block |
||
| 159 | 168 | [ |
| 160 | 169 | ServerRequestInterface::class => $request |
| 161 | 170 | ], |
| 162 | - function () use ($reflection, $parameters, $callback) { |
|
| 171 | + function () use ($reflection, $parameters, $callback) |
|
| 172 | + { |
|
| 163 | 173 | return call_user_func_array( |
| 164 | 174 | $callback, |
| 165 | 175 | $this->resolver->resolveArguments($reflection, $parameters) |
@@ -34,7 +34,8 @@ discard block |
||
| 34 | 34 | { |
| 35 | 35 | parent::__construct($config); |
| 36 | 36 | |
| 37 | - foreach ($config['authorizeTopics'] as $topic => $callback) { |
|
| 37 | + foreach ($config['authorizeTopics'] as $topic => $callback) |
|
| 38 | + { |
|
| 38 | 39 | $this->patterns[$this->compilePattern($topic)] = $callback; |
| 39 | 40 | } |
| 40 | 41 | } |
@@ -62,8 +63,10 @@ discard block |
||
| 62 | 63 | */ |
| 63 | 64 | public function findTopicCallback(string $topic, array &$matches): ?callable |
| 64 | 65 | { |
| 65 | - foreach ($this->patterns as $pattern => $callback) { |
|
| 66 | - if (preg_match($pattern, $topic, $matches)) { |
|
| 66 | + foreach ($this->patterns as $pattern => $callback) |
|
| 67 | + { |
|
| 68 | + if (preg_match($pattern, $topic, $matches)) |
|
| 69 | + { |
|
| 67 | 70 | return $callback; |
| 68 | 71 | } |
| 69 | 72 | } |
@@ -78,9 +81,11 @@ discard block |
||
| 78 | 81 | private function compilePattern(string $topic): string |
| 79 | 82 | { |
| 80 | 83 | $replaces = []; |
| 81 | - if (preg_match_all('/\{(\w+):?(.*?)?\}/', $topic, $matches)) { |
|
| 84 | + if (preg_match_all('/\{(\w+):?(.*?)?\}/', $topic, $matches)) |
|
| 85 | + { |
|
| 82 | 86 | $variables = array_combine($matches[1], $matches[2]); |
| 83 | - foreach ($variables as $key => $segment) { |
|
| 87 | + foreach ($variables as $key => $segment) |
|
| 88 | + { |
|
| 84 | 89 | $replaces['{' . $key . '}'] = '(?P<' . $key . '>[^\/\.]+)'; |
| 85 | 90 | } |
| 86 | 91 | } |