Commit e92c002a authored by Fabien Potencier's avatar Fabien Potencier

added a Session extension

parent 5fdbaf7f
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Silex\Extension;
use Silex\Application;
use Silex\ExtensionInterface;
use Symfony\Component\HttpFoundation\SessionStorage\NativeSessionStorage;
use Symfony\Component\HttpFoundation\Session;
use Symfony\Component\HttpKernel\Events as HttpKernelEvents;
class SessionExtension implements ExtensionInterface
{
private $app;
public function register(Application $app)
{
$this->app = $app;
$app['session'] = $app->share(function () use ($app) {
return new Session($app['session_storage']);
});
$app['session_storage'] = $app->share(function () {
return new NativeSessionStorage();
});
$app['dispatcher']->addListener(HttpKernelEvents::onCoreRequest, $this, -255);
}
public function onCoreRequest($event)
{
$request = $event->getRequest();
$request->setSession($this->app['session']);
// starts the session if a session cookie already exists in the request...
if ($request->hasSession()) {
$request->getSession()->start();
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment