diff --git a/config/autoload/installer.global.php b/config/autoload/installer.global.php index c82b4a97..8a452d92 100644 --- a/config/autoload/installer.global.php +++ b/config/autoload/installer.global.php @@ -65,13 +65,13 @@ return [ ], 'installation_commands' => [ - InstallationCommand::DB_CREATE_SCHEMA => [ + InstallationCommand::DB_CREATE_SCHEMA->value => [ 'command' => 'bin/cli ' . Command\Db\CreateDatabaseCommand::NAME, ], - InstallationCommand::DB_MIGRATE => [ + InstallationCommand::DB_MIGRATE->value => [ 'command' => 'bin/cli ' . Command\Db\MigrateDatabaseCommand::NAME, ], - InstallationCommand::GEOLITE_DOWNLOAD_DB => [ + InstallationCommand::GEOLITE_DOWNLOAD_DB->value => [ 'command' => 'bin/cli ' . Command\Visit\DownloadGeoLiteDbCommand::NAME, ], ], diff --git a/config/autoload/middleware-pipeline.global.php b/config/autoload/middleware-pipeline.global.php index a5446671..9c0e2978 100644 --- a/config/autoload/middleware-pipeline.global.php +++ b/config/autoload/middleware-pipeline.global.php @@ -68,7 +68,7 @@ return [ // This middleware is in front of tracking actions explicitly. Putting here for orphan visits tracking IpAddress::class, Core\ErrorHandler\NotFoundTypeResolverMiddleware::class, - // TODO MultiSegmentCustomSlugRedirectMiddleware + // TODO MultiSegmentSlugRedirectMiddleware Core\ShortUrl\Middleware\ExtraPathRedirectMiddleware::class, Core\ErrorHandler\NotFoundTrackerMiddleware::class, Core\ErrorHandler\NotFoundRedirectHandler::class, diff --git a/module/Core/src/ErrorHandler/Model/NotFoundType.php b/module/Core/src/ErrorHandler/Model/NotFoundType.php index f95368cb..99f7fbe6 100644 --- a/module/Core/src/ErrorHandler/Model/NotFoundType.php +++ b/module/Core/src/ErrorHandler/Model/NotFoundType.php @@ -13,21 +13,21 @@ use function rtrim; class NotFoundType { - private function __construct(private readonly VisitType $type) + private function __construct(private readonly ?VisitType $type) { } public static function fromRequest(ServerRequestInterface $request, string $basePath): self { /** @var RouteResult $routeResult */ - $routeResult = $request->getAttribute(RouteResult::class, RouteResult::fromRouteFailure(null)); + $routeResult = $request->getAttribute(RouteResult::class) ?? RouteResult::fromRouteFailure(null); $isBaseUrl = rtrim($request->getUri()->getPath(), '/') === $basePath; $type = match (true) { $isBaseUrl => VisitType::BASE_URL, $routeResult->isFailure() => VisitType::REGULAR_404, $routeResult->getMatchedRouteName() === RedirectAction::class => VisitType::INVALID_SHORT_URL, - default => VisitType::VALID_SHORT_URL, + default => null, }; return new self($type); diff --git a/module/Core/src/ShortUrl/Middleware/ExtraPathRedirectMiddleware.php b/module/Core/src/ShortUrl/Middleware/ExtraPathRedirectMiddleware.php index 9d92067c..2704cadb 100644 --- a/module/Core/src/ShortUrl/Middleware/ExtraPathRedirectMiddleware.php +++ b/module/Core/src/ShortUrl/Middleware/ExtraPathRedirectMiddleware.php @@ -26,11 +26,11 @@ use function trim; class ExtraPathRedirectMiddleware implements MiddlewareInterface { public function __construct( - private ShortUrlResolverInterface $resolver, - private RequestTrackerInterface $requestTracker, - private ShortUrlRedirectionBuilderInterface $redirectionBuilder, - private RedirectResponseHelperInterface $redirectResponseHelper, - private UrlShortenerOptions $urlShortenerOptions, + private readonly ShortUrlResolverInterface $resolver, + private readonly RequestTrackerInterface $requestTracker, + private readonly ShortUrlRedirectionBuilderInterface $redirectionBuilder, + private readonly RedirectResponseHelperInterface $redirectResponseHelper, + private readonly UrlShortenerOptions $urlShortenerOptions, ) { } @@ -39,7 +39,7 @@ class ExtraPathRedirectMiddleware implements MiddlewareInterface /** @var NotFoundType|null $notFoundType */ $notFoundType = $request->getAttribute(NotFoundType::class); - // We'll apply this logic only if actively opted in and current URL is potentially /{shortCode}/[...] + // This logic is applied only if actively opted in and current URL is potentially /{shortCode}/[...] if (! $notFoundType?->isRegularNotFound() || ! $this->urlShortenerOptions->appendExtraPath()) { return $handler->handle($request); } @@ -50,6 +50,7 @@ class ExtraPathRedirectMiddleware implements MiddlewareInterface $identifier = ShortUrlIdentifier::fromShortCodeAndDomain($potentialShortCode, $uri->getAuthority()); try { + // TODO Try pieces of the URL in order to match multi-segment slugs too $shortUrl = $this->resolver->resolveEnabledShortUrl($identifier); $this->requestTracker->trackIfApplicable($shortUrl, $request); diff --git a/module/Core/src/Visit/RequestTracker.php b/module/Core/src/Visit/RequestTracker.php index 3177d08c..1887dbfd 100644 --- a/module/Core/src/Visit/RequestTracker.php +++ b/module/Core/src/Visit/RequestTracker.php @@ -24,8 +24,10 @@ use function str_contains; class RequestTracker implements RequestTrackerInterface, RequestMethodInterface { - public function __construct(private VisitsTrackerInterface $visitsTracker, private TrackingOptions $trackingOptions) - { + public function __construct( + private readonly VisitsTrackerInterface $visitsTracker, + private readonly TrackingOptions $trackingOptions, + ) { } public function trackIfApplicable(ShortUrl $shortUrl, ServerRequestInterface $request): void