Merge branch '2.3' into 2.6
* 2.3: [HttpFoundation] MongoDbSessionHandler::read() now checks for valid session age [WebProfilerBundle] Set debug+charset on the ExceptionHandler fallback used HTML5 meta charset tag and removed hardcoded ones Revert "bug #13715 Enforce UTF-8 charset for core controllers (WouterJ)" Conflicts: src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php src/Symfony/Bundle/WebProfilerBundle/Controller/ProfilerController.php src/Symfony/Component/HttpFoundation/Session/Storage/Handler/MongoDbSessionHandler.php src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/MongoDbSessionHandlerTest.php
This commit is contained in:
commit
43efb1ac1c
@ -1,7 +1,7 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8" />
|
<meta charset="{{ _charset }}" />
|
||||||
<title>{% block title %}Welcome!{% endblock %}</title>
|
<title>{% block title %}Welcome!{% endblock %}</title>
|
||||||
{% block stylesheets %}{% endblock %}
|
{% block stylesheets %}{% endblock %}
|
||||||
<link rel="shortcut icon" href="{{ asset('favicon.ico') }}" />
|
<link rel="shortcut icon" href="{{ asset('favicon.ico') }}" />
|
||||||
|
@ -62,7 +62,7 @@ class ExceptionController
|
|||||||
|
|
||||||
$code = $exception->getStatusCode();
|
$code = $exception->getStatusCode();
|
||||||
|
|
||||||
return Response::create($this->twig->render(
|
return new Response($this->twig->render(
|
||||||
$this->findTemplate($request, $request->getRequestFormat(), $code, $showException),
|
$this->findTemplate($request, $request->getRequestFormat(), $code, $showException),
|
||||||
array(
|
array(
|
||||||
'status_code' => $code,
|
'status_code' => $code,
|
||||||
@ -71,7 +71,7 @@ class ExceptionController
|
|||||||
'logger' => $logger,
|
'logger' => $logger,
|
||||||
'currentContent' => $currentContent,
|
'currentContent' => $currentContent,
|
||||||
)
|
)
|
||||||
))->setCharset('UTF-8');
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8" />
|
<meta charset="{{ _charset }}" />
|
||||||
<title>An Error Occurred: {{ status_text }}</title>
|
<title>An Error Occurred: {{ status_text }}</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
@ -40,8 +40,7 @@ class ExceptionControllerTest extends TestCase
|
|||||||
$request->headers->set('X-Php-Ob-Level', 1);
|
$request->headers->set('X-Php-Ob-Level', 1);
|
||||||
|
|
||||||
$controller = new ExceptionController($twig, false);
|
$controller = new ExceptionController($twig, false);
|
||||||
$response = $controller->showAction($request, $flatten);
|
$controller->showAction($request, $flatten);
|
||||||
$this->assertEquals('UTF-8', $response->getCharset(), 'Request charset is explicitly set to UTF-8');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testShowActionCanBeForcedToShowErrorPage()
|
public function testShowActionCanBeForcedToShowErrorPage()
|
||||||
|
@ -55,24 +55,23 @@ class ExceptionController
|
|||||||
$template = $this->getTemplate();
|
$template = $this->getTemplate();
|
||||||
|
|
||||||
if (!$this->twig->getLoader()->exists($template)) {
|
if (!$this->twig->getLoader()->exists($template)) {
|
||||||
$handler = new ExceptionHandler();
|
$handler = new ExceptionHandler($this->debug, $this->twig->getCharset());
|
||||||
|
|
||||||
return new Response($handler->getContent($exception), 200, array('Content-Type' => 'text/html'));
|
return new Response($handler->getContent($exception), 200, array('Content-Type' => 'text/html'));
|
||||||
}
|
}
|
||||||
|
|
||||||
$code = $exception->getStatusCode();
|
$code = $exception->getStatusCode();
|
||||||
|
|
||||||
return Response::create(
|
return new Response($this->twig->render(
|
||||||
$this->twig->render($template, array(
|
$template,
|
||||||
|
array(
|
||||||
'status_code' => $code,
|
'status_code' => $code,
|
||||||
'status_text' => Response::$statusTexts[$code],
|
'status_text' => Response::$statusTexts[$code],
|
||||||
'exception' => $exception,
|
'exception' => $exception,
|
||||||
'logger' => null,
|
'logger' => null,
|
||||||
'currentContent' => '',
|
'currentContent' => '',
|
||||||
)),
|
)
|
||||||
200,
|
), 200, array('Content-Type' => 'text/html'));
|
||||||
array('Content-Type' => 'text/html')
|
|
||||||
)->setCharset('UTF-8');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -96,16 +95,12 @@ class ExceptionController
|
|||||||
$template = $this->getTemplate();
|
$template = $this->getTemplate();
|
||||||
|
|
||||||
if (!$this->templateExists($template)) {
|
if (!$this->templateExists($template)) {
|
||||||
$handler = new ExceptionHandler();
|
$handler = new ExceptionHandler($this->debug, $this->twig->getCharset());
|
||||||
|
|
||||||
$response = new Response($handler->getStylesheet($exception), 200, array('Content-Type' => 'text/css'));
|
return new Response($handler->getStylesheet($exception), 200, array('Content-Type' => 'text/css'));
|
||||||
} else {
|
|
||||||
$response = new Response($this->twig->render('@WebProfiler/Collector/exception.css.twig'), 200, array('Content-Type' => 'text/css'));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$response->setCharset('UTF-8');
|
return new Response($this->twig->render('@WebProfiler/Collector/exception.css.twig'), 200, array('Content-Type' => 'text/css'));
|
||||||
|
|
||||||
return $response;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getTemplate()
|
protected function getTemplate()
|
||||||
|
@ -99,8 +99,7 @@ class ProfilerController
|
|||||||
throw new NotFoundHttpException(sprintf('Panel "%s" is not available for token "%s".', $panel, $token));
|
throw new NotFoundHttpException(sprintf('Panel "%s" is not available for token "%s".', $panel, $token));
|
||||||
}
|
}
|
||||||
|
|
||||||
return Response::create(
|
return new Response($this->twig->render($this->getTemplateManager()->getName($profile, $panel), array(
|
||||||
$this->twig->render($this->getTemplateManager()->getName($profile, $panel), array(
|
|
||||||
'token' => $token,
|
'token' => $token,
|
||||||
'profile' => $profile,
|
'profile' => $profile,
|
||||||
'collector' => $profile->getCollector($panel),
|
'collector' => $profile->getCollector($panel),
|
||||||
@ -109,10 +108,7 @@ class ProfilerController
|
|||||||
'request' => $request,
|
'request' => $request,
|
||||||
'templates' => $this->getTemplateManager()->getTemplates($profile),
|
'templates' => $this->getTemplateManager()->getTemplates($profile),
|
||||||
'is_ajax' => $request->isXmlHttpRequest(),
|
'is_ajax' => $request->isXmlHttpRequest(),
|
||||||
)),
|
)), 200, array('Content-Type' => 'text/html'));
|
||||||
200,
|
|
||||||
array('Content-Type' => 'text/html')
|
|
||||||
)->setCharset('UTF-8');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -151,13 +147,9 @@ class ProfilerController
|
|||||||
|
|
||||||
$this->profiler->disable();
|
$this->profiler->disable();
|
||||||
|
|
||||||
return Response::create(
|
return new Response($this->twig->render('@WebProfiler/Profiler/info.html.twig', array(
|
||||||
$this->twig->render('@WebProfiler/Profiler/info.html.twig', array(
|
|
||||||
'about' => $about,
|
'about' => $about,
|
||||||
)),
|
)), 200, array('Content-Type' => 'text/html'));
|
||||||
200,
|
|
||||||
array('Content-Type' => 'text/html')
|
|
||||||
)->setCharset('UTF-8');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -205,17 +197,13 @@ class ProfilerController
|
|||||||
// the profiler is not enabled
|
// the profiler is not enabled
|
||||||
}
|
}
|
||||||
|
|
||||||
return Response::create(
|
return new Response($this->twig->render('@WebProfiler/Profiler/toolbar.html.twig', array(
|
||||||
$this->twig->render('@WebProfiler/Profiler/toolbar.html.twig', array(
|
|
||||||
'position' => $position,
|
'position' => $position,
|
||||||
'profile' => $profile,
|
'profile' => $profile,
|
||||||
'templates' => $this->getTemplateManager()->getTemplates($profile),
|
'templates' => $this->getTemplateManager()->getTemplates($profile),
|
||||||
'profiler_url' => $url,
|
'profiler_url' => $url,
|
||||||
'token' => $token,
|
'token' => $token,
|
||||||
)),
|
)), 200, array('Content-Type' => 'text/html'));
|
||||||
200,
|
|
||||||
array('Content-Type' => 'text/html')
|
|
||||||
)->setCharset('UTF-8');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -253,7 +241,7 @@ class ProfilerController
|
|||||||
$token = $session->get('_profiler_search_token');
|
$token = $session->get('_profiler_search_token');
|
||||||
}
|
}
|
||||||
|
|
||||||
return Response::create(
|
return new Response(
|
||||||
$this->twig->render('@WebProfiler/Profiler/search.html.twig', array(
|
$this->twig->render('@WebProfiler/Profiler/search.html.twig', array(
|
||||||
'token' => $token,
|
'token' => $token,
|
||||||
'ip' => $ip,
|
'ip' => $ip,
|
||||||
@ -266,7 +254,7 @@ class ProfilerController
|
|||||||
)),
|
)),
|
||||||
200,
|
200,
|
||||||
array('Content-Type' => 'text/html')
|
array('Content-Type' => 'text/html')
|
||||||
)->setCharset('UTF-8');
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -296,8 +284,7 @@ class ProfilerController
|
|||||||
$end = $request->query->get('end', null);
|
$end = $request->query->get('end', null);
|
||||||
$limit = $request->query->get('limit');
|
$limit = $request->query->get('limit');
|
||||||
|
|
||||||
return Response::create(
|
return new Response($this->twig->render('@WebProfiler/Profiler/results.html.twig', array(
|
||||||
$this->twig->render('@WebProfiler/Profiler/results.html.twig', array(
|
|
||||||
'token' => $token,
|
'token' => $token,
|
||||||
'profile' => $profile,
|
'profile' => $profile,
|
||||||
'tokens' => $this->profiler->find($ip, $url, $limit, $method, $start, $end),
|
'tokens' => $this->profiler->find($ip, $url, $limit, $method, $start, $end),
|
||||||
@ -308,10 +295,7 @@ class ProfilerController
|
|||||||
'end' => $end,
|
'end' => $end,
|
||||||
'limit' => $limit,
|
'limit' => $limit,
|
||||||
'panel' => null,
|
'panel' => null,
|
||||||
)),
|
)), 200, array('Content-Type' => 'text/html'));
|
||||||
200,
|
|
||||||
array('Content-Type' => 'text/html')
|
|
||||||
)->setCharset('UTF-8');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -385,7 +369,7 @@ class ProfilerController
|
|||||||
phpinfo();
|
phpinfo();
|
||||||
$phpinfo = ob_get_clean();
|
$phpinfo = ob_get_clean();
|
||||||
|
|
||||||
return Response::create($phpinfo, 200, array('Content-Type' => 'text/html'))->setCharset('UTF-8');
|
return new Response($phpinfo, 200, array('Content-Type' => 'text/html'));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -68,14 +68,10 @@ class RouterController
|
|||||||
|
|
||||||
$request = $profile->getCollector('request');
|
$request = $profile->getCollector('request');
|
||||||
|
|
||||||
return Response::create(
|
return new Response($this->twig->render('@WebProfiler/Router/panel.html.twig', array(
|
||||||
$this->twig->render('@WebProfiler/Router/panel.html.twig', array(
|
|
||||||
'request' => $request,
|
'request' => $request,
|
||||||
'router' => $profile->getCollector('router'),
|
'router' => $profile->getCollector('router'),
|
||||||
'traces' => $matcher->getTraces($request->getPathInfo()),
|
'traces' => $matcher->getTraces($request->getPathInfo()),
|
||||||
)),
|
)), 200, array('Content-Type' => 'text/html'));
|
||||||
200,
|
|
||||||
array('Content-Type' => 'text/html')
|
|
||||||
)->setCharset('UTF-8');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="{{ _charset }}" />
|
||||||
<meta name="robots" content="noindex,nofollow">
|
<meta name="robots" content="noindex,nofollow" />
|
||||||
<title>{% block title 'Profiler' %}</title>
|
<title>{% block title 'Profiler' %}</title>
|
||||||
<link rel="icon" type="image/x-icon" sizes="16x16" href="data:image/ico;base64,AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAQAABMLAAATCwAAAAAAAAAAAAAAAAAAAAAAADIvMQAyLzEIMi8xSzEuMKoyLzHkMi8x/TIvMf0yLzHlMi8xrDIvMU4yLzEJM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5qb/u7u7v9WVFX/MC0v/zIvMf8yLzH/Mi8x/zIvMf8yLzHkMi8x+jAtL/9iYGH/mZiZ/2dlZv/p6On/oJ+g/np4ev/6+vr/dXN1/y0qLP8xLjD/Mi8x/zIvMf8yLzH/Mi8x/DIvMfoyLzH/MzAy/0A+QP7JyMj85eXl/1tYWv9XVVf/8fDx/6qpqv9ZV1j/Q0BC/zIvMf8yLzH/Mi8x/zIvMfwyLzHeMi8x/zEuMP8/PD762dnZ9JWTlP81MjT/ZmRm/+Dg4P/DwsP/YV5g/6Wkpf9BPkD/MS4w/zIvMf8yLzHjMi8xozIvMf8yLzH/Mi8x/nZ0dv2amJn4dXN0+V5bXf+Pjo//0tLS/0hFR/9vbG7/Ozg6/zEuMP8yLzH/Mi8xqTIvMUcyLzHrMi8x/zIvMf8xLjD/Ozg6/Do3OfwwLS//REFD/728vP9nZWb/TktN/4mIif05Nzn/Mi8x7jMwMkwyLzEGMi8xjDIvMf4yLzH/Mi8x/zIvMf8yLzH/Mi8x/zAtL/9XVFb/goGC+Hx6e+6qqanwOzg6/DMwMpJDQEIIMi8xADIvMRUyLzGrMi8x/jIvMf8yLzH/Mi8x/zIvMf8yLzH/MS4w/zg2N/xBPkD3OTY4/DIvMa8yLzEYMi8xAAAAAAAyLzEAMi8xGDIvMY0yLzHqMi8x/zIvMf8yLzH/Mi8x/zIvMf8yLzH/MS4w7DEuMJEyLzEaMi8xAAAAAAAAAAAAAAAAADIvMQAyLzEGMi8xQzIvMZ4yLzHdMi8x+jIvMfoyLzHeMi8xoDIvMUUyLzEGMi8xAAAAAAAAAAAA4AcAAMADAACAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIABAADAAwAA4AcAAA==">
|
||||||
<style>
|
<style>
|
||||||
|
@ -69,7 +69,6 @@ class ProfilerControllerTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
$response = $controller->toolbarAction(Request::create('/_wdt/found'), 'found');
|
$response = $controller->toolbarAction(Request::create('/_wdt/found'), 'found');
|
||||||
$this->assertEquals(200, $response->getStatusCode());
|
$this->assertEquals(200, $response->getStatusCode());
|
||||||
$this->assertEquals('UTF-8', $response->getCharset(), 'Request charset is explicitly set to UTF-8');
|
|
||||||
|
|
||||||
$response = $controller->toolbarAction(Request::create('/_wdt/notFound'), 'notFound');
|
$response = $controller->toolbarAction(Request::create('/_wdt/notFound'), 'notFound');
|
||||||
$this->assertEquals(404, $response->getStatusCode());
|
$this->assertEquals(404, $response->getStatusCode());
|
||||||
|
@ -177,6 +177,7 @@ class ExceptionHandler
|
|||||||
foreach ($exception->getHeaders() as $name => $value) {
|
foreach ($exception->getHeaders() as $name => $value) {
|
||||||
header($name.': '.$value, false);
|
header($name.': '.$value, false);
|
||||||
}
|
}
|
||||||
|
header('Content-Type: text/html; charset='.$this->charset);
|
||||||
}
|
}
|
||||||
|
|
||||||
echo $this->decorate($this->getContent($exception), $this->getStylesheet($exception));
|
echo $this->decorate($this->getContent($exception), $this->getStylesheet($exception));
|
||||||
@ -195,7 +196,7 @@ class ExceptionHandler
|
|||||||
$exception = FlattenException::create($exception);
|
$exception = FlattenException::create($exception);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Response($this->decorate($this->getContent($exception), $this->getStylesheet($exception)), $exception->getStatusCode(), $exception->getHeaders());
|
return Response::create($this->decorate($this->getContent($exception), $this->getStylesheet($exception)), $exception->getStatusCode(), $exception->getHeaders())->setCharset($this->charset);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -42,6 +42,24 @@ class MongoDbSessionHandler implements \SessionHandlerInterface
|
|||||||
* * id_field: The field name for storing the session id [default: _id]
|
* * id_field: The field name for storing the session id [default: _id]
|
||||||
* * data_field: The field name for storing the session data [default: data]
|
* * data_field: The field name for storing the session data [default: data]
|
||||||
* * time_field: The field name for storing the timestamp [default: time]
|
* * time_field: The field name for storing the timestamp [default: time]
|
||||||
|
* * expiry_field: The field name for storing the expiry-timestamp [default: expires_at]
|
||||||
|
*
|
||||||
|
* It is strongly recommended to put an index on the `expiry_field` for
|
||||||
|
* garbage-collection. Alternatively it's possible to automatically expire
|
||||||
|
* the sessions in the database as described below:
|
||||||
|
*
|
||||||
|
* A TTL collections can be used on MongoDB 2.2+ to cleanup expired sessions
|
||||||
|
* automatically. Such an index can for example look like this:
|
||||||
|
*
|
||||||
|
* db.<session-collection>.ensureIndex(
|
||||||
|
* { "<expiry-field>": 1 },
|
||||||
|
* { "expireAfterSeconds": 0 }
|
||||||
|
* )
|
||||||
|
*
|
||||||
|
* More details on: http://docs.mongodb.org/manual/tutorial/expire-data/
|
||||||
|
*
|
||||||
|
* If you use such an index, you can drop `gc_probability` to 0 since
|
||||||
|
* no garbage-collection is required.
|
||||||
*
|
*
|
||||||
* @param \Mongo|\MongoClient $mongo A MongoClient or Mongo instance
|
* @param \Mongo|\MongoClient $mongo A MongoClient or Mongo instance
|
||||||
* @param array $options An associative array of field options
|
* @param array $options An associative array of field options
|
||||||
@ -65,7 +83,7 @@ class MongoDbSessionHandler implements \SessionHandlerInterface
|
|||||||
'id_field' => '_id',
|
'id_field' => '_id',
|
||||||
'data_field' => 'data',
|
'data_field' => 'data',
|
||||||
'time_field' => 'time',
|
'time_field' => 'time',
|
||||||
'expiry_field' => false,
|
'expiry_field' => 'expires_at',
|
||||||
), $options);
|
), $options);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -102,21 +120,8 @@ class MongoDbSessionHandler implements \SessionHandlerInterface
|
|||||||
*/
|
*/
|
||||||
public function gc($maxlifetime)
|
public function gc($maxlifetime)
|
||||||
{
|
{
|
||||||
/* Note: MongoDB 2.2+ supports TTL collections, which may be used in
|
|
||||||
* place of this method by indexing the "time_field" field with an
|
|
||||||
* "expireAfterSeconds" option. Regardless of whether TTL collections
|
|
||||||
* are used, consider indexing this field to make the remove query more
|
|
||||||
* efficient.
|
|
||||||
*
|
|
||||||
* See: http://docs.mongodb.org/manual/tutorial/expire-data/
|
|
||||||
*/
|
|
||||||
if (false !== $this->options['expiry_field']) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
$time = new \MongoDate(time() - $maxlifetime);
|
|
||||||
|
|
||||||
$this->getCollection()->remove(array(
|
$this->getCollection()->remove(array(
|
||||||
$this->options['time_field'] => array('$lt' => $time),
|
$this->options['expiry_field'] => array('$lt' => new \MongoDate()),
|
||||||
));
|
));
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@ -127,24 +132,14 @@ class MongoDbSessionHandler implements \SessionHandlerInterface
|
|||||||
*/
|
*/
|
||||||
public function write($sessionId, $data)
|
public function write($sessionId, $data)
|
||||||
{
|
{
|
||||||
|
$expiry = new \MongoDate(time() + (int) ini_get('session.gc_maxlifetime'));
|
||||||
|
|
||||||
$fields = array(
|
$fields = array(
|
||||||
$this->options['data_field'] => new \MongoBinData($data, \MongoBinData::BYTE_ARRAY),
|
$this->options['data_field'] => new \MongoBinData($data, \MongoBinData::BYTE_ARRAY),
|
||||||
$this->options['time_field'] => new \MongoDate(),
|
$this->options['time_field'] => new \MongoDate(),
|
||||||
|
$this->options['expiry_field'] => $expiry,
|
||||||
);
|
);
|
||||||
|
|
||||||
/* Note: As discussed in the gc method of this class. You can utilise
|
|
||||||
* TTL collections in MongoDB 2.2+
|
|
||||||
* We are setting the "expiry_field" as part of the write operation here
|
|
||||||
* You will need to create the index on your collection that expires documents
|
|
||||||
* at that time
|
|
||||||
* e.g.
|
|
||||||
* db.MySessionCollection.ensureIndex( { "expireAt": 1 }, { expireAfterSeconds: 0 } )
|
|
||||||
*/
|
|
||||||
if (false !== $this->options['expiry_field']) {
|
|
||||||
$expiry = new \MongoDate(time() + (int) ini_get('session.gc_maxlifetime'));
|
|
||||||
$fields[$this->options['expiry_field']] = $expiry;
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->getCollection()->update(
|
$this->getCollection()->update(
|
||||||
array($this->options['id_field'] => $sessionId),
|
array($this->options['id_field'] => $sessionId),
|
||||||
array('$set' => $fields),
|
array('$set' => $fields),
|
||||||
@ -161,6 +156,7 @@ class MongoDbSessionHandler implements \SessionHandlerInterface
|
|||||||
{
|
{
|
||||||
$dbData = $this->getCollection()->findOne(array(
|
$dbData = $this->getCollection()->findOne(array(
|
||||||
$this->options['id_field'] => $sessionId,
|
$this->options['id_field'] => $sessionId,
|
||||||
|
$this->options['expiry_field'] => array('$gte' => new \MongoDate()),
|
||||||
));
|
));
|
||||||
|
|
||||||
return null === $dbData ? '' : $dbData[$this->options['data_field']]->bin;
|
return null === $dbData ? '' : $dbData[$this->options['data_field']]->bin;
|
||||||
|
@ -40,6 +40,7 @@ class MongoDbSessionHandlerTest extends \PHPUnit_Framework_TestCase
|
|||||||
'id_field' => '_id',
|
'id_field' => '_id',
|
||||||
'data_field' => 'data',
|
'data_field' => 'data',
|
||||||
'time_field' => 'time',
|
'time_field' => 'time',
|
||||||
|
'expiry_field' => 'expires_at',
|
||||||
'database' => 'sf2-test',
|
'database' => 'sf2-test',
|
||||||
'collection' => 'session-test',
|
'collection' => 'session-test',
|
||||||
);
|
);
|
||||||
@ -73,6 +74,42 @@ class MongoDbSessionHandlerTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertTrue($this->storage->close(), 'The "close" method should always return true');
|
$this->assertTrue($this->storage->close(), 'The "close" method should always return true');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testRead()
|
||||||
|
{
|
||||||
|
$collection = $this->createMongoCollectionMock();
|
||||||
|
|
||||||
|
$this->mongo->expects($this->once())
|
||||||
|
->method('selectCollection')
|
||||||
|
->with($this->options['database'], $this->options['collection'])
|
||||||
|
->will($this->returnValue($collection));
|
||||||
|
|
||||||
|
$that = $this;
|
||||||
|
|
||||||
|
// defining the timeout before the actual method call
|
||||||
|
// allows to test for "greater than" values in the $criteria
|
||||||
|
$testTimeout = time();
|
||||||
|
|
||||||
|
$collection->expects($this->once())
|
||||||
|
->method('findOne')
|
||||||
|
->will($this->returnCallback(function ($criteria) use ($that, $testTimeout) {
|
||||||
|
$that->assertArrayHasKey($that->options['id_field'], $criteria);
|
||||||
|
$that->assertEquals($criteria[$that->options['id_field']], 'foo');
|
||||||
|
|
||||||
|
$that->assertArrayHasKey($that->options['expiry_field'], $criteria);
|
||||||
|
$that->assertArrayHasKey('$gte', $criteria[$that->options['expiry_field']]);
|
||||||
|
$that->assertInstanceOf('MongoDate', $criteria[$that->options['expiry_field']]['$gte']);
|
||||||
|
$that->assertGreaterThanOrEqual($criteria[$that->options['expiry_field']]['$gte']->sec, $testTimeout);
|
||||||
|
|
||||||
|
return array(
|
||||||
|
$that->options['id_field'] => 'foo',
|
||||||
|
$that->options['data_field'] => new \MongoBinData('bar', \MongoBinData::BYTE_ARRAY),
|
||||||
|
$that->options['id_field'] => new \MongoDate(),
|
||||||
|
);
|
||||||
|
}));
|
||||||
|
|
||||||
|
$this->assertEquals('bar', $this->storage->read('foo'));
|
||||||
|
}
|
||||||
|
|
||||||
public function testWrite()
|
public function testWrite()
|
||||||
{
|
{
|
||||||
$collection = $this->createMongoCollectionMock();
|
$collection = $this->createMongoCollectionMock();
|
||||||
@ -94,10 +131,13 @@ class MongoDbSessionHandlerTest extends \PHPUnit_Framework_TestCase
|
|||||||
$data = $updateData['$set'];
|
$data = $updateData['$set'];
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
$expectedExpiry = time() + (int) ini_get('session.gc_maxlifetime');
|
||||||
$this->assertTrue($this->storage->write('foo', 'bar'));
|
$this->assertTrue($this->storage->write('foo', 'bar'));
|
||||||
|
|
||||||
$this->assertEquals('bar', $data[$this->options['data_field']]->bin);
|
$this->assertEquals('bar', $data[$this->options['data_field']]->bin);
|
||||||
$that->assertInstanceOf('MongoDate', $data[$this->options['time_field']]);
|
$that->assertInstanceOf('MongoDate', $data[$this->options['time_field']]);
|
||||||
|
$this->assertInstanceOf('MongoDate', $data[$this->options['expiry_field']]);
|
||||||
|
$this->assertGreaterThanOrEqual($expectedExpiry, $data[$this->options['expiry_field']]->sec);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testWriteWhenUsingExpiresField()
|
public function testWriteWhenUsingExpiresField()
|
||||||
@ -192,49 +232,13 @@ class MongoDbSessionHandlerTest extends \PHPUnit_Framework_TestCase
|
|||||||
$collection->expects($this->once())
|
$collection->expects($this->once())
|
||||||
->method('remove')
|
->method('remove')
|
||||||
->will($this->returnCallback(function ($criteria) use ($that) {
|
->will($this->returnCallback(function ($criteria) use ($that) {
|
||||||
$that->assertInstanceOf('MongoDate', $criteria[$that->options['time_field']]['$lt']);
|
$that->assertInstanceOf('MongoDate', $criteria[$that->options['expiry_field']]['$lt']);
|
||||||
$that->assertGreaterThanOrEqual(time() - 1, $criteria[$that->options['time_field']]['$lt']->sec);
|
$that->assertGreaterThanOrEqual(time() - 1, $criteria[$that->options['expiry_field']]['$lt']->sec);
|
||||||
}));
|
}));
|
||||||
|
|
||||||
$this->assertTrue($this->storage->gc(1));
|
$this->assertTrue($this->storage->gc(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGcWhenUsingExpiresField()
|
|
||||||
{
|
|
||||||
$this->options = array(
|
|
||||||
'id_field' => '_id',
|
|
||||||
'data_field' => 'data',
|
|
||||||
'time_field' => 'time',
|
|
||||||
'database' => 'sf2-test',
|
|
||||||
'collection' => 'session-test',
|
|
||||||
'expiry_field' => 'expiresAt',
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->storage = new MongoDbSessionHandler($this->mongo, $this->options);
|
|
||||||
|
|
||||||
$collection = $this->createMongoCollectionMock();
|
|
||||||
|
|
||||||
$this->mongo->expects($this->never())
|
|
||||||
->method('selectCollection');
|
|
||||||
|
|
||||||
$that = $this;
|
|
||||||
|
|
||||||
$collection->expects($this->never())
|
|
||||||
->method('remove');
|
|
||||||
|
|
||||||
$this->assertTrue($this->storage->gc(1));
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testGetConnection()
|
|
||||||
{
|
|
||||||
$method = new \ReflectionMethod($this->storage, 'getMongo');
|
|
||||||
$method->setAccessible(true);
|
|
||||||
|
|
||||||
$mongoClass = (version_compare(phpversion('mongo'), '1.3.0', '<')) ? '\Mongo' : '\MongoClient';
|
|
||||||
|
|
||||||
$this->assertInstanceOf($mongoClass, $method->invoke($this->storage));
|
|
||||||
}
|
|
||||||
|
|
||||||
private function createMongoCollectionMock()
|
private function createMongoCollectionMock()
|
||||||
{
|
{
|
||||||
$mongoClient = $this->getMockBuilder('MongoClient')
|
$mongoClient = $this->getMockBuilder('MongoClient')
|
||||||
|
Reference in New Issue
Block a user