Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. MockFileSessionStorage.php

function MockFileSessionStorage::save

Overrides MockArraySessionStorage::save

File

vendor/symfony/http-foundation/Session/Storage/MockFileSessionStorage.php, line 76

Class

MockFileSessionStorage
MockFileSessionStorage is used to mock sessions for functional testing where you may need to persist session data across separate PHP processes.

Namespace

Symfony\Component\HttpFoundation\Session\Storage

Code

public function save() : void {
    if (!$this->started) {
        throw new \RuntimeException('Trying to save a session that was not started yet or was already closed.');
    }
    $data = $this->data;
    foreach ($this->bags as $bag) {
        if (empty($data[$key = $bag->getStorageKey()])) {
            unset($data[$key]);
        }
    }
    if ([
        $key = $this->metadataBag
            ->getStorageKey(),
    ] === array_keys($data)) {
        unset($data[$key]);
    }
    try {
        if ($data) {
            $path = $this->getFilePath();
            $tmp = $path . bin2hex(random_bytes(6));
            file_put_contents($tmp, serialize($data));
            rename($tmp, $path);
        }
        else {
            $this->destroy();
        }
    } finally {
        $this->data = $data;
    }
    // this is needed when the session object is re-used across multiple requests
    // in functional tests.
    $this->started = false;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal