@@ -210,7 +210,9 @@ public function processRequest(
210
210
));
211
211
$ multimeter ->setEventContext ('web. ' .$ controller_class );
212
212
213
+ $ request ->setController ($ controller );
213
214
$ request ->setURIMap ($ uri_data );
215
+
214
216
$ controller ->setRequest ($ request );
215
217
216
218
// If execution throws an exception and then trying to render that
@@ -283,35 +285,13 @@ public function processRequest(
283
285
284
286
285
287
/**
286
- * Using builtin and application routes, build the appropriate
287
- * @{class:AphrontController} class for the request. To route a request, we
288
- * first test if the HTTP_HOST is configured as a valid Phabricator URI. If
289
- * it isn't, we do a special check to see if it's a custom domain for a blog
290
- * in the Phame application and if that fails we error. Otherwise, we test
291
- * against all application routes from installed
292
- * @{class:PhabricatorApplication}s.
293
- *
294
- * If we match a route, we construct the controller it points at, build it,
295
- * and return it.
296
- *
297
- * If we fail to match a route, but the current path is missing a trailing
298
- * "/", we try routing the same path with a trailing "/" and do a redirect
299
- * if that has a valid route. The idea is to canoncalize URIs for consistency,
300
- * but avoid breaking noncanonical URIs that we can easily salvage.
301
- *
302
- * NOTE: We only redirect on GET. On POST, we'd drop parameters and most
303
- * likely mutate the request implicitly, and a bad POST usually indicates a
304
- * programming error rather than a sloppy typist.
305
- *
306
- * If the failing path already has a trailing "/", or we can't route the
307
- * version with a "/", we call @{method:build404Controller}, which build a
308
- * fallback @{class:AphrontController}.
288
+ * Build a controller to respond to the request.
309
289
*
310
290
* @return pair<AphrontController,dict> Controller and dictionary of request
311
291
* parameters.
312
292
* @task routing
313
293
*/
314
- final public function buildController () {
294
+ final private function buildController () {
315
295
$ request = $ this ->getRequest ();
316
296
317
297
// If we're configured to operate in cluster mode, reject requests which
@@ -373,78 +353,48 @@ final public function buildController() {
373
353
}
374
354
}
375
355
376
- // TODO: Really, the Site should get more control here and be able to
377
- // do its own routing logic if it wants, but we don't need that for now.
378
- $ path = $ site ->getPathForRouting ($ request );
379
-
380
- list ($ controller , $ uri_data ) = $ this ->buildControllerForPath ($ path );
381
- if (!$ controller ) {
382
- if (!preg_match ('@/$@ ' , $ path )) {
383
- // If we failed to match anything but don't have a trailing slash, try
384
- // to add a trailing slash and issue a redirect if that resolves.
385
- list ($ controller , $ uri_data ) = $ this ->buildControllerForPath ($ path .'/ ' );
386
-
387
- // NOTE: For POST, just 404 instead of redirecting, since the redirect
388
- // will be a GET without parameters.
356
+ $ maps = $ site ->getRoutingMaps ();
357
+ $ path = $ request ->getPath ();
389
358
390
- if ($ controller && !$ request ->isHTTPPost ()) {
391
- $ slash_uri = $ request ->getRequestURI ()->setPath ($ path .'/ ' );
359
+ $ result = $ this ->routePath ($ maps , $ path );
360
+ if ($ result ) {
361
+ return $ result ;
362
+ }
392
363
393
- $ external = strlen ($ request ->getRequestURI ()->getDomain ());
394
- return $ this ->buildRedirectController ($ slash_uri , $ external );
395
- }
364
+ // If we failed to match anything but don't have a trailing slash, try
365
+ // to add a trailing slash and issue a redirect if that resolves.
366
+
367
+ // NOTE: We only do this for GET, since redirects switch to GET and drop
368
+ // data like POST parameters.
369
+ if (!preg_match ('@/$@ ' , $ path ) && $ request ->isHTTPGet ()) {
370
+ $ result = $ this ->routePath ($ maps , $ path .'/ ' );
371
+ if ($ result ) {
372
+ $ slash_uri = $ request ->getRequestURI ()->setPath ($ path .'/ ' );
373
+ $ external = strlen ($ request ->getRequestURI ()->getDomain ());
374
+ return $ this ->buildRedirectController ($ slash_uri , $ external );
396
375
}
397
- return $ this ->build404Controller ();
398
376
}
399
377
400
- return array ( $ controller , $ uri_data );
378
+ return $ this -> build404Controller ( );
401
379
}
402
380
403
-
404
381
/**
405
382
* Map a specific path to the corresponding controller. For a description
406
383
* of routing, see @{method:buildController}.
407
384
*
385
+ * @param list<AphrontRoutingMap> List of routing maps.
386
+ * @param string Path to route.
408
387
* @return pair<AphrontController,dict> Controller and dictionary of request
409
388
* parameters.
410
389
* @task routing
411
390
*/
412
- final public function buildControllerForPath ($ path ) {
413
- $ maps = array ();
414
-
415
- $ applications = PhabricatorApplication::getAllInstalledApplications ();
416
- foreach ($ applications as $ application ) {
417
- $ maps [] = array ($ application , $ application ->getRoutes ());
418
- }
419
-
420
- $ current_application = null ;
421
- $ controller_class = null ;
422
- foreach ($ maps as $ map_info ) {
423
- list ($ application , $ map ) = $ map_info ;
424
-
425
- $ mapper = new AphrontURIMapper ($ map );
426
- list ($ controller_class , $ uri_data ) = $ mapper ->mapPath ($ path );
427
-
428
- if ($ controller_class ) {
429
- if ($ application ) {
430
- $ current_application = $ application ;
431
- }
432
- break ;
391
+ private function routePath (array $ maps , $ path ) {
392
+ foreach ($ maps as $ map ) {
393
+ $ result = $ map ->routePath ($ path );
394
+ if ($ result ) {
395
+ return array ($ result ->getController (), $ result ->getURIData ());
433
396
}
434
397
}
435
-
436
- if (!$ controller_class ) {
437
- return array (null , null );
438
- }
439
-
440
- $ request = $ this ->getRequest ();
441
-
442
- $ controller = newv ($ controller_class , array ());
443
- if ($ current_application ) {
444
- $ controller ->setCurrentApplication ($ current_application );
445
- }
446
-
447
- return array ($ controller , $ uri_data );
448
398
}
449
399
450
400
private function buildSiteForRequest (AphrontRequest $ request ) {
@@ -469,6 +419,8 @@ private function buildSiteForRequest(AphrontRequest $request) {
469
419
$ host ));
470
420
}
471
421
422
+ $ request ->setSite ($ site );
423
+
472
424
return $ site ;
473
425
}
474
426
0 commit comments