Commit 143ca105 authored by Igor Wiedler's avatar Igor Wiedler

[session-update] update to 2.1 session refactoring

parent d230b294
...@@ -7,12 +7,14 @@ between requests. ...@@ -7,12 +7,14 @@ between requests.
Parameters Parameters
---------- ----------
* **session.default_locale**: The locale used by default in the session. * **session.storage.save_path** (optional): The path for the
``NativeFileSessionStorage``, defaults to the value of
``sys_get_temp_dir()``.
* **session.storage.options**: An array of options that is passed to the * **session.storage.options**: An array of options that is passed to the
constructor of the ``session.storage`` service. constructor of the ``session.storage`` service.
In case of the default ``NativeSessionStorage``, the possible options are: In case of the default ``NativeFileSessionStorage``, the possible options are:
* **name**: The cookie name (_SESS by default) * **name**: The cookie name (_SESS by default)
* **id**: The session id (null by default) * **id**: The session id (null by default)
......
...@@ -264,6 +264,10 @@ Core parameters ...@@ -264,6 +264,10 @@ Core parameters
This parameter can be used by the ``UrlGeneratorProvider``. This parameter can be used by the ``UrlGeneratorProvider``.
* **request.default_locale** (optional): The locale used by default.
Defaults to ``en``.
* **debug** (optional): Returns whether or not the application is running in * **debug** (optional): Returns whether or not the application is running in
debug mode. debug mode.
......
...@@ -111,6 +111,8 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe ...@@ -111,6 +111,8 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
return new RedirectableUrlMatcher($app['routes'], $app['request_context']); return new RedirectableUrlMatcher($app['routes'], $app['request_context']);
}); });
$this['request.default_locale'] = 'en';
$this['request'] = function () { $this['request'] = function () {
throw new \RuntimeException('Accessed request service outside of request scope. Try moving that call to a before handler or controller.'); throw new \RuntimeException('Accessed request service outside of request scope. Try moving that call to a before handler or controller.');
}; };
...@@ -375,6 +377,7 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe ...@@ -375,6 +377,7 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
$this->beforeDispatched = false; $this->beforeDispatched = false;
$this['request'] = $request; $this['request'] = $request;
$this['request']->setDefaultLocale($this['request.default_locale']);
$this->flush(); $this->flush();
......
...@@ -14,8 +14,8 @@ namespace Silex\Provider; ...@@ -14,8 +14,8 @@ namespace Silex\Provider;
use Silex\Application; use Silex\Application;
use Silex\ServiceProviderInterface; use Silex\ServiceProviderInterface;
use Symfony\Component\HttpFoundation\SessionStorage\NativeSessionStorage; use Symfony\Component\HttpFoundation\Session\Storage\NativeFileSessionStorage;
use Symfony\Component\HttpFoundation\Session; use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\HttpKernel\KernelEvents;
/** /**
...@@ -32,11 +32,14 @@ class SessionServiceProvider implements ServiceProviderInterface ...@@ -32,11 +32,14 @@ class SessionServiceProvider implements ServiceProviderInterface
$this->app = $app; $this->app = $app;
$app['session'] = $app->share(function () use ($app) { $app['session'] = $app->share(function () use ($app) {
return new Session($app['session.storage'], $app['session.default_locale']); return new Session($app['session.storage']);
}); });
$app['session.storage'] = $app->share(function () use ($app) { $app['session.storage'] = $app->share(function () use ($app) {
return new NativeSessionStorage($app['session.storage.options']); return new NativeFileSessionStorage(
isset($app['session.storage.save_path']) ? $app['session.storage.save_path'] : null,
$app['session.storage.options']
);
}); });
$app['dispatcher']->addListener(KernelEvents::REQUEST, array($this, 'onKernelRequest'), 128); $app['dispatcher']->addListener(KernelEvents::REQUEST, array($this, 'onKernelRequest'), 128);
......
...@@ -15,7 +15,7 @@ use Silex\Application; ...@@ -15,7 +15,7 @@ use Silex\Application;
use Silex\Provider\SessionServiceProvider; use Silex\Provider\SessionServiceProvider;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\SessionStorage\ArraySessionStorage; use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
/** /**
* SessionProvider test cases. * SessionProvider test cases.
...@@ -31,7 +31,7 @@ class SessionServiceProviderTest extends \PHPUnit_Framework_TestCase ...@@ -31,7 +31,7 @@ class SessionServiceProviderTest extends \PHPUnit_Framework_TestCase
$app->register(new SessionServiceProvider()); $app->register(new SessionServiceProvider());
$app['session.storage'] = $app->share(function () use ($app) { $app['session.storage'] = $app->share(function () use ($app) {
return new ArraySessionStorage(); return new MockArraySessionStorage();
}); });
$app->get('/login', function () use ($app) { $app->get('/login', function () use ($app) {
...@@ -41,7 +41,7 @@ class SessionServiceProviderTest extends \PHPUnit_Framework_TestCase ...@@ -41,7 +41,7 @@ class SessionServiceProviderTest extends \PHPUnit_Framework_TestCase
$app->get('/account', function () use ($app) { $app->get('/account', function () use ($app) {
if (!$app['session']->get('logged_in')) { if (!$app['session']->get('logged_in')) {
return 'You are not in.'; return 'You are not logged in.';
} }
return 'This is your account.'; return 'This is your account.';
......
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