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.
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
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)
* **id**: The session id (null by default)
......
......@@ -264,6 +264,10 @@ Core parameters
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 mode.
......
......@@ -111,6 +111,8 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
return new RedirectableUrlMatcher($app['routes'], $app['request_context']);
});
$this['request.default_locale'] = 'en';
$this['request'] = function () {
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
$this->beforeDispatched = false;
$this['request'] = $request;
$this['request']->setDefaultLocale($this['request.default_locale']);
$this->flush();
......
......@@ -14,8 +14,8 @@ namespace Silex\Provider;
use Silex\Application;
use Silex\ServiceProviderInterface;
use Symfony\Component\HttpFoundation\SessionStorage\NativeSessionStorage;
use Symfony\Component\HttpFoundation\Session;
use Symfony\Component\HttpFoundation\Session\Storage\NativeFileSessionStorage;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpKernel\KernelEvents;
/**
......@@ -32,11 +32,14 @@ class SessionServiceProvider implements ServiceProviderInterface
$this->app = $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) {
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);
......@@ -44,7 +47,7 @@ class SessionServiceProvider implements ServiceProviderInterface
if (!isset($app['session.storage.options'])) {
$app['session.storage.options'] = array();
}
if (!isset($app['session.default_locale'])) {
$app['session.default_locale'] = 'en';
}
......
......@@ -15,7 +15,7 @@ use Silex\Application;
use Silex\Provider\SessionServiceProvider;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\SessionStorage\ArraySessionStorage;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
/**
* SessionProvider test cases.
......@@ -31,7 +31,7 @@ class SessionServiceProviderTest extends \PHPUnit_Framework_TestCase
$app->register(new SessionServiceProvider());
$app['session.storage'] = $app->share(function () use ($app) {
return new ArraySessionStorage();
return new MockArraySessionStorage();
});
$app->get('/login', function () use ($app) {
......@@ -41,7 +41,7 @@ class SessionServiceProviderTest extends \PHPUnit_Framework_TestCase
$app->get('/account', function () use ($app) {
if (!$app['session']->get('logged_in')) {
return 'You are not in.';
return 'You are not logged in.';
}
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