[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/guzzlehttp/guzzle/src/Cookie/ -> SessionCookieJar.php (source)

   1  <?php
   2  namespace GuzzleHttp\Cookie;
   3  
   4  use GuzzleHttp\Utils;
   5  
   6  /**
   7   * Persists cookies in the client session
   8   */
   9  class SessionCookieJar extends CookieJar
  10  {
  11      /** @var string session key */
  12      private $sessionKey;
  13  
  14      /**
  15       * Create a new SessionCookieJar object
  16       *
  17       * @param string $sessionKey Session key name to store the cookie data in session
  18       */
  19      public function __construct($sessionKey)
  20      {
  21          $this->sessionKey = $sessionKey;
  22          $this->load();
  23      }
  24  
  25      /**
  26       * Saves cookies to session when shutting down
  27       */
  28      public function __destruct()
  29      {
  30          $this->save();
  31      }
  32  
  33      /**
  34       * Save cookies to the client session
  35       */
  36      public function save()
  37      {
  38          $json = [];
  39          foreach ($this as $cookie) {
  40              if ($cookie->getExpires() && !$cookie->getDiscard()) {
  41                  $json[] = $cookie->toArray();
  42              }
  43          }
  44  
  45          $_SESSION[$this->sessionKey] = json_encode($json);
  46      }
  47  
  48      /**
  49       * Load the contents of the client session into the data array
  50       */
  51      protected function load()
  52      {
  53          $cookieJar = isset($_SESSION[$this->sessionKey])
  54              ? $_SESSION[$this->sessionKey]
  55              : null;
  56  
  57          $data = Utils::jsonDecode($cookieJar, true);
  58          if (is_array($data)) {
  59              foreach ($data as $cookie) {
  60                  $this->setCookie(new SetCookie($cookie));
  61              }
  62          } elseif (strlen($data)) {
  63              throw new \RuntimeException("Invalid cookie data");
  64          }
  65      }
  66  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1