diff --git a/src/Core/Application/AppContext.php b/src/Core/Application/AppContext.php index 5c618bd9..d4e34320 100644 --- a/src/Core/Application/AppContext.php +++ b/src/Core/Application/AppContext.php @@ -17,8 +17,8 @@ use Psr\Http\Message\UploadedFileInterface; use Psr\Http\Message\UriInterface; use Stringable; -use Windwalker\Core\Application\Context\RequestAppContextInterface; -use Windwalker\Core\Application\Context\RequestAppContextTrait; +use Windwalker\Core\Application\Context\AppContextInterface; +use Windwalker\Core\Application\Context\AppContextTrait; use Windwalker\Core\Http\AppRequest; use Windwalker\Core\Router\SystemUri; use Windwalker\Core\State\AppState; @@ -36,10 +36,10 @@ * @property-read AppState $state */ #[Immutable(Immutable::PROTECTED_WRITE_SCOPE)] -class AppContext implements WebApplicationInterface, RequestAppContextInterface +class AppContext implements WebApplicationInterface, AppContextInterface { use FilterAwareTrait; - use RequestAppContextTrait; + use AppContextTrait; protected ?AppRequest $appRequest = null; diff --git a/src/Core/Application/Context/RequestAppContextInterface.php b/src/Core/Application/Context/AppContextInterface.php similarity index 98% rename from src/Core/Application/Context/RequestAppContextInterface.php rename to src/Core/Application/Context/AppContextInterface.php index de562a5e..f81490ec 100644 --- a/src/Core/Application/Context/RequestAppContextInterface.php +++ b/src/Core/Application/Context/AppContextInterface.php @@ -23,7 +23,7 @@ /** * Interface RequestAppContextInterface */ -interface RequestAppContextInterface +interface AppContextInterface { public function getRootApp(): RootApplicationInterface; diff --git a/src/Core/Application/Context/RequestAppContextTrait.php b/src/Core/Application/Context/AppContextTrait.php similarity index 99% rename from src/Core/Application/Context/RequestAppContextTrait.php rename to src/Core/Application/Context/AppContextTrait.php index f36ef3fb..cdfd3529 100644 --- a/src/Core/Application/Context/RequestAppContextTrait.php +++ b/src/Core/Application/Context/AppContextTrait.php @@ -22,7 +22,7 @@ /** * Trait RequestAppContextTrait */ -trait RequestAppContextTrait +trait AppContextTrait { use WebApplicationTrait { __get as magicGet; diff --git a/src/Core/Application/Context/AppRequestInterface.php b/src/Core/Application/Context/AppRequestInterface.php new file mode 100644 index 00000000..57886bff --- /dev/null +++ b/src/Core/Application/Context/AppRequestInterface.php @@ -0,0 +1,71 @@ +compileInput(); @@ -203,28 +204,6 @@ protected function fetchInputFields(array $input, array $fields): mixed abstract protected function compileInput(): mixed; - /** - * @return WebSocketRequestInterface - */ - public function getRequest(): WebSocketRequestInterface - { - return $this->request; - } - - /** - * @param WebSocketRequestInterface $request - * - * @return static Return self to support chaining. - */ - public function withRequest(WebSocketRequestInterface $request): static - { - $new = clone $this; - $new->request = $request; - $this->input = null; - - return $new; - } - /** * @return SystemUri */ @@ -295,4 +274,19 @@ public function getProxyResolver(): ProxyResolver { return $this->proxyResolver; } + + /** + * @return ServerRequestInterface + * + * @deprecated Use getServerRequest() instead. + */ + public function getRequest(): ServerRequestInterface + { + return $this->request; + } + + public function getServerRequest(): ServerRequestInterface + { + return $this->request; + } } diff --git a/src/Core/Application/WebSocket/WsAppContext.php b/src/Core/Application/WebSocket/WsAppContext.php index 28d9955d..c00a0e41 100644 --- a/src/Core/Application/WebSocket/WsAppContext.php +++ b/src/Core/Application/WebSocket/WsAppContext.php @@ -12,8 +12,8 @@ namespace Windwalker\Core\Application\WebSocket; use JetBrains\PhpStorm\NoReturn; -use Windwalker\Core\Application\Context\RequestAppContextInterface; -use Windwalker\Core\Application\Context\RequestAppContextTrait; +use Windwalker\Core\Application\Context\AppContextInterface; +use Windwalker\Core\Application\Context\AppContextTrait; use Windwalker\Core\Application\WebRootApplicationInterface; use Windwalker\DI\Container; use Windwalker\Reactor\WebSocket\WebSocketFrameInterface; @@ -25,9 +25,9 @@ * * @method WebRootApplicationInterface getRootApp() */ -class WsAppContext implements WsApplicationInterface, RequestAppContextInterface, WebSocketFrameInterface +class WsAppContext implements WsApplicationInterface, AppContextInterface, WebSocketFrameInterface { - use RequestAppContextTrait; + use AppContextTrait; use WsApplicationTrait; protected WsAppRequest $appRequest; diff --git a/src/Core/Application/WebSocket/WsAppRequest.php b/src/Core/Application/WebSocket/WsAppRequest.php index 220684de..d83e2f6c 100644 --- a/src/Core/Application/WebSocket/WsAppRequest.php +++ b/src/Core/Application/WebSocket/WsAppRequest.php @@ -11,11 +11,11 @@ namespace Windwalker\Core\Application\WebSocket; +use Windwalker\Core\Application\Context\AppRequestInterface; use Windwalker\Core\Application\Context\AppRequestTrait; use Windwalker\Core\Event\CoreEventAwareTrait; use Windwalker\Core\Http\ProxyResolver; use Windwalker\Core\Router\SystemUri; -use Windwalker\Event\EventAwareInterface; use Windwalker\Filter\Traits\FilterAwareTrait; use Windwalker\Reactor\WebSocket\WebSocketFrameInterface; use Windwalker\Reactor\WebSocket\WebSocketRequestInterface; @@ -23,7 +23,7 @@ /** * The WebSocketAppRequest class. */ -class WsAppRequest implements \JsonSerializable, EventAwareInterface, WebSocketFrameInterface +class WsAppRequest implements AppRequestInterface, \JsonSerializable, WebSocketFrameInterface { use CoreEventAwareTrait; use FilterAwareTrait; @@ -56,7 +56,7 @@ public function getData(): string protected function compileInput(): mixed { - return $this->input = $this->request->getParsedData(); + return $this->input ??= $this->request->getParsedData(); } protected function getUriQueryValues(): array @@ -72,4 +72,23 @@ public function getUrlVars(): array return $this->matchedRoute->getVars(); } + + /** + * @param WebSocketRequestInterface $request + * + * @return static Return self to support chaining. + */ + public function withServerRequest(WebSocketRequestInterface $request): static + { + $new = clone $this; + $new->request = $request; + $this->input = null; + + return $new; + } + + public function getServerRequest(): WebSocketRequestInterface + { + return $this->request; + } } diff --git a/src/Core/Application/WebSocket/WsApplicationTrait.php b/src/Core/Application/WebSocket/WsApplicationTrait.php index b9c894cd..aeaf53cb 100644 --- a/src/Core/Application/WebSocket/WsApplicationTrait.php +++ b/src/Core/Application/WebSocket/WsApplicationTrait.php @@ -11,6 +11,7 @@ namespace Windwalker\Core\Application\WebSocket; +use Windwalker\Core\WebSocket\WebSocketParserInterface; use Windwalker\Reactor\WebSocket\MessageEmitterInterface; /** @@ -20,7 +21,7 @@ trait WsApplicationTrait { public function pushTo(int $fd, mixed ...$args): bool { - $data = $this->getWebSocketClient()->formatMessage(...$args); + $data = $this->getParser()->format(...$args); return $this->pushRawTo($fd, $data); } @@ -30,8 +31,8 @@ public function pushRawTo(int $fd, string $data): bool return $this->getContainer()->get(MessageEmitterInterface::class)->emit($fd, $data); } - public function getWebSocketClient(): WsClientAdapterInterface + public function getParser(): WebSocketParserInterface { - return $this->service(WsClientAdapterInterface::class); + return $this->service(WebSocketParserInterface::class); } } diff --git a/src/Core/Attributes/Controller.php b/src/Core/Attributes/Controller.php index 64e7aa1e..e46f9fa5 100644 --- a/src/Core/Attributes/Controller.php +++ b/src/Core/Attributes/Controller.php @@ -13,7 +13,7 @@ use Attribute; use Windwalker\Core\Application\AppContext; -use Windwalker\Core\Application\Context\RequestAppContextInterface; +use Windwalker\Core\Application\Context\AppContextInterface; use Windwalker\Core\Controller\DelegatingController; use Windwalker\DI\Attributes\AttributeHandler; use Windwalker\DI\Attributes\ContainerAttributeInterface; @@ -58,7 +58,7 @@ public function __invoke(AttributeHandler $handler): callable } return fn(...$args): DelegatingController => (new DelegatingController( - $container->get(RequestAppContextInterface::class), + $container->get(AppContextInterface::class), $handler(...$args) )) ->setModule($this->module) diff --git a/src/Core/Controller/ControllerDispatcher.php b/src/Core/Controller/ControllerDispatcher.php index 53b4618b..aff8cd5b 100644 --- a/src/Core/Controller/ControllerDispatcher.php +++ b/src/Core/Controller/ControllerDispatcher.php @@ -18,7 +18,7 @@ use Psr\Http\Message\UriInterface; use ReflectionAttribute; use Windwalker\Attributes\AttributesAccessor; -use Windwalker\Core\Application\Context\RequestAppContextInterface; +use Windwalker\Core\Application\Context\AppContextInterface; use Windwalker\Core\Attributes\TaskMapping; use Windwalker\Core\Controller\Exception\ControllerDispatchException; use Windwalker\Core\Events\Web\AfterControllerDispatchEvent; @@ -47,7 +47,7 @@ public function __construct(protected Container $container) // } - public function dispatch(RequestAppContextInterface $app): ResponseInterface + public function dispatch(AppContextInterface $app): ResponseInterface { $controller = $app->getController(); @@ -77,7 +77,7 @@ public function dispatch(RequestAppContextInterface $app): ResponseInterface if (is_array($controller)) { $controller = $this->prepareArrayCallable($controller, $app); } else { - $controller = fn(RequestAppContextInterface $app): mixed + $controller = fn(AppContextInterface $app): mixed => $this->container->call($controller, $app->getUrlVars()); } @@ -112,7 +112,7 @@ protected function getDefaultTask(AppRequest $request): string return $task; } - protected function prepareArrayCallable(array $handler, RequestAppContextInterface $app): Closure + protected function prepareArrayCallable(array $handler, AppContextInterface $app): Closure { if (\Windwalker\count($handler) !== 2) { throw new LogicException( @@ -127,7 +127,7 @@ protected function prepareArrayCallable(array $handler, RequestAppContextInterfa $handler[0] = $this->container->createObject($class); if ($handler[0] instanceof ControllerInterface) { - return function (RequestAppContextInterface $app) use ($handler): mixed { + return function (AppContextInterface $app) use ($handler): mixed { [$object, $task] = $handler; return $this->container->call( @@ -141,12 +141,12 @@ protected function prepareArrayCallable(array $handler, RequestAppContextInterfa throw new ControllerDispatchException('Controller is not callable.'); } - return function (RequestAppContextInterface $app) use ($handler) { + return function (AppContextInterface $app) use ($handler) { $this->container->call($handler, $app->getUrlVars()); }; } - protected function processTaskMapping(string $class, ?string $task, RequestAppContextInterface $app): ?string + protected function processTaskMapping(string $class, ?string $task, AppContextInterface $app): ?string { $mapping = AttributesAccessor::getFirstAttributeInstance( $class, diff --git a/src/Core/Controller/DelegatingController.php b/src/Core/Controller/DelegatingController.php index 3e00d19f..c011dd7a 100644 --- a/src/Core/Controller/DelegatingController.php +++ b/src/Core/Controller/DelegatingController.php @@ -15,7 +15,7 @@ use ReflectionException; use Throwable; use Windwalker\Core\Application\AppContext; -use Windwalker\Core\Application\Context\RequestAppContextInterface; +use Windwalker\Core\Application\Context\AppContextInterface; use Windwalker\Core\Application\WebSocket\WsApplicationInterface; use Windwalker\Core\Form\Exception\ValidateFailException; use Windwalker\Core\Module\ModuleInterface; @@ -37,11 +37,11 @@ class DelegatingController implements ControllerInterface /** * DelegatingController constructor. * - * @param RequestAppContextInterface $app - * @param object $controller + * @param AppContextInterface $app + * @param object $controller */ public function __construct( - protected RequestAppContextInterface $app, + protected AppContextInterface $app, protected object $controller ) { // diff --git a/src/Core/Events/Web/AfterControllerDispatchEvent.php b/src/Core/Events/Web/AfterControllerDispatchEvent.php index b7185e6a..a4d6500b 100644 --- a/src/Core/Events/Web/AfterControllerDispatchEvent.php +++ b/src/Core/Events/Web/AfterControllerDispatchEvent.php @@ -13,7 +13,7 @@ use Psr\Http\Message\ResponseInterface; use Windwalker\Core\Application\AppContext; -use Windwalker\Core\Application\Context\RequestAppContextInterface; +use Windwalker\Core\Application\Context\AppContextInterface; use Windwalker\Event\AbstractEvent; /** @@ -23,7 +23,7 @@ class AfterControllerDispatchEvent extends AbstractEvent { protected ResponseInterface $response; - protected RequestAppContextInterface $app; + protected AppContextInterface $app; /** * @return ResponseInterface @@ -46,19 +46,19 @@ public function setResponse(ResponseInterface $response): static } /** - * @return RequestAppContextInterface + * @return AppContextInterface */ - public function getApp(): RequestAppContextInterface + public function getApp(): AppContextInterface { return $this->app; } /** - * @param RequestAppContextInterface $app + * @param AppContextInterface $app * * @return static Return self to support chaining. */ - public function setApp(RequestAppContextInterface $app): static + public function setApp(AppContextInterface $app): static { $this->app = $app; diff --git a/src/Core/Events/Web/BeforeControllerDispatchEvent.php b/src/Core/Events/Web/BeforeControllerDispatchEvent.php index 9d3e6d21..218056dd 100644 --- a/src/Core/Events/Web/BeforeControllerDispatchEvent.php +++ b/src/Core/Events/Web/BeforeControllerDispatchEvent.php @@ -12,7 +12,7 @@ namespace Windwalker\Core\Events\Web; use Windwalker\Core\Application\AppContext; -use Windwalker\Core\Application\Context\RequestAppContextInterface; +use Windwalker\Core\Application\Context\AppContextInterface; use Windwalker\Event\AbstractEvent; /** @@ -22,7 +22,7 @@ class BeforeControllerDispatchEvent extends AbstractEvent { protected mixed $controller; - protected RequestAppContextInterface $app; + protected AppContextInterface $app; /** * @return mixed @@ -45,19 +45,19 @@ public function setController(mixed $controller): static } /** - * @return RequestAppContextInterface + * @return AppContextInterface */ - public function getApp(): RequestAppContextInterface + public function getApp(): AppContextInterface { return $this->app; } /** - * @param RequestAppContextInterface $app + * @param AppContextInterface $app * * @return static Return self to support chaining. */ - public function setApp(RequestAppContextInterface $app): static + public function setApp(AppContextInterface $app): static { $this->app = $app; diff --git a/src/Core/Http/AppRequest.php b/src/Core/Http/AppRequest.php index b4f91256..4fb425d2 100644 --- a/src/Core/Http/AppRequest.php +++ b/src/Core/Http/AppRequest.php @@ -15,6 +15,7 @@ use JsonSerializable; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\UploadedFileInterface; +use Windwalker\Core\Application\Context\AppRequestInterface; use Windwalker\Core\Application\Context\AppRequestTrait; use Windwalker\Core\Event\CoreEventAwareTrait; use Windwalker\Core\Http\Event\RequestGetValueEvent; @@ -23,13 +24,15 @@ use Windwalker\Event\EventAwareInterface; use Windwalker\Filter\Traits\FilterAwareTrait; +use Windwalker\Reactor\WebSocket\WebSocketRequestInterface; + use function Windwalker\collect; /** * The AppRequest class. */ #[Immutable(Immutable::PRIVATE_WRITE_SCOPE)] -class AppRequest implements JsonSerializable, EventAwareInterface +class AppRequest implements AppRequestInterface, JsonSerializable { use CoreEventAwareTrait; use FilterAwareTrait; @@ -58,11 +61,25 @@ public function getMethod(): string public function getOverrideMethod(): string { return $this->request->getHeaderLine('X-Http-Method-Override') - ?: $this->getRequest()->getParsedBody()['_method'] + ?: $this->getServerRequest()->getParsedBody()['_method'] ?? $this->getUri()->getQueryValues()['_method'] ?? $this->request->getMethod(); } + /** + * @param ServerRequestInterface $request + * + * @return static Return self to support chaining. + */ + public function withRequest(ServerRequestInterface $request): static + { + $new = clone $this; + $new->request = $request; + $this->input = null; + + return $new; + } + /** * inputWithMethod * diff --git a/src/Core/Middleware/WebSocket/WebSocketRoutingMiddleware.php b/src/Core/Middleware/WebSocket/WebSocketRoutingMiddleware.php index 24610d7a..f6f37e9c 100644 --- a/src/Core/Middleware/WebSocket/WebSocketRoutingMiddleware.php +++ b/src/Core/Middleware/WebSocket/WebSocketRoutingMiddleware.php @@ -85,7 +85,7 @@ function (WsAppContext $context) use ($request, $controller, $matched) { protected function handleByClient(WebSocketRequestInterface $request): WebSocketRequestInterface { - return $this->app->getWebSocketClient()->handleRequest($request); + return $this->app->getParser()->handleRequest($request); } protected function findController(Route $route): mixed diff --git a/src/Core/Provider/RequestProvider.php b/src/Core/Provider/RequestProvider.php index 394d5193..bdb61067 100644 --- a/src/Core/Provider/RequestProvider.php +++ b/src/Core/Provider/RequestProvider.php @@ -13,7 +13,7 @@ use Psr\Http\Message\ServerRequestInterface; use Windwalker\Core\Application\ApplicationInterface; -use Windwalker\Core\Application\Context\RequestAppContextInterface; +use Windwalker\Core\Application\Context\AppContextInterface; use Windwalker\Core\Event\EventCollector; use Windwalker\Core\Event\EventDispatcherRegistry; use Windwalker\Core\Runtime\Config; @@ -40,7 +40,7 @@ class RequestProvider implements ServiceProviderInterface */ public function register(Container $container): void { - $container->alias(ApplicationInterface::class, RequestAppContextInterface::class); + $container->alias(ApplicationInterface::class, AppContextInterface::class); $container->alias(ServerRequestInterface::class, ServerRequest::class); $container->share( diff --git a/src/Core/Provider/WebProvider.php b/src/Core/Provider/WebProvider.php index be380819..63d6494b 100644 --- a/src/Core/Provider/WebProvider.php +++ b/src/Core/Provider/WebProvider.php @@ -14,7 +14,8 @@ use Psr\Http\Message\ServerRequestFactoryInterface; use Psr\Http\Message\ServerRequestInterface; use Windwalker\Core\Application\AppContext; -use Windwalker\Core\Application\Context\RequestAppContextInterface; +use Windwalker\Core\Application\Context\AppContextInterface; +use Windwalker\Core\Application\Context\AppRequestInterface; use Windwalker\Core\Application\WebApplicationInterface; use Windwalker\Core\Controller\ControllerDispatcher; use Windwalker\Core\Security\CspNonceService; @@ -72,9 +73,6 @@ public function register(Container $container): void fn(Navigator $nav, Container $container) => $nav->addEventDealer($container->get(AppContext::class)) ); - // Renderer - $this->extendRenderer($container); - // Security $this->registerSecurityServices($container); } @@ -120,7 +118,7 @@ function (AppContext $app, Container $container) { ->setState($container->get(AppState::class)); } ) - ->alias(RequestAppContextInterface::class, AppContext::class); + ->alias(AppContextInterface::class, AppContext::class); } /** @@ -171,7 +169,8 @@ function (Container $container) { $container->set( AppRequest::class, fn(Container $container) => $container->get(AppContext::class)->getAppRequest() - ); + ) + ->alias(AppRequestInterface::class, AppRequest::class); // Browser Agent Detect $container->share( @@ -180,25 +179,6 @@ function (Container $container) { ); } - /** - * extendRenderer - * - * @param Container $container - * - * @return void - */ - protected function extendRenderer(Container $container): void - { - // $container->extend( - // RendererService::class, - // function (RendererService $service, Container $container) { - // return $service->addGlobal('app', $app = $container->get(AppContext::class)) - // ->addGlobal('uri', $app->getSystemUri()) - // ->addGlobal('nav', $container->get(Navigator::class)); - // } - // ); - } - protected function registerSecurityServices(Container $container): void { $container->prepareSharedObject( diff --git a/src/Core/Provider/WebSocketProvider.php b/src/Core/Provider/WebSocketProvider.php index bb3adfbf..7060026e 100644 --- a/src/Core/Provider/WebSocketProvider.php +++ b/src/Core/Provider/WebSocketProvider.php @@ -14,19 +14,19 @@ use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; use Psr\Http\Message\ServerRequestInterface; -use Windwalker\Core\Application\Context\RequestAppContextInterface; -use Windwalker\Core\Application\WebSocket\SocketIOAdapter; +use Windwalker\Core\Application\Context\AppContextInterface; +use Windwalker\Core\Application\Context\AppRequestInterface; use Windwalker\Core\Application\WebSocket\WsAppContext; use Windwalker\Core\Application\WebSocket\WsApplicationInterface; use Windwalker\Core\Application\WebSocket\WsAppRequest; -use Windwalker\Core\Application\WebSocket\WsClientAdapterInterface; use Windwalker\Core\Application\WebSocket\WsRootApplicationInterface; -use Windwalker\Core\CliServer\Swoole\RequestRegistry; use Windwalker\Core\Controller\ControllerDispatcher; use Windwalker\Core\Http\Browser; use Windwalker\Core\Http\ProxyResolver; use Windwalker\Core\Router\SystemUri; use Windwalker\Core\State\AppState; +use Windwalker\Core\WebSocket\SimpleMessageParser; +use Windwalker\Core\WebSocket\WebSocketParserInterface; use Windwalker\DI\Container; use Windwalker\DI\Exception\DefinitionException; use Windwalker\DI\ServiceProviderInterface; @@ -70,8 +70,7 @@ public function register(Container $container): void fn (Container $container) => $container->get(WebSocketServerInterface::class)->getMessageEmitter() ); - $container->prepareSharedObject(SocketIOAdapter::class) - ->alias(WsClientAdapterInterface::class, SocketIOAdapter::class); + $container->bindShared(WebSocketParserInterface::class, SimpleMessageParser::class); $this->registerRequestObject($container); @@ -80,18 +79,6 @@ public function register(Container $container): void // Controller Dispatcher $container->prepareSharedObject(ControllerDispatcher::class); - - // // Navigator - // $container->prepareSharedObject( - // Navigator::class, - // fn(Navigator $nav, Container $container) => $nav->addEventDealer($container->get(AppContext::class)) - // ); - - // Renderer - // $this->extendRenderer($container); - - // Security - // $this->registerSecurityServices($container); } /** @@ -135,7 +122,7 @@ function (WsAppContext $app, Container $container) { ->setState($container->get(AppState::class)); } ) - ->alias(RequestAppContextInterface::class, WsAppContext::class); + ->alias(AppContextInterface::class, WsAppContext::class); } /** @@ -170,9 +157,10 @@ function (Container $container) { // App Request $container->set( - WsAppContext::class, + WsAppRequest::class, fn(Container $container) => $container->get(WsAppContext::class)->getAppRequest() - ); + ) + ->alias(AppRequestInterface::class, WsAppRequest::class); // Browser Agent Detect $container->share( diff --git a/src/Core/WebSocket/ChainingArgumentsParer.php b/src/Core/WebSocket/ChainingArgumentsParer.php new file mode 100644 index 00000000..9fa4b7b1 --- /dev/null +++ b/src/Core/WebSocket/ChainingArgumentsParer.php @@ -0,0 +1,57 @@ + $name, + 'args' => $args, + ] = $this->parse($request->getData()); + + return $request->withUri((new Uri())->withPath($name)) + ->withParsedData($args); + } +} diff --git a/src/Core/Application/WebSocket/SocketIOAdapter.php b/src/Core/WebSocket/SimpleMessageParser.php similarity index 74% rename from src/Core/Application/WebSocket/SocketIOAdapter.php rename to src/Core/WebSocket/SimpleMessageParser.php index b32d1855..7a8a7557 100644 --- a/src/Core/Application/WebSocket/SocketIOAdapter.php +++ b/src/Core/WebSocket/SimpleMessageParser.php @@ -9,24 +9,24 @@ declare(strict_types=1); -namespace Windwalker\Core\Application\WebSocket; +namespace Windwalker\Core\WebSocket; use Windwalker\Reactor\WebSocket\WebSocketRequestInterface; use Windwalker\Uri\Uri; /** - * The SocketIOAdapter class. + * The SampleMessageParser class. */ -class SocketIOAdapter implements WsClientAdapterInterface +class SimpleMessageParser implements WebSocketParserInterface { - public function parseMessage(string $data): array + public function parse(string $data): array { [$name, $payload] = json_decode($data, true, 512, JSON_THROW_ON_ERROR) + ['', '']; return compact('name', 'payload'); } - public function formatMessage(...$args): string + public function format(...$args): string { $name = (string) ($args['name'] ?? $args[0] ?? ''); $payload = $args['payload'] ?? $args[1] ?? null; @@ -39,7 +39,7 @@ public function handleRequest(WebSocketRequestInterface $request): WebSocketRequ [ 'name' => $name, 'payload' => $payload, - ] = $this->parseMessage($request->getData()); + ] = $this->parse($request->getData()); return $request->withUri((new Uri())->withPath($name)) ->withParsedData($payload); diff --git a/src/Core/Application/WebSocket/WsClientAdapterInterface.php b/src/Core/WebSocket/WebSocketParserInterface.php similarity index 64% rename from src/Core/Application/WebSocket/WsClientAdapterInterface.php rename to src/Core/WebSocket/WebSocketParserInterface.php index 91ac4fda..65f5703d 100644 --- a/src/Core/Application/WebSocket/WsClientAdapterInterface.php +++ b/src/Core/WebSocket/WebSocketParserInterface.php @@ -9,18 +9,18 @@ declare(strict_types=1); -namespace Windwalker\Core\Application\WebSocket; +namespace Windwalker\Core\WebSocket; use Windwalker\Reactor\WebSocket\WebSocketRequestInterface; /** * Interface WsDispatcherInterface */ -interface WsClientAdapterInterface +interface WebSocketParserInterface { - public function parseMessage(string $data): mixed; + public function parse(string $data): mixed; - public function formatMessage(mixed ...$args): string; + public function format(mixed ...$args): string; public function handleRequest(WebSocketRequestInterface $request): WebSocketRequestInterface; }