Skip to content

Commit

Permalink
misc for websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
asika32764 committed Sep 1, 2023
1 parent a715048 commit f14234d
Show file tree
Hide file tree
Showing 21 changed files with 252 additions and 125 deletions.
8 changes: 4 additions & 4 deletions src/Core/Application/AppContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
/**
* Interface RequestAppContextInterface
*/
interface RequestAppContextInterface
interface AppContextInterface
{
public function getRootApp(): RootApplicationInterface;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
/**
* Trait RequestAppContextTrait
*/
trait RequestAppContextTrait
trait AppContextTrait
{
use WebApplicationTrait {
__get as magicGet;
Expand Down
71 changes: 71 additions & 0 deletions src/Core/Application/Context/AppRequestInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

/**
* Part of cati project.
*
* @copyright Copyright (C) 2023 __ORGANIZATION__.
* @license __LICENSE__
*/

declare(strict_types=1);

namespace Windwalker\Core\Application\Context;

use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\UriInterface;
use Windwalker\Core\Http\ProxyResolver;
use Windwalker\Core\Router\Route;
use Windwalker\Core\Router\SystemUri;
use Windwalker\Data\Collection;
use Windwalker\Event\EventAwareInterface;

/**
* Interface AppRequestInterface
*/
interface AppRequestInterface extends EventAwareInterface
{
public function getClientIP(): string;

/**
* @return UriInterface
*/
public function getUri(): UriInterface;

public function getQueryValues(): array;

/**
* @return array
*/
public function getUrlVars(): array;

/**
* @return array
*/
public function getInput(): array;

public function getHeader(string $name): string;

/**
* @param mixed ...$fields
*
* @return mixed|Collection
*/
public function input(...$fields): mixed;

/**
* @return SystemUri
*/
public function getSystemUri(): SystemUri;

/**
* @return Route|null
*/
public function getMatchedRoute(): ?Route;

/**
* @return ProxyResolver
*/
public function getProxyResolver(): ProxyResolver;

public function getServerRequest(): ServerRequestInterface;
}
40 changes: 17 additions & 23 deletions src/Core/Application/Context/AppRequestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Windwalker\Core\Application\Context;

use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\UriInterface;
use Windwalker\Core\Form\Exception\ValidateFailException;
use Windwalker\Core\Http\ProxyResolver;
Expand Down Expand Up @@ -149,7 +150,7 @@ public function getHeader(string $name): string
*
* @return mixed|Collection
*/
public function input(...$fields): mixed
public function input(mixed ...$fields): mixed
{
$input = $this->compileInput();

Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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;
}
}
8 changes: 4 additions & 4 deletions src/Core/Application/WebSocket/WsAppContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
25 changes: 22 additions & 3 deletions src/Core/Application/WebSocket/WsAppRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@

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;

/**
* The WebSocketAppRequest class.
*/
class WsAppRequest implements \JsonSerializable, EventAwareInterface, WebSocketFrameInterface
class WsAppRequest implements AppRequestInterface, \JsonSerializable, WebSocketFrameInterface
{
use CoreEventAwareTrait;
use FilterAwareTrait;
Expand Down Expand Up @@ -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
Expand All @@ -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;
}
}
7 changes: 4 additions & 3 deletions src/Core/Application/WebSocket/WsApplicationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Windwalker\Core\Application\WebSocket;

use Windwalker\Core\WebSocket\WebSocketParserInterface;
use Windwalker\Reactor\WebSocket\MessageEmitterInterface;

/**
Expand All @@ -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);
}
Expand All @@ -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);
}
}
4 changes: 2 additions & 2 deletions src/Core/Attributes/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 7 additions & 7 deletions src/Core/Controller/ControllerDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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());
}

Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions src/Core/Controller/DelegatingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
) {
//
Expand Down
Loading

0 comments on commit f14234d

Please sign in to comment.