[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

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

   1  <?php
   2  namespace GuzzleHttp\Cookie;
   3  
   4  use GuzzleHttp\Utils;
   5  
   6  /**
   7   * Persists non-session cookies using a JSON formatted file
   8   */
   9  class FileCookieJar extends CookieJar
  10  {
  11      /** @var string filename */
  12      private $filename;
  13  
  14      /**
  15       * Create a new FileCookieJar object
  16       *
  17       * @param string $cookieFile File to store the cookie data
  18       *
  19       * @throws \RuntimeException if the file cannot be found or created
  20       */
  21      public function __construct($cookieFile)
  22      {
  23          $this->filename = $cookieFile;
  24  
  25          if (file_exists($cookieFile)) {
  26              $this->load($cookieFile);
  27          }
  28      }
  29  
  30      /**
  31       * Saves the file when shutting down
  32       */
  33      public function __destruct()
  34      {
  35          $this->save($this->filename);
  36      }
  37  
  38      /**
  39       * Saves the cookies to a file.
  40       *
  41       * @param string $filename File to save
  42       * @throws \RuntimeException if the file cannot be found or created
  43       */
  44      public function save($filename)
  45      {
  46          $json = [];
  47          foreach ($this as $cookie) {
  48              if ($cookie->getExpires() && !$cookie->getDiscard()) {
  49                  $json[] = $cookie->toArray();
  50              }
  51          }
  52  
  53          if (false === file_put_contents($filename, json_encode($json), LOCK_EX)) {
  54              // @codeCoverageIgnoreStart
  55              throw new \RuntimeException("Unable to save file {$filename}");
  56              // @codeCoverageIgnoreEnd
  57          }
  58      }
  59  
  60      /**
  61       * Load cookies from a JSON formatted file.
  62       *
  63       * Old cookies are kept unless overwritten by newly loaded ones.
  64       *
  65       * @param string $filename Cookie file to load.
  66       * @throws \RuntimeException if the file cannot be loaded.
  67       */
  68      public function load($filename)
  69      {
  70          $json = file_get_contents($filename);
  71          if (false === $json) {
  72              // @codeCoverageIgnoreStart
  73              throw new \RuntimeException("Unable to load file {$filename}");
  74              // @codeCoverageIgnoreEnd
  75          }
  76  
  77          $data = Utils::jsonDecode($json, true);
  78          if (is_array($data)) {
  79              foreach (Utils::jsonDecode($json, true) as $cookie) {
  80                  $this->setCookie(new SetCookie($cookie));
  81              }
  82          } elseif (strlen($data)) {
  83              throw new \RuntimeException("Invalid cookie file: {$filename}");
  84          }
  85      }
  86  }


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