Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[HttpFoundation] fixed bag registration after session start #16136

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@ public function clear()
public function registerBag(SessionBagInterface $bag)
{
$this->bags[$bag->getName()] = $bag;

if ($this->isStarted()) {
$this->loadSessionBag($bag);
}
}

/**
Expand Down Expand Up @@ -422,12 +426,28 @@ protected function loadSession(array &$session = null)
$bags = array_merge($this->bags, array($this->metadataBag));

foreach ($bags as $bag) {
$key = $bag->getStorageKey();
$session[$key] = isset($session[$key]) ? $session[$key] : array();
$bag->initialize($session[$key]);
$this->loadSessionBag($bag, $session);
}

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

/**
* Load and initialize the session for the given bag and pass the
* session variable into the bag.
*
* @param SessionBagInterface $bag
* @param array|null $session
*/
private function loadSessionBag(SessionBagInterface $bag, array &$session = null)
{
if (null === $session) {
$session = &$_SESSION;
}

$key = $bag->getStorageKey();
$session[$key] = isset($session[$key]) ? $session[$key] : array();
$bag->initialize($session[$key]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -254,4 +254,34 @@ public function testCanStartOutside54()
$this->assertFalse(isset($_SESSION[$key]));
$storage->start();
}

public function testRegisterBagBeforeStarting()
{
$storage = $this->getStorage();
$bag = new AttributeBag('test_bag');
$bag->setName('test_bag');
$storage->registerBag($bag);

$storage->start();

// variable is set after session start, as the session handler overwrites $_SESSION
$_SESSION['test_bag']['some_key'] = 'some_value';

$this->assertSame('some_value', $bag->get('some_key'));
}

public function testRegisterBagAfterStarting()
{
$storage = $this->getStorage();
$storage->start();
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you also add a test to see what happens when you register before starting?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, see above. Did you expect it this way?


// variable is set after session start, as the session handler overwrites $_SESSION
$_SESSION['test_bag']['some_key'] = 'some_value';

$bag = new AttributeBag('test_bag');
$bag->setName('test_bag');
$storage->registerBag($bag);

$this->assertSame('some_value', $bag->get('some_key'));
}
}