Some improvements and comments in preparation of multi-segment slugs

This commit is contained in:
Alejandro Celaya 2022-07-29 17:02:00 +02:00
parent d375dece0e
commit e0e511f56d
5 changed files with 18 additions and 15 deletions

View file

@ -65,13 +65,13 @@ return [
], ],
'installation_commands' => [ 'installation_commands' => [
InstallationCommand::DB_CREATE_SCHEMA => [ InstallationCommand::DB_CREATE_SCHEMA->value => [
'command' => 'bin/cli ' . Command\Db\CreateDatabaseCommand::NAME, 'command' => 'bin/cli ' . Command\Db\CreateDatabaseCommand::NAME,
], ],
InstallationCommand::DB_MIGRATE => [ InstallationCommand::DB_MIGRATE->value => [
'command' => 'bin/cli ' . Command\Db\MigrateDatabaseCommand::NAME, 'command' => 'bin/cli ' . Command\Db\MigrateDatabaseCommand::NAME,
], ],
InstallationCommand::GEOLITE_DOWNLOAD_DB => [ InstallationCommand::GEOLITE_DOWNLOAD_DB->value => [
'command' => 'bin/cli ' . Command\Visit\DownloadGeoLiteDbCommand::NAME, 'command' => 'bin/cli ' . Command\Visit\DownloadGeoLiteDbCommand::NAME,
], ],
], ],

View file

@ -68,7 +68,7 @@ return [
// This middleware is in front of tracking actions explicitly. Putting here for orphan visits tracking // This middleware is in front of tracking actions explicitly. Putting here for orphan visits tracking
IpAddress::class, IpAddress::class,
Core\ErrorHandler\NotFoundTypeResolverMiddleware::class, Core\ErrorHandler\NotFoundTypeResolverMiddleware::class,
// TODO MultiSegmentCustomSlugRedirectMiddleware // TODO MultiSegmentSlugRedirectMiddleware
Core\ShortUrl\Middleware\ExtraPathRedirectMiddleware::class, Core\ShortUrl\Middleware\ExtraPathRedirectMiddleware::class,
Core\ErrorHandler\NotFoundTrackerMiddleware::class, Core\ErrorHandler\NotFoundTrackerMiddleware::class,
Core\ErrorHandler\NotFoundRedirectHandler::class, Core\ErrorHandler\NotFoundRedirectHandler::class,

View file

@ -13,21 +13,21 @@ use function rtrim;
class NotFoundType 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 public static function fromRequest(ServerRequestInterface $request, string $basePath): self
{ {
/** @var RouteResult $routeResult */ /** @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; $isBaseUrl = rtrim($request->getUri()->getPath(), '/') === $basePath;
$type = match (true) { $type = match (true) {
$isBaseUrl => VisitType::BASE_URL, $isBaseUrl => VisitType::BASE_URL,
$routeResult->isFailure() => VisitType::REGULAR_404, $routeResult->isFailure() => VisitType::REGULAR_404,
$routeResult->getMatchedRouteName() === RedirectAction::class => VisitType::INVALID_SHORT_URL, $routeResult->getMatchedRouteName() === RedirectAction::class => VisitType::INVALID_SHORT_URL,
default => VisitType::VALID_SHORT_URL, default => null,
}; };
return new self($type); return new self($type);

View file

@ -26,11 +26,11 @@ use function trim;
class ExtraPathRedirectMiddleware implements MiddlewareInterface class ExtraPathRedirectMiddleware implements MiddlewareInterface
{ {
public function __construct( public function __construct(
private ShortUrlResolverInterface $resolver, private readonly ShortUrlResolverInterface $resolver,
private RequestTrackerInterface $requestTracker, private readonly RequestTrackerInterface $requestTracker,
private ShortUrlRedirectionBuilderInterface $redirectionBuilder, private readonly ShortUrlRedirectionBuilderInterface $redirectionBuilder,
private RedirectResponseHelperInterface $redirectResponseHelper, private readonly RedirectResponseHelperInterface $redirectResponseHelper,
private UrlShortenerOptions $urlShortenerOptions, private readonly UrlShortenerOptions $urlShortenerOptions,
) { ) {
} }
@ -39,7 +39,7 @@ class ExtraPathRedirectMiddleware implements MiddlewareInterface
/** @var NotFoundType|null $notFoundType */ /** @var NotFoundType|null $notFoundType */
$notFoundType = $request->getAttribute(NotFoundType::class); $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()) { if (! $notFoundType?->isRegularNotFound() || ! $this->urlShortenerOptions->appendExtraPath()) {
return $handler->handle($request); return $handler->handle($request);
} }
@ -50,6 +50,7 @@ class ExtraPathRedirectMiddleware implements MiddlewareInterface
$identifier = ShortUrlIdentifier::fromShortCodeAndDomain($potentialShortCode, $uri->getAuthority()); $identifier = ShortUrlIdentifier::fromShortCodeAndDomain($potentialShortCode, $uri->getAuthority());
try { try {
// TODO Try pieces of the URL in order to match multi-segment slugs too
$shortUrl = $this->resolver->resolveEnabledShortUrl($identifier); $shortUrl = $this->resolver->resolveEnabledShortUrl($identifier);
$this->requestTracker->trackIfApplicable($shortUrl, $request); $this->requestTracker->trackIfApplicable($shortUrl, $request);

View file

@ -24,8 +24,10 @@ use function str_contains;
class RequestTracker implements RequestTrackerInterface, RequestMethodInterface 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 public function trackIfApplicable(ShortUrl $shortUrl, ServerRequestInterface $request): void