Skip to content

Commit ecf6259

Browse files
committed
minor #17524 Simplify migration LegacyBridge example (jzohrab)
This PR was squashed before being merged into the 6.2 branch. Discussion ---------- Simplify migration LegacyBridge example Rather than return null and then return the prior request, just send the request, and fall back to the LegacyBridge if needed. I implemented my LegacyBridge following the example given in the docs (thank you for the starting point!) and then realized that the code flow could be simplified. Cheers and regards! jz Commits ------- 99eecb6 Simplify migration LegacyBridge example
2 parents 2c1aaa2 + 99eecb6 commit ecf6259

File tree

1 file changed

+48
-21
lines changed

1 file changed

+48
-21
lines changed

migration.rst

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -275,16 +275,13 @@ could look something like this::
275275
$request = Request::createFromGlobals();
276276
$response = $kernel->handle($request);
277277

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.
286280
$response->send();
281+
} else {
282+
LegacyBridge::handleRequest($request, $response, __DIR__);
287283
}
284+
288285
$kernel->terminate($request, $response);
289286

290287
There are 2 major deviations from the original file:
@@ -297,10 +294,9 @@ Line 18
297294
it over. For instance, by replacing outdated or redundant libraries with
298295
Symfony components.
299296

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.
304300

305301
This legacy bridge is responsible for figuring out which file should be loaded
306302
in order to process the old application logic. This can either be a front
@@ -316,19 +312,50 @@ somewhat like this::
316312

317313
class LegacyBridge
318314
{
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
320327
{
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";
324334
}
325335

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;
330357

331-
return $legacyScriptFilename;
358+
require $legacyScriptFilename;
332359
}
333360
}
334361

0 commit comments

Comments
 (0)