@@ -275,16 +275,13 @@ could look something like this::
275
275
$request = Request::createFromGlobals();
276
276
$response = $kernel->handle($request);
277
277
278
- /*
279
- * LegacyBridge will take care of figuring out whether to boot up the
280
- * existing application or to send the Symfony response back to the client.
281
- */
282
- $scriptFile = LegacyBridge::prepareLegacyScript($request, $response, __DIR__);
283
- if ($scriptFile !== null) {
284
- require $scriptFile;
285
- } else {
278
+ if (false === $response->isNotFound()) {
279
+ // Symfony successfully handled the route.
286
280
$response->send();
281
+ } else {
282
+ LegacyBridge::handleRequest($request, $response, __DIR__);
287
283
}
284
+
288
285
$kernel->terminate($request, $response);
289
286
290
287
There are 2 major deviations from the original file:
@@ -297,10 +294,9 @@ Line 18
297
294
it over. For instance, by replacing outdated or redundant libraries with
298
295
Symfony components.
299
296
300
- Line 41 - 50
301
- Instead of sending the Symfony response directly, a ``LegacyBridge `` is
302
- called to decide whether the legacy application should be booted and used to
303
- create the response instead.
297
+ Line 41 - 46
298
+ If Symfony handled the response, it is sent; otherwise, the ``LegacyBridge ``
299
+ handles the request.
304
300
305
301
This legacy bridge is responsible for figuring out which file should be loaded
306
302
in order to process the old application logic. This can either be a front
@@ -316,19 +312,50 @@ somewhat like this::
316
312
317
313
class LegacyBridge
318
314
{
319
- public static function prepareLegacyScript(Request $request, Response $response, string $publicDirectory): ?string
315
+
316
+ /**
317
+ * Map the incoming request to the right file. This is the
318
+ * key function of the LegacyBridge.
319
+ *
320
+ * Sample code only. Your implementation will vary, depending on the
321
+ * architecture of the legacy code and how it's executed.
322
+ *
323
+ * If your mapping is complicated, you may want to write unit tests
324
+ * to verify your logic, hence this is public static.
325
+ */
326
+ public static function getLegacyScript(Request $request): string
320
327
{
321
- // If Symfony successfully handled the route, you do not have to do anything.
322
- if (false === $response->isNotFound()) {
323
- return null;
328
+ $requestPathInfo = $request->getPathInfo();
329
+ $legacyRoot = __DIR__ . '/../';
330
+
331
+ // Map a route to a legacy script:
332
+ if ($requestPathInfo == '/customer/') {
333
+ return "{$legacyRoot}src/customers/list.php";
324
334
}
325
335
326
- // Figure out how to map to the needed script file
327
- // from the existing application and possibly (re-)set
328
- // some env vars.
329
- $legacyScriptFilename = ...;
336
+ // Map a direct file call, e.g. an ajax call:
337
+ if ($requestPathInfo == 'inc/ajax_cust_details.php') {
338
+ return "{$legacyRoot}inc/ajax_cust_details.php";
339
+ }
340
+
341
+ // ... etc.
342
+
343
+ throw new \Exception("Unhandled legacy mapping for $requestPathInfo");
344
+ }
345
+
346
+
347
+ public static function handleRequest(Request $request, Response $response, string $publicDirectory): ?string
348
+ {
349
+ $legacyScriptFilename = LegacyBridge::getLegacyScript($request);
350
+
351
+ // Possibly (re-)set some env vars (e.g. to handle forms
352
+ // posting to PHP_SELF):
353
+ $p = $request->getPathInfo();
354
+ $_SERVER['PHP_SELF'] = $p;
355
+ $_SERVER['SCRIPT_NAME'] = $p;
356
+ $_SERVER['SCRIPT_FILENAME'] = $legacyScriptFilename;
330
357
331
- return $legacyScriptFilename;
358
+ require $legacyScriptFilename;
332
359
}
333
360
}
334
361
0 commit comments