PluginProbe
Yatra – Travel Booking & Tour Operator Software / 3.0.2.8
Yatra – Travel Booking & Tour Operator Software v3.0.2.8
3.0.14 3.0.14.1 3.0.14.2 3.0.12 3.0.13 3.0.11 3.0.10 3.0.9 3.0.8 3.0.7 3.0.6 3.0.5 3.0.5.1 3.0.4 3.0.3 3.0.2.9 3.0.2.7 3.0.2.8 3.0.2.6 trunk 1.0.0 2.0.0 2.0.1 2.0.10 2.0.11 All 82 releases
yatra / app / Core / Routing / Router.php

Router.php in Yatra – Travel Booking & Tour Operator Software 3.0.2.8, at app/Core/Routing/Router.php

162 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Yatra\Core\Routing;
6
7 use Yatra\Core\Handlers\BasePageHandler;
8 use Yatra\Core\Handlers\TripPageHandler;
9 use Yatra\Core\Handlers\AccountPageHandler;
10 use Yatra\Core\Handlers\ListingPageHandler;
11 use Yatra\Core\Handlers\TaxonomyPageHandler;
12 use Yatra\Core\Handlers\BookingPageHandler;
13 use Yatra\Core\Handlers\BookingConfirmationPageHandler;
14 use Yatra\Core\Handlers\CheckoutPageHandler;
15 use Yatra\Core\Handlers\LoginPageHandler;
16 use Yatra\Core\Routing\PlainPageMatcher;
17 use Yatra\Core\Routing\PrettyRouteMatcher;
18
19 /**
20 * Router
21 *
22 * Central routing system that matches routes and delegates to appropriate handlers
23 */
24 class Router
25 {
26 /**
27 * Route handlers registry
28 */
29 private array $handlers = [];
30
31 /**
32 * Constructor - register all route handlers
33 */
34 public function __construct()
35 {
36 $this->registerHandlers();
37 }
38
39 /**
40 * Register page handlers with production optimizations
41 */
42 private function registerHandlers(): void
43 {
44 // Lazy loading for better performance
45 $this->handlers = [
46 'trip' => function() { return new TripPageHandler(); },
47 'account' => function() { return new AccountPageHandler(); },
48 'listing' => function() { return new ListingPageHandler(); },
49 'taxonomy' => function() { return new TaxonomyPageHandler(); },
50 'booking' => function() { return new BookingPageHandler(); },
51 'booking_confirmation' => function() { return new BookingConfirmationPageHandler(); },
52 'checkout' => function() { return new CheckoutPageHandler(); },
53 'login' => function() { return new LoginPageHandler(); },
54 ];
55 }
56
57
58 /**
59 * Route the current request
60 *
61 * @return bool True if route was handled
62 */
63 public function route(): bool
64 {
65 $plain = PlainPageMatcher::match();
66 if ($plain !== null) {
67 return $this->handleRouteData($plain);
68 }
69
70 $request_path = UrlParser::getCleanRequestPath();
71
72 if ($request_path === '') {
73 return false;
74 }
75
76 $route_data = $this->matchRoute($request_path);
77
78 if ($route_data === null) {
79 return false;
80 }
81
82 return $this->handleRouteData($route_data);
83 }
84
85 /**
86 * @param array<string, mixed> $route_data
87 */
88 private function handleRouteData(array $route_data): bool
89 {
90 $handler = $this->getHandler($route_data['type']);
91
92 if (!$handler) {
93 $this->logError("No handler found for route type: {$route_data['type']}");
94
95 return false;
96 }
97
98 try {
99 return $handler->handle($route_data);
100 } catch (\Exception $e) {
101 $this->logError("Handler error for route type {$route_data['type']}: " . $e->getMessage());
102
103 return false;
104 }
105 }
106
107 /**
108 * Match pretty-permalink path (non-plain) to route data.
109 *
110 * @param string $path Request path
111 * @return array|null Route data or null if no match
112 */
113 private function matchRoute(string $path): ?array
114 {
115 return PrettyRouteMatcher::match($path);
116 }
117
118 /**
119 * Get handler instance with lazy loading and error handling
120 *
121 * @param string $route_type Route type
122 * @return BasePageHandler|null Handler instance or null if not found
123 */
124 private function getHandler(string $route_type): ?BasePageHandler
125 {
126 if (!isset($this->handlers[$route_type])) {
127 return null;
128 }
129
130 try {
131 $handler = $this->handlers[$route_type];
132
133 // Lazy loading - instantiate only when needed
134 if ($handler instanceof \Closure) {
135 $this->handlers[$route_type] = $handler();
136 return $this->handlers[$route_type];
137 }
138
139 return $handler;
140 } catch (\Throwable $e) {
141 // Log error for debugging
142 if (defined('WP_DEBUG') && WP_DEBUG) {
143 error_log('Yatra Router: Failed to load handler "' . $route_type . '": ' . $e->getMessage());
144 }
145
146 return null;
147 }
148 }
149
150 /**
151 * Log error message
152 *
153 * @param string $message Error message
154 */
155 private function logError(string $message): void
156 {
157 if (defined('WP_DEBUG') && WP_DEBUG) {
158 }
159 }
160
161 }
162