-
-
Save frodosghost/1c24e6437f7b1bf7d56bc5d02fb7a421 to your computer and use it in GitHub Desktop.
Bridging the gap between standard $_SESSION and HttpFoundation Session with Silex.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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