Search code examples
phpsilexlegacy-codesessionstorage

Bridging a legacy app sessions with silex app sessions


I am attempting to wrap a legacy application in silex by placing it in the web folder and slowly replacing pieces with new controller based silex code as updates are requested. The old application is a flat php, no controller based application that has no budgetted time within the next 16 months for complete rewrite. Placing the legacy flat code in the web folder has the desired effect where if a file is found, routes don't take effect. What I need assistance with now is configuring silex to use the php sessions the other code uses and vice versa.

I found this Symfony post that seems to indicate a way I want to do it, but the examples are not structured the same as how our silex setup is configured. http://symfony.com/doc/current/cookbook/session/php_bridge.html

How we are starting sessions currently in silex. $ app-> register (new ServiceSessionProvider ());

Sorry for lack of actual code, will edit with code snippets later today when back at a computer.

Any and all help in integrating legacy sessions is appreciated.


Solution

  • When it came to using the same sessions, the option we went with was....less than desirable, but it allows for us to continue with our plans without hindering the use of either application. The current plan is to implement database stored sessions once we have completed migrating the application's code to Silex.

    We went with an option first identified in this post Symfony session avoid _sf2_attributes. This is quite an ugly solution, but allows for the flexibility we need in attempting to migrate the application over in time with minimal effort up front. The goal is to migrate it over completely to the new Silex application, but the timeframe is over a year or more to do so.

    Here is how the session is configured in our Silex application. It is using file based storage.

    $app->register(new Silex\Provider\SessionServiceProvider(), array(
      'cookie_lifetime' => 86400,
    ));
    $app['session.storage'] = $app->share(function () use ($app) {
      return new \Symfony\Component\HttpFoundation\Session\Storage\LegacySessionStorage;
    });
    

    Here is a copy of the controller code located originally here, in case it is removed at some point.

    <?php
    
    use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
    
    /**
     * Session sotrage that avoids using _sf2_attributes subkey
     * in the $_SESSION superglobal but instead it uses
     * the root variable.
     */
    class LegacySessionStorage extends NativeSessionStorage 
    {
    const SYMFONY_SESSION_SUBKEY = '_sf2_attributes';
    
    /**
     * @inheritdoc
     */
        protected function loadSession(array &$session = null)
        {
            if (null === $session) {
                $session = &$_SESSION;
            }
    
            parent::loadSession($session);
    
            foreach ($this->bags as $bag) {
                $key = $bag->getStorageKey();
    
                if (self::SYMFONY_SESSION_SUBKEY === $key)
                {
                    $bag->initialize($session);
                }
            }
        }
    }
    

    I hope this helps some others in allowing them to migrate to a new coding style from an old application that is a thorn in their side. Wanted to make sure to sum up our findings over time in order to ensure others don't have to look as much in the future hopefully.