From 846802c00332661d5752c185c8a1ea311acb9201 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Fri, 26 Aug 2022 17:58:25 +0200 Subject: [PATCH] Slight improvements on RoadRunner config --- config/autoload/url-shortener.local.php.dist | 3 ++- config/roadrunner/.rr.dev.yml | 6 ++++-- .../EventDispatcher/Event/AbstractVisitEvent.php | 2 +- .../Core/src/EventDispatcher/Event/UrlVisited.php | 14 ++++++++++++-- module/Core/src/EventDispatcher/LocateVisit.php | 2 +- module/Core/src/Visit/VisitsTracker.php | 2 +- .../Core/test/EventDispatcher/LocateVisitTest.php | 2 +- .../DropDefaultDomainFromRequestMiddleware.php | 4 ++-- 8 files changed, 24 insertions(+), 11 deletions(-) diff --git a/config/autoload/url-shortener.local.php.dist b/config/autoload/url-shortener.local.php.dist index 1fffcd0d..1ed5a47e 100644 --- a/config/autoload/url-shortener.local.php.dist +++ b/config/autoload/url-shortener.local.php.dist @@ -2,6 +2,7 @@ declare(strict_types=1); +use function Shlinkio\Shlink\Config\env; use function Shlinkio\Shlink\Config\swooleIsInstalled; return [ @@ -10,8 +11,8 @@ return [ 'domain' => [ 'schema' => 'http', 'hostname' => sprintf('localhost:%s', match (true) { + PHP_SAPI === 'cli' && env('RR_MODE') !== null => '8800', // Roadrunner swooleIsInstalled() => '8080', // Swoole - PHP_SAPI === 'cli' => '8800', // Roadrunner default => '8000', // FPM }), ], diff --git a/config/roadrunner/.rr.dev.yml b/config/roadrunner/.rr.dev.yml index 389ed003..4136f195 100644 --- a/config/roadrunner/.rr.dev.yml +++ b/config/roadrunner/.rr.dev.yml @@ -42,10 +42,12 @@ logs: level: debug reload: - enabled: true interval: 1s patterns: ['.php', '.yml', '.yaml'] services: http: - dirs: ['.'] + dirs: ['../..'] + recursive: true + jobs: + dirs: ['../..'] recursive: true diff --git a/module/Core/src/EventDispatcher/Event/AbstractVisitEvent.php b/module/Core/src/EventDispatcher/Event/AbstractVisitEvent.php index 0d41b7d1..907b3d9c 100644 --- a/module/Core/src/EventDispatcher/Event/AbstractVisitEvent.php +++ b/module/Core/src/EventDispatcher/Event/AbstractVisitEvent.php @@ -9,7 +9,7 @@ use Shlinkio\Shlink\EventDispatcher\Util\JsonUnserializable; abstract class AbstractVisitEvent implements JsonSerializable, JsonUnserializable { - public function __construct(public readonly string $visitId) + final public function __construct(public readonly string $visitId) { } diff --git a/module/Core/src/EventDispatcher/Event/UrlVisited.php b/module/Core/src/EventDispatcher/Event/UrlVisited.php index 02452a3e..c57d59d6 100644 --- a/module/Core/src/EventDispatcher/Event/UrlVisited.php +++ b/module/Core/src/EventDispatcher/Event/UrlVisited.php @@ -6,8 +6,18 @@ namespace Shlinkio\Shlink\Core\EventDispatcher\Event; final class UrlVisited extends AbstractVisitEvent { - public function __construct(string $visitId, public readonly ?string $originalIpAddress = null) + private ?string $originalIpAddress = null; + + public static function withOriginalIpAddress(string $visitId, ?string $originalIpAddress): self { - parent::__construct($visitId); + $instance = new self($visitId); + $instance->originalIpAddress = $originalIpAddress; + + return $instance; + } + + public function originalIpAddress(): ?string + { + return $this->originalIpAddress; } } diff --git a/module/Core/src/EventDispatcher/LocateVisit.php b/module/Core/src/EventDispatcher/LocateVisit.php index 197ce9a0..8708fb8a 100644 --- a/module/Core/src/EventDispatcher/LocateVisit.php +++ b/module/Core/src/EventDispatcher/LocateVisit.php @@ -41,7 +41,7 @@ class LocateVisit return; } - $this->locateVisit($visitId, $shortUrlVisited->originalIpAddress, $visit); + $this->locateVisit($visitId, $shortUrlVisited->originalIpAddress(), $visit); $this->eventDispatcher->dispatch(new VisitLocated($visitId)); } diff --git a/module/Core/src/Visit/VisitsTracker.php b/module/Core/src/Visit/VisitsTracker.php index 3aef46df..585a0f86 100644 --- a/module/Core/src/Visit/VisitsTracker.php +++ b/module/Core/src/Visit/VisitsTracker.php @@ -72,6 +72,6 @@ class VisitsTracker implements VisitsTrackerInterface $this->em->persist($visit); $this->em->flush(); - $this->eventDispatcher->dispatch(new UrlVisited($visit->getId(), $visitor->remoteAddress)); + $this->eventDispatcher->dispatch(UrlVisited::withOriginalIpAddress($visit->getId(), $visitor->remoteAddress)); } } diff --git a/module/Core/test/EventDispatcher/LocateVisitTest.php b/module/Core/test/EventDispatcher/LocateVisitTest.php index 406e8146..09a8086d 100644 --- a/module/Core/test/EventDispatcher/LocateVisitTest.php +++ b/module/Core/test/EventDispatcher/LocateVisitTest.php @@ -193,7 +193,7 @@ class LocateVisitTest extends TestCase { $ipAddr = $originalIpAddress ?? $visit->getRemoteAddr(); $location = new Location('', '', '', '', 0.0, 0.0, ''); - $event = new UrlVisited('123', $originalIpAddress); + $event = UrlVisited::withOriginalIpAddress('123', $originalIpAddress); $findVisit = $this->em->find(Visit::class, '123')->willReturn($visit); $flush = $this->em->flush()->will(function (): void { diff --git a/module/Rest/src/Middleware/ShortUrl/DropDefaultDomainFromRequestMiddleware.php b/module/Rest/src/Middleware/ShortUrl/DropDefaultDomainFromRequestMiddleware.php index 8eb98153..73a6ac69 100644 --- a/module/Rest/src/Middleware/ShortUrl/DropDefaultDomainFromRequestMiddleware.php +++ b/module/Rest/src/Middleware/ShortUrl/DropDefaultDomainFromRequestMiddleware.php @@ -11,14 +11,14 @@ use Psr\Http\Server\RequestHandlerInterface; class DropDefaultDomainFromRequestMiddleware implements MiddlewareInterface { - public function __construct(private string $defaultDomain) + public function __construct(private readonly string $defaultDomain) { } public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { /** @var array $body */ - $body = $request->getParsedBody(); + $body = $request->getParsedBody() ?? []; $request = $request->withQueryParams($this->sanitizeDomainFromPayload($request->getQueryParams())) ->withParsedBody($this->sanitizeDomainFromPayload($body));