8000 [2.1] Support PHP 5.4 \SessionHandler · Pull Request #3493 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[2.1] Support PHP 5.4 \SessionHandler #3493

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from Mar 15, 2012
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[HttpFoundation][FrameworkBundle] Separate out mock session storage a…
…nd stop polluting global namespace.

This makes mock sessions truly mock and not to interfere with global namespace.
Add getters and setters for session name and ID.
  • Loading branch information
Drak committed Mar 14, 2012
commit b12ece0ff715a423e0f293f2648f3aa4bc55e445
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@
*/
class SessionListener implements EventSubscriberInterface
{
/**
* @var ContainerInterface
*/
private $container;

/**
* @var boolean
*/
private $autoStart;

public function __construct(ContainerInterface $container, $autoStart = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,16 @@ public function onKernelRequest(GetResponseEvent $event)
}

// bootstrap the session
if ($this->container->has('session')) {
$this->container->get('session');
if (!$this->container->has('session')) {
return;
}

$session = $this->container->get('session');
$cookies = $event->getRequest()->cookies;
if ($cookies->has(session_name())) {
session_id($cookies->get(session_name()));
if ($cookies->has($session->getName())) {
$session->setId($cookies->get($session->getName()));
} else {
session_id('');
$session->setId('');
}
}

Expand All @@ -72,7 +73,7 @@ public function onKernelResponse(FilterResponseEvent $event)

$params = session_get_cookie_params();

$event->getResponse()->headers->setCookie(new Cookie(session_name(), $session->getId(), 0 === $params['lifetime'] ? 0 : time() + $params['lifetime'], $params['path'], $params['domain'], $params['secure'], $params['httponly']));
$event->getResponse()->headers->setCookie(new Cookie($session->getName(), $session->getId(), 0 === $params['lifetime'] ? 0 : time() + $params['lifetime'], $params['path'], $params['domain'], $params['secure'], $params['httponly']));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,13 @@ private function sessionMustBeSaved()

private function getSession()
{
return $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\Session')
$mock = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\Session')
->disableOriginalConstructor()
->getMock();

// set return value for getName()
$mock->expects($this->any())->method('getName')->will($this->returnValue('MOCKSESSID'));

return $mock;
}
}
3 changes: 2 additions & 1 deletion src/Symfony/Component/HttpFoundation/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,8 @@ public function getSession()
public function hasPreviousSession()
{
// the check for $this->session avoids malicious users trying to fake a session cookie with proper name
return $this->cookies->has(session_name()) && null !== $this->session;
$sessionName = $this->hasSession() ? $this->session->getName() : null;
return $this->cookies->has($sessionName) && $this->hasSession();
}

/**
Expand Down
33 changes: 27 additions & 6 deletions src/Symfony/Component/HttpFoundation/Session/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
use Symfony\Component\HttpFoundation\Session\SessionBagInterface;
use Symfony\Component\HttpFoundation\Session\Storage\SessionStorage;

/**
* Session.
Expand Down Expand Up @@ -140,19 +141,39 @@ public function save()
}

/**
* Returns the session ID
*
* @return mixed The session ID
*
* @api
* {@inheritdoc}
*/
public function getId()
{
return $this->storage->getId();
}

/**
* Registers a SessionBagInterface with the sessio.
* {@inheritdoc}
*/
public function setId($id)
{
$this->storage->setId($id);
}

/**
* {@inheritdoc}
*/
public function getName()
{
return $this->storage->getName();
}

/**
* {@inheritdoc}
*/
public function setName($name)
{
$this->storage->setName($name);
}

/**
* Registers a SessionBagInterface with the session.
*
* @param SessionBagInterface $bag
*/
Expand Down
36 changes: 36 additions & 0 deletions src/Symfony/Component/HttpFoundation/Session/SessionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,42 @@ interface SessionInterface
*/
function start();

/**
* Returns the session ID.
*
* @return string The session ID.
*
* @api
*/
function getId();

/**
* Sets the session ID
*
* @param string $id
*
* @api
*/
function setId($id);

/**
* Returns the session name.
*
* @return mixed The session name.
*
* @api
*/
function getName();

/**
* Sets the session name.
*
* @param string $name
*
* @api
*/
function setName($name);

/**
* Invalidates the current session.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace Symfony\Component\HttpFoundation\Session\Storage\Handler;

/**
* NativeFileSessionStorage.
* NativeFileSessionHandler.
*
* Native session handler using PHP's built in file storage.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Symfony\Component\HttpFoundation\Session\Storage;

use Symfony\Component\HttpFoundation\Session\Storage\Handler\NullSessionHandler;
use Symfony\Component\HttpFoundation\Session\SessionBagInterface;

/**
* MockArraySessionStorage mocks the session for unit tests.
Expand All @@ -25,21 +25,41 @@
* @author Bulat Shakirzyanov <mallluhuct@gmail.com>
* @author Drak <drak@zikula.org>
*/
class MockArraySessionStorage extends SessionStorage
class MockArraySessionStorage implements SessionStorageInterface
{
/**
* @var string
*/
protected $sessionId;
protected $id = '';

/**
* @var string
*/
protected $name;

/**
* @var boolean
*/
protected $started = false;

/**
* @var boolean
*/
protected $closed = false;

/**
* @var array
*/
protected $sessionData = array();
protected $data = array();

public function __construct(array $options = array())
/**
* Constructor.
*
* @param string $name Session name
*/
public function __construct($name = 'MOCKSESSID')
{
parent::__construct($options, new NullSessionHandler());
$this->name = $name;
}

/**
Expand All @@ -49,7 +69,7 @@ public function __construct(array $options = array())
*/
public function setSessionData(array $array)
{
$this->sessionData = $array;
$this->data = $array;
}

/**
Expand All @@ -61,11 +81,11 @@ public function start()
return true;
}

$this->started = true;
$this->loadSession($this->sessionData);
if (empty($this->id)) {
$this->id = $this->generateId();
}

$this->sessionId = $this->generateSessionId();
session_id($this->sessionId);
$this->loadSession();

return true;
}
Expand All @@ -80,8 +100,7 @@ public function regenerate($destroy = false)
$this->start();
}

$this->sessionId = $this->generateSessionId();
session_id($this->sessionId);
$this->id = $this->generateId();

return true;
}
Expand All @@ -91,11 +110,35 @@ public function regenerate($destroy = false)
*/
public function getId()
{
if (!$this->started) {
return '';
return $this->id;
}

/**
* {@inheritdoc}
*/
public function setId($id)
{
if ($this->started) {
throw new \LogicException('Cannot set session ID after the session has started.');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, this exception throws in test environment for any $client->request

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have a specific example to show?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, its FOS_facebook auth break all

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you link to the specific tests it breaks please? I tried to look for them but didnt see anything.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,

I not find tests, but they start issue
FriendsOfSymfony/FOSFacebookBundle#79
but fix not work :)

2012/3/22 Drak <
reply@reply.github.com

@@ -79,11 +110,35 @@ public function regenerate($destroy = false)
*/
public function getId()
{

  •    if (!$this->started) {
    
  •        return '';
    
  •    return $this->id;
    
  • }
  • /**
  • \* {@inheritdoc}
    
  • */
    
  • public function setId($id)
  • {
  •    if ($this->started) {
    
  •        throw new \LogicException('Cannot set session ID after the
    
    session has started.');

Can you link to the specific tests it breaks please? I tried to look for
them but didnt see anything.


Reply to this email directly or view it on GitHub:
https://github.com/symfony/symfony/pull/3493/files#r588565

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I have same problem

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you show me the code please?

}

return $this->sessionId;
$this->id = $id;
}

/**
* {@inheritdoc}
*/
public function getName()
{
return $this->name;
}

D1E0 /**
* {@inheritdoc}
*/
public function setName($name)
{
$this->name = $name;
}

/**
Expand All @@ -118,10 +161,18 @@ public function clear()
}

// clear out the session
$this->sessionData = array();
$this->data = array();

// reconnect the bags to the session
$this->loadSession($this->sessionData);
$this->loadSession();
}

/**
* {@inheritdoc}
*/
public function registerBag(SessionBagInterface $bag)
{
$this->bags[$bag->getName()] = $bag;
}

/**
Expand All @@ -143,10 +194,25 @@ public function getBag($name)
/**
* Generates a session ID.
*
* This doesn't need to be particularly cryptographically secure since this is just
* a mock.
*
* @return string
*/
protected function generateSessionId()
protected function generateId()
{
return sha1(uniqid(mt_rand()));
}

protected function loadSession()
{
return sha1(uniqid(mt_rand(), true));
foreach ($this->bags as $bag) {
$key = $bag->getStorageKey();
$this->data[$key] = isset($this->data[$key]) ? $this->data[$key] : array();
$bag->initialize($this->data[$key]);
}

$this->started = true;
$this->closed = false;
}
}
Loading
0