Skip to content
This repository has been archived by the owner on Dec 4, 2023. It is now read-only.

Commit

Permalink
Merge fd65430 into 1170bc5
Browse files Browse the repository at this point in the history
  • Loading branch information
bckp committed Mar 10, 2022
2 parents 1170bc5 + fd65430 commit 31b856c
Show file tree
Hide file tree
Showing 9 changed files with 711 additions and 116 deletions.
4 changes: 4 additions & 0 deletions config/roadrunner.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ extensions:

roadrunner:
showExceptions: true
middlewares:
- TracyMiddleware
- SessionMiddleware
- PsrApplication
4 changes: 2 additions & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
parameters:
level: 7

checkGenericClassInNonGenericObjectType: false
level: 5
paths:
- src
43 changes: 43 additions & 0 deletions src/DI/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

namespace Mallgroup\RoadRunner\DI;

use Mallgroup\RoadRunner\PsrChain;
use Nette;
use Nette\Http\Session;
use Tracy;
use Nette\Schema\Expect;
use Nette\Schema\Schema;
use Nyholm\Psr7\Factory\Psr17Factory;
Expand Down Expand Up @@ -76,5 +79,45 @@ public function loadConfiguration()
->setFactory(PsrApplication::class)
->addSetup('$catchExceptions', [$config->catchExceptions])
->addSetup('$errorPresenter', [$config->errorPresenter]);

# Session should be ours, to support RR
/** @var Nette\DI\Definitions\ServiceDefinition $sessionDefinition */
$sessionDefinition = $builder->getDefinitionByType(Session::class);
$sessionDefinition->setFactory(\Mallgroup\RoadRunner\Http\Session::class)
->setType(\Mallgroup\RoadRunner\Http\Session::class);
}

public function beforeCompile()
{
$builder = $this->getContainerBuilder();

# Setup blueScreen if possible
if ($builder->getByType(Tracy\BlueScreen::class)) {
/** @var Nette\DI\Definitions\ServiceDefinition $serviceDefinition */
$serviceDefinition = $builder->getDefinition($this->prefix('application'));
$serviceDefinition->addSetup([self::class, 'initializeBlueScreenPanel']);
}
}

/** @internal */
public static function initializeBlueScreenPanel(
Tracy\BlueScreen $blueScreen,
Nette\Http\IRequest $httpRequest,
Nette\Http\IResponse $httpResponse,
PsrApplication $application,
): void {
$blueScreen->addPanel(function (?\Throwable $e) use ($application, $blueScreen, $httpResponse, $httpRequest): ?array {
/** @psalm-suppress InternalMethod */
$dumper = $blueScreen->getDumper();
return $e ? null : [
'tab' => 'Psr Application',
'panel' => '<h3>Requests</h3>' . $dumper($application->getRequests())
. '<h3>Presenter</h3>' . $dumper($application->getPresenter())
. '<h3>Http/Request</h3>' . $dumper($httpRequest)
. '<h3>Http/Response</h3>' . $dumper($httpResponse),
];
});
}

/** Nette\Http\Helpers::initCookie(self::$defaultHttpRequest, new Nette\Http\Response); */
}
49 changes: 28 additions & 21 deletions src/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,109 +9,116 @@

class Request implements IRequest
{
private Http\Request $request;
private ?Http\Request $request = null;

public function __construct(
private RequestFactory $requestFactory
) {
$this->request = $this->requestFactory->fromRequest();
}

public function updateFromPsr(ServerRequestInterface $request): void
{
$this->request = $this->requestFactory->fromPsr($request);
$this->request = $this->requestFactory->getRequest($request);
}

public function getUrl(): Http\UrlScript
{
return $this->request->getUrl();
return $this->getRequest()->getUrl();
}

public function getQuery(string $key = null)
{
return $this->request->getQuery(...func_get_args());
return $this->getRequest()->getQuery(...func_get_args());
}

public function getPost(string $key = null)
{
return $this->request->getPost(...func_get_args());
return $this->getRequest()->getPost(...func_get_args());
}

public function getFile(string $key): ?Http\FileUpload
{
return $this->request->getFile($key);
return $this->getRequest()->getFile($key);
}

/** @return Http\FileUpload[] */
public function getFiles(): array
{
return $this->request->getFiles();
return $this->getRequest()->getFiles();
}

public function getCookie(string $key)
{
return $this->request->getCookie($key);
return $this->getRequest()->getCookie($key);
}

/** @return mixed[] */
public function getCookies(): array
{
return $this->request->getCookies();
return $this->getRequest()->getCookies();
}

public function getMethod(): string
{
return $this->request->getMethod();
return $this->getRequest()->getMethod();
}

public function isMethod(string $method): bool
{
return $this->request->isMethod($method);
return $this->getRequest()->isMethod($method);
}

public function getHeader(string $header): ?string
{
return $this->request->getHeader($header);
return $this->getRequest()->getHeader($header);
}

/** @return array<string, string[]> */
public function getHeaders(): array
{
return $this->request->getHeaders();
return $this->getRequest()->getHeaders();
}

public function isSecured(): bool
{
return $this->request->isSecured();
return $this->getRequest()->isSecured();
}

public function isAjax(): bool
{
return $this->request->isAjax();
return $this->getRequest()->isAjax();
}

public function getRemoteAddress(): ?string
{
return $this->request->getRemoteAddress();
return $this->getRequest()->getRemoteAddress();
}

public function getRemoteHost(): ?string
{
return $this->request->getRemoteHost();
return $this->getRequest()->getRemoteHost();
}

public function getRawBody(): ?string
{
return $this->request->getRawBody();
return $this->getRequest()->getRawBody();
}

public function getReferer(): ?Http\UrlImmutable
{
return $this->request->getReferer();
return $this->getRequest()->getReferer();
}

public function isSameSite(): bool
{
return $this->request->isSameSite();
return $this->getRequest()->isSameSite();
}

private function getRequest(): Http\Request
{
if (null === $this->request) {
throw new \RuntimeException('Request is not set.');
}
return $this->request;
}
}

0 comments on commit 31b856c

Please sign in to comment.