PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.0
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.0
28.6 28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 All 130 releases
wordpress-seo / vendor_prefixed / guzzlehttp / guzzle / src / Cookie / FileCookieJar.php

FileCookieJar.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.0, at vendor_prefixed/guzzlehttp/guzzle/src/Cookie/FileCookieJar.php

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