Skip to content

Commit 2041cc2

Browse files
committed
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.
1 parent f4d4197 commit 2041cc2

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

migration.rst

Lines changed: 21 additions & 22 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,22 @@ somewhat like this::
316312

317313
class LegacyBridge
318314
{
319-
public static function prepareLegacyScript(Request $request, Response $response, string $publicDirectory): ?string
315+
public static function handleRequest(Request $request, Response $response, string $publicDirectory): ?string
320316
{
321-
// If Symfony successfully handled the route, you do not have to do anything.
322-
if (false === $response->isNotFound()) {
323-
return null;
324-
}
325-
326317
// 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 = ...;
318+
// from the existing application.
319+
function getScript($requestPathInfo) { ... }
320+
321+
$p = $request->getPathInfo();
322+
$legacyScriptFilename = getScript($p);
323+
324+
// Possibly (re-)set some env vars (e.g. to handle forms
325+
// posting to PHP_SELF):
326+
$_SERVER['PHP_SELF'] = $p;
327+
$_SERVER['SCRIPT_NAME'] = $p;
328+
$_SERVER['SCRIPT_FILENAME'] = $legacyScriptFilename;
330329

331-
return $legacyScriptFilename;
330+
require $legacyScriptFilename;
332331
}
333332
}
334333

0 commit comments

Comments
 (0)