Skip to content

Instantly share code, notes, and snippets.

@frodosghost
Forked from tcz/LegacySessionStorage.php
Last active April 28, 2016 12:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frodosghost/1c24e6437f7b1bf7d56bc5d02fb7a421 to your computer and use it in GitHub Desktop.
Save frodosghost/1c24e6437f7b1bf7d56bc5d02fb7a421 to your computer and use it in GitHub Desktop.
Bridging the gap between standard $_SESSION and HttpFoundation Session with Silex.
<?php
// Register all other Silex Providers
$app->register(new Silex\Provider\SessionServiceProvider, array(
'cookie_lifetime' => 86400
));
/**
* LegacyStorage for setting Base SESSION keys into Symfony Session
*/
$app['session.storage'] = $app->share(function ($app) {
return new Session\Storage\LegacySessionStorage();
});
$app['session']->start();
<?php
namespace Session\Storage;
use Symfony\Component\HttpFoundation\Session\Storage\PhpBridgeSessionStorage;
/**
* Session storage that avoids using _sf2_attributes subkey in the $_SESSION
* superglobal but instead it uses the root variable.
*
* @link(GIST, https://gist.github.com/tcz/9756112)
*/
class LegacySessionStorage extends PhpBridgeSessionStorage
{
const SYMFONY_SESSION_STORAGE_KEY = '_sf2_attributes';
const SYMFONY_SESSION_FLASH_KEY = '_sf2_flashes';
const SYMFONY_SESSION_METADATA_KEY = '_sf2_meta';
/**
* @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_STORAGE_KEY === $key)
{
$bag->initialize($session);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment