PluginProbe
Yatra – Travel Booking & Tour Operator Software / 3.0.2.9
Yatra – Travel Booking & Tour Operator Software v3.0.2.9
3.0.15 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 All 83 releases
yatra / app / Core / Routing / Router.php

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

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