registerHandlers(); } /** * Register page handlers with production optimizations */ private function registerHandlers(): void { // Lazy loading for better performance $this->handlers = [ 'trip' => function() { return new TripPageHandler(); }, 'account' => function() { return new AccountPageHandler(); }, 'listing' => function() { return new ListingPageHandler(); }, 'taxonomy' => function() { return new TaxonomyPageHandler(); }, 'booking' => function() { return new BookingPageHandler(); }, 'booking_confirmation' => function() { return new BookingConfirmationPageHandler(); }, 'checkout' => function() { return new CheckoutPageHandler(); }, 'email_verification' => function() { return new EmailVerificationPageHandler(); }, ]; } /** * Route the current request * * @return bool True if route was handled */ public function route(): bool { $plain = PlainPageMatcher::match(); if ($plain !== null) { return $this->handleRouteData($plain); } $request_path = UrlParser::getCleanRequestPath(); if ($request_path === '') { return false; } $route_data = $this->matchRoute($request_path); if ($route_data === null) { return false; } return $this->handleRouteData($route_data); } /** * @param array $route_data */ private function handleRouteData(array $route_data): bool { $handler = $this->getHandler($route_data['type']); if (!$handler) { $this->logError("No handler found for route type: {$route_data['type']}"); return false; } try { return $handler->handle($route_data); } catch (\Exception $e) { $this->logError("Handler error for route type {$route_data['type']}: " . $e->getMessage()); return false; } } /** * Match pretty-permalink path (non-plain) to route data. * * @param string $path Request path * @return array|null Route data or null if no match */ private function matchRoute(string $path): ?array { return PrettyRouteMatcher::match($path); } /** * Get handler instance with lazy loading and error handling * * @param string $route_type Route type * @return BasePageHandler|null Handler instance or null if not found */ private function getHandler(string $route_type): ?BasePageHandler { if (!isset($this->handlers[$route_type])) { return null; } try { $handler = $this->handlers[$route_type]; // Lazy loading - instantiate only when needed if ($handler instanceof \Closure) { $this->handlers[$route_type] = $handler(); return $this->handlers[$route_type]; } return $handler; } catch (\Throwable $e) { return null; } } /** * Log error message * * @param string $message Error message */ private function logError(string $message): void { if (defined('WP_DEBUG') && WP_DEBUG) { } } }