Skip to content

Instantly share code, notes, and snippets.

@shostelet
Last active August 29, 2015 13:57
Show Gist options
  • Save shostelet/9897683 to your computer and use it in GitHub Desktop.
Save shostelet/9897683 to your computer and use it in GitHub Desktop.
<?php
// in a controller
$app->get('/some-page', function() use ($app) {
$address = $app['session']->getAddress();
return $app->render('view.html.twig', ['address' => $address]);
});
// in the extended SessionServiceProvider class
protected $app;
public function register(Application $app)
{
$this->app = $app;
$app['session.test'] = false;
$app['session'] = $app->share(function ($app) {
if (!isset($app['session.storage'])) {
if ($app['session.test']) {
$app['session.storage'] = $app['session.storage.test'];
} else {
$app['session.storage'] = $app['session.storage.native'];
}
}
if(!isset($app['session.attributebag'])) {
$app['session.attributebag'] = null;
}
if(!isset($app['session.flashbag'])) {
$app['session.flashbag'] = null;
}
return new Session($app['session.storage'], $app['session.attributebag'], $app['session.flashbag'], $app);
});
$app['session.storage.handler'] = $app->share(function ($app) {
return new NativeFileSessionHandler($app['session.storage.save_path']);
});
$app['session.storage.native'] = $app->share(function ($app) {
return new NativeSessionStorage(
$app['session.storage.options'],
$app['session.storage.handler']
);
});
$app['session.storage.test'] = $app->share(function() {
return new MockFileSessionStorage();
});
$app['session.storage.options'] = array();
$app['session.default_locale'] = 'en';
$app['session.storage.save_path'] = null;
}
// in the extended Session class
public function getAddress()
{
if(is_null($this->address))
{
if(!$data = $this->get('address'))
{
// call the API to get current user address
$data = $this->app['api']->getAddress();
$this->set('address', $data);
}
$this->address = new \mynamespace\Address($data);
}
return $this->address;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment