2017 © Pedro Peláez
 

library legacy-integration-bundle

A battle-proven approach to facilitate the incremental migration of legacy applications to the Symfony2 stack.

image

webfactory/legacy-integration-bundle

A battle-proven approach to facilitate the incremental migration of legacy applications to the Symfony2 stack.

  • Saturday, April 21, 2018
  • by webfactory
  • Repository
  • 15 Watchers
  • 20 Stars
  • 5,417 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 3 Forks
  • 1 Open issues
  • 37 Versions
  • 2 % Grown

The README.md

WebfactoryLegacyIntegrationBundle

This bundle provides an approach to integrate existing legacy applications with Symfony2., (*1)

In brief, the approach is to run a particular event listener on the kernel.controller event. This event listener will decide whether and how to run the legacy application. The response generated by the legacy application will then be captured., (*2)

Later, after the Symfony2 Controller has been executed and a Twig template is being rendered, fragments of the "legacy response" can be retrieved and be used as part of the output., (*3)

That way, you can start with your legacy application and Symfony2 in coexistence next to each other. You can then incrementally start to shift functionality over to the Symfony2 stack while maintaining a coherent user experience., (*4)

Because Symfony2 is driving this process and Twig templates have the control authority of which parts of the legacy response are being used, this approach will naturally "grow" away from the legacy application towards the Symfony2 stack., (*5)

Take care: This bundle currently uses webfactory/dom, a small set of utilities on top of PHP's DOM extension, to process the legacy application's repsonse and provide XPath-based access to it. It will only work if your old application returns well-formed XHTML or Polyglot HTML5., (*6)

To learn more about this concept, its benefits and caveats, use your time-machine and visit the Symfony2 User Group Colone meeting on 2013-12-18., (*7)

Installation

Just like any other Symfony2 bundle, left as an exercise to the reader., (*8)

Configuration

webfactory_legacy_integration:
    # Whether your legacy application returns text/html as XHTML 1.0 or Polyglot HTML5.
    parsingMode: xhtml10|html5
    # Bootstrap file for the legacy application (see next section)
    legacyApplicationBootstrapFile: www/index-legacy.php

The legacy "bootstrap" file

You must provide a single file that can be include()ed in order to run your legacy application. Typically you will already have this - it should be your legacy application's front controller., (*9)

If you are running PHP < 5.4, this file should return the HTTP status code sent by the legacy application. Starting with PHP 5.4, http_response_code() will be used to detect it., (*10)

Also, as this bundle will try to capture the response including headers using output buffering, you must not flush the response body or headers to the client., (*11)

Using it

Once you're set up, use the @Dispatch annotation on your controller to run the legacy application before your controller - like so:, (*12)

<?php
namespace Acme\My\Bundle\Controller;

use Webfactory\Bundle\LegacyIntegrationBundle\Integration\Annotation as Legacy;
use ...

class MyController ...
{
    /**
     * @Legacy\Dispatch
     */
    public function myAction()
    {
        return ...
    }
}

This will run your legacy application before entering the controller. The entire output including HTTP headers (repeat after me: including HTTP headers) will be captured and saved. Nothing will be sent to the client unless you take care of doing so., (*13)

Regarding the legacy response body, there are two ways of mixing your legacy world with your new world: Either you create a new layout and embed parts of the legacy application, or you retain your old layout and embed new parts in it. The following sections explain both of them., (*14)

Regarding HTTP headers and especially cookies sent by the legacy application make sure you don't miss the filters explained further below. For example, if your legacy code uses session_start() you probably need to forward the session cookie., (*15)

Using XPath to embed parts of the legacy response in your new layout

This is the way we recommend in general. If you're doing your legacy integration for a frontend redesign or if it's not too hard to rewrite your layout in the Symfony world, you should do that., (*16)

Only with this approach you can implement new functionality on the Symfony 2 stack that can be executed without running the legacy application at all., (*17)

For the use cases that need to be run (partially) in legacy, you can use the Twig global legacyApplication provided by the bundle to access parts of the response and place it in your views., (*18)

{# you_template.html.twig #}
<html>
    ...
    <body>
        ...
        <div id="content">
            {{ legacyApplication.xpath('//*[@id="content"]/*') | raw }}
        </div>
        ...
    </body>
</html>

Replacing arbitrary placeholders in the legacy response with markup generated by Twig

Sometimes you will have to focus on delivering new functionality on the Symfony 2 stack and you will be stuck with a page layout generated in the legacy application., (*19)

This has the disadvantage of having to run the legacy application on every request just to get this layout, which is a penalty performance-wise. Even completely new functionality will need to run the legacy application every time., (*20)

The two Twig functions webfactoy_legacy_integration_embed() and webfactory_legacy_integration_embed_result() help with this style of integration., (*21)

  • webfactory_legacy_integration_embed(placeholder, replacement) will search for arbitrary placeholder strings in the legacy application's response. It will then replace those with replacement. This function cann be used several times for different replacements., (*22)

  • webfactory_legacy_integration_embed_result() will return the final result after one or more substitutions have been performed., (*23)

These replacements are best kept in a base layout template in Twig and mapped to Twig blocks, like so:, (*24)

{# baseLayout.html.twig #}
{{ webfactory_legacy_integration_embed('<!-- MAIN_CONTENT -->', block('main_content')) }}
{{ webfactory_legacy_integration_embed_result() }}

This layout template performs no other output than the one provided by webfactory_legacy_integration_embed_result(). It can then be extended like this:, (*25)

{# new-functionality.html.twig #}
{% extends 'baseLayout.html.twig' %}
{% block main_content %}
    your new content here
{% end block %}

This has the benefit of isolating the new-functionality.html.twig template from the way it is integrated with the legacy application. Once you shift the page layout over to the Symfony 2 stack, you can update the baseLayout.html.twig template (of course it should output the main_content block as well!) and stop dispatching the legacy application for the new functionality., (*26)

Filters

The LegacyApplicationDispatchingEventListener can take a set of Filters which must implement the Webfactory\Bundle\LegacyIntegrationBundle\Integration\Filter interface., (*27)

Once the legacy application has been executed, all registered filters are passed the ControllerEvent which triggered the event listener as well as the Response object that was created for the legacy application., (*28)

The primary use case for this is to be able to examine the response and choose to send it to the client as-is, bypassing execution of the Symfony2 controller. That way, you can have routes/controllers in Symfony2 to trigger (or allow) the execution of particular use-cases in your legacy application while still returning its response unmodified initially., (*29)

You can use the webfactory_legacy_integration.filter tag to add more filters. A more convenient way is the use of additional annotations as follows., (*30)

Filter annotations

The Webfactory\Bundle\LegacyIntegrationBundle\Integration\Annotation namespace contains a few annotations you can use in addition to the @Legacy\Dispatch annotation described earlier., (*31)

In particular,, (*32)

  • @Legacy\Passthru will send the legacy application's response as-is, so the controller itself will never be run
  • @Legacy\IgnoreRedirect will bypass the controller if the legacy application sent a Location: redirect header.
  • @Legacy\IgnoreHeader("some-name") will bypass the controller if the legacy application sent "Some-Name:" header. This can be used to make the legacy application control execution of the Symfony2 controller (use with caution).
  • @Legacy\KeepHeaders will apply all HTTP headers found in the legacy response and add them to the response created by Symfony controller. You can also selectively pick headers via @Legacy\KeepHeaders({"X-Some-Header", "X-Some-Other"})
  • @Legacy\KeepCookies works like KeepHeaders but peeks at cookie names.

Bugs

We've used this bundle successfully to slowly migrate a couple of applications and projects to the Symfony2 stack. Yet it deserves a little more love, fine-polishing, and documentation. Feel free to open PRs or open issues for it to express your interest in it., (*33)

Oh, and unit tests would be fine :), (*34)

The Versions

21/04 2018
28/04 2016

dev-feature/decode_cookie_values

dev-feature/decode_cookie_values

A battle-proven approach to facilitate the incremental migration of legacy applications to the Symfony2 stack.

  Sources   Download

MIT

The Requires

 

27/04 2016

dev-feature/dispatch_sub_requests

dev-feature/dispatch_sub_requests

A battle-proven approach to facilitate the incremental migration of legacy applications to the Symfony2 stack.

  Sources   Download

MIT

The Requires

 

21/03 2013

1.0.22

1.0.22.0

  Sources   Download

The Requires

 

12/03 2013

1.0.21

1.0.21.0

  Sources   Download

The Requires

 

22/01 2013

1.0.20

1.0.20.0

  Sources   Download

The Requires

 

09/01 2013

1.0.19

1.0.19.0

  Sources   Download

The Requires

 

12/12 2012

1.0.18

1.0.18.0

  Sources   Download

The Requires

 

15/09 2012

1.0.17

1.0.17.0

  Sources   Download

The Requires

 

14/09 2012

1.0.16

1.0.16.0

  Sources   Download

The Requires

 

13/09 2012

1.0.15

1.0.15.0

  Sources   Download

The Requires

 

13/09 2012

1.0.14

1.0.14.0

  Sources   Download

The Requires

 

06/09 2012

1.0.13

1.0.13.0

  Sources   Download

The Requires

 

23/08 2012

1.0.12

1.0.12.0

  Sources   Download

The Requires

 

22/08 2012

1.0.11

1.0.11.0

  Sources   Download

The Requires

 

22/08 2012

1.0.10

1.0.10.0

  Sources   Download

The Requires

 

22/08 2012

1.0.9

1.0.9.0

  Sources   Download

The Requires

 

26/07 2012
25/07 2012
25/07 2012
11/07 2012
09/07 2012
09/07 2012
09/07 2012
06/07 2012
03/07 2012